网站黑名单,建网站需要什么设计专业,手机网站设计教程,海口房产网4.4 装饰模式
1.模式定义
不改变现有对象结构的情况下#xff0c;动态地给该对象增加一些职责#xff08;即增加其额外功能#xff09;的模式。
2.模式结构 抽象构件角色 #xff1a;定义一个抽象接口以规范准备接收附加责任的对象。客户端可以方便调用装饰类和被装饰类…4.4 装饰模式
1.模式定义
不改变现有对象结构的情况下动态地给该对象增加一些职责即增加其额外功能的模式。
2.模式结构 抽象构件角色 定义一个抽象接口以规范准备接收附加责任的对象。客户端可以方便调用装饰类和被装饰类。
具体构件角色 实现抽象构件通过装饰角色为其添加一些职责。
抽象装饰角色 继承或实现抽象构件并包含具体构件的实例可以通过其子类扩展具体构件的功能。
具体装饰角色 实现抽象装饰的相关方法并给具体构件对象添加附加的责任。
3.模式原理
在不改变对象本身的基础之上给对象添加或删除行为往往可以通过继承机制或者是关联机制实现
继承机制通过子类对父类的继承重写或添加新的方法来扩展类关联机制将一个类的对象嵌入另一个类的对象之中进而在另一个类中扩展其行为通过递归嵌套实现多层装饰
4.代码模板
抽象装饰类
public class Decorator implements Component {//关联抽象构建private Component component;//构造注入具体构建public Decorator(Component component) {this.component component;}Overridepublic void operation() {component.operation();}
}具体装饰类
public class ConcreteDecorator extends Decorator{public ConcreteDecorator(Component component) {super(component);}public void operation() {super.operation();addBehavior();}private void addBehavior() {//新增方法}
}5.案例分析 public interface Call {public void callMusic();
}public class Phone implements Call {public Phone() {System.out.println(普通手机);}Overridepublic void callMusic() {System.out.println(来电话了手机发出响声);}
}public class Decorator implements Call{private Call call;public Decorator(Call call) {this.call call;}public void setCall(Call call) {this.call call;}Overridepublic void callMusic() {System.out.println(来电话了手机发出响声);}
}public class JarPhone extends Decorator {public JarPhone(Call call) {super(call);System.out.println(振动手机);}public void jar() {super.callMusic();System.out.println(振动~~~);}
}public class LightPhone extends Decorator{public LightPhone(Call call) {super(call);System.out.println(闪光手机);}public void light() {super.callMusic();System.out.println(闪光~~~);}
}public class Main {public static void main(String[] args) {Phone phone new Phone();System.out.println(电话来了);phone.callMusic();System.out.println(————————————————);JarPhone jarPhone new JarPhone(phone);System.out.println(电话来了);jarPhone.jar();System.out.println(————————————————);LightPhone lightPhone new LightPhone(phone);System.out.println(电话来了);lightPhone.light();System.out.println(————————————————);//将闪光手机改装成可以振动且闪光的手机System.out.println(组装手机);lightPhone.light();jarPhone.setCall(lightPhone);jarPhone.jar();}
}6.模式优缺点 7.模式使用场景 8.模式应用
IO流中使用InputStream和OutputStream中只提供了简单的读写操作通过装饰模式可以得到具有文件输入输出的FileInputStream等javax.swing中也有大量使用