An Image/Link below is provided (as is) to download presentation
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.Content is provided to you AS IS for your information and personal use only.Download presentation by click this link.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.During download, if you can't get a presentation, the file might be deleted by the publisher.
Java Java 设计模式 设计模式 第四章 命令模式 命令模式(别名:动作,事务) 将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤消的操作。 Command Pattern(Another Name: Action, Transaction) Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. 2014/11/17 12
Java Java 设计模式 设计模式 第五章 观察者模式 观察者模式(别名:依赖,发布-订阅) 定义对象间的一种一对多的依赖关系,当一个对象的状态发生变化时,所有依赖于它的对象都得到通知并被自动更新。 Observer Pattern(Another Name: Dependents, Publish-Subscribe) Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.. 2014/11/17 24
Java Java 设计模式 设计模式 第六章 装饰模式 装饰模式(别名:包装器) 动态地给对象添加一些额外的职责。就功能来说装饰模式相比生成子类更为灵活。 Decorator Pattern(Another Name: Wrapper) Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. 2014/11/17 38
Java Java 设计模式 设计模式 装饰模式的结构的描述与使用 1.抽象组件 : Bird.java public abstract class Bird{ public abstract int fly(); } 2014/11/17 43
Java Java 设计模式 设计模式 装饰模式的结构的描述与使用 2.具体组件 : Sparrow.java public class Sparrow extends Bird{ public final int DISTANCE=100; public int fly(){ return DISTANCE; } } 2014/11/17 44
Java Java 设计模式 设计模式 装饰模式的结构的描述与使用 3.装饰 (Decorator):Decorator.java public abstract class Decorator extends Bird{ protected Bird bird; public Decorator(){ } public Decorator(Bird bird){ this.bird=bird; } } 2014/11/17 45
Java Java 设计模式 设计模式 装饰模式的结构的描述与使用 4.具体装饰(ConcreteDecotator):SparrowDecorator.java public class SparrowDecorator extends Decorator{ public final int DISTANCE=50; //eleFly方法能飞50米 SparrowDecorator(Bird bird){ super(bird); } public int fly(){ int distance=0; distance=bird.fly()+eleFly(); return distance; } private int eleFly(){ //装饰者新添加的方法 return DISTANCE; } } 2014/11/17 46
Java Java 设计模式 设计模式 装饰模式的结构的描述与使用 5.应用 Application.java public class Application{ public void needBird(Bird bird){ int flyDistance=bird.fly(); System.out.println("这只鸟能飞行"+flyDistance +"米"); } public static void main(String args[]){ Application client=new Application (); Bird sparrow=new Sparrow(); Bird sparrowDecorator1= new SparrowDecorator(sparrow); Bird sparrowDecorator2= new SparrowDecorator(sparrowDecorator1); client.needBird(sparrowDecorator1); client.needBird(sparrowDecorator2); } } 2014/11/17 47