装饰者模式(2)
https://blog.****.net/pnjlc/article/details/52701929
装饰模式的java实现例子
说明:本文是《大话设计模式》一书的学习文摘和网上相关信息文摘,原书代码例子用C#写,下面用Java改写。
1、装饰模式:在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。
2、装饰模式由4种角色组成:
(1)抽象构件(Component)角色:给出一个抽象接口,以规范准备接收附加职责的对象。
(2)具体构件(Concrete Component)角色:定义一个将要接收附加职责的类。
(3)装饰(Decorator)角色:持有一个构件(Component)对象的实例,并实现一个与抽象构件接口一致的接口,从外类来扩展Component类的功能,但对于Component类来说,是无需知道Decorato的存在的。
(4)具体装饰(Concrete Decorator)角色:负责给构件对象添加上附加的职责。
3、装饰模式的UML类图
4、装饰模式的特点
(1)装饰对象和真实对象有相同的接口。这样客户端对象就能以和真实对象相同的方式和装饰对象交互。
(2)装饰对象包含一个真实对象的引用。
(3)装饰对象接受所有来自客户端的请求。它把这些请求转发给真实的对象。
(4)装饰对象可以在转发这些请求以前或以后增加一些附加功能。这样就确保了在运行时,不用修改给定对象的结构就可以在外部增加附加的功能。在面向对象的设计中,通常是通过继承来实现对给定类的功能扩展。
5、适用性
(1)需要扩展一个类的功能,或给一个类添加附加职责。
(2)需要动态的给一个对象添加功能,这些功能可以再动态的撤销。
(3)需要增加由一些基本功能的排列组合而产生的非常大量的功能,从而使继承关系变的不现实。
(4)当不能采用生成子类的方法进行扩充时。一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。另一种情况可能是因为类定义被隐藏,或类定义不能用于生成子类。
6、模式简化
(1)如果只有一个Concrete Component类而没有抽象的Component接口时,可以让Decorator继承Concrete Component。
(2)如果只有一个Concrete Decorator类时,可以将Decorator和Concrete Decorator合并。
7、代码实现
- package demo4;
- /**
- * 抽象构件角色
- */
- public abstract class Component {
- public abstract void operation();
- }
- package demo4;
- /**
- * 具体构件角色
- */
- public class ConcreteComponent extends Component {
- @Override
- public void operation() {
- System.out.println("具体对象的操作");
- }
- }
- package demo4;
- /**
- * 装饰角色
- */
- public abstract class Decorator extends Component {
- protected Component component;
- public void setComponent(Component component){
- this.component = component;
- }
- @Override
- public void operation() {
- if(component != null){
- component.operation();
- }
- }
- }
- package demo4;
- /**
- * 具体装饰角色A
- */
- public class ConcreteDecoratorA extends Decorator {
- //本类的独有功能
- private String addedState;
- //首先运行原Component的operation(),再执行本类的功能,相当于对原Component进行了装饰
- @Override
- public void operation(){
- super.operation();
- addedState = "New State";
- System.out.println("具体装饰对象A的操作。" + addedState);
- }
- }
- package demo4;
- /**
- * 具体装饰角色B
- */
- public class ConcreteDecoratorB extends Decorator {
- //首先运行原Component的operation(),再执行本类的功能,相当于对原Component进行了装饰
- @Override
- public void operation() {
- super.operation();
- AddedBehavior();
- System.out.println("具体装饰对象B的操作");
- }
- //本类的独有功能
- private void AddedBehavior() {
- System.out.println("我是具体装饰对象B的独有方法");
- }
- }
- package demo4;
- /**
- * 客户端代码
- */
- public class Demo4 {
- public static void main(String[] args) {
- ConcreteComponent c = new ConcreteComponent();
- ConcreteDecoratorA d1 = new ConcreteDecoratorA();
- ConcreteDecoratorB d2 = new ConcreteDecoratorB();
- d1.setComponent(c);
- d2.setComponent(d1);
- d2.operation();
- /*运行输出结果:
- 具体对象的操作
- 具体装饰对象A的操作。New State
- 我是具体装饰对象B的独有方法
- 具体装饰对象B的操作
- */
- }
- }
8、一个例子:给人搭配不同的服饰
- package demo5;
- /**
- *“Person”类(ConcreteComponent)
- */
- public class Person {
- public Person() {
- }
- private String name;
- public Person(String name){
- this.name = name;
- }
- public void show(){
- System.out.println(String.format("装扮的%s", name));
- }
- }
- package demo5;
- /**
- * 服饰类(Decorator)
- */
- public class Finery extends Person {
- protected Person component;
- public void decorate(Person component) {
- this.component = component;
- }
- @Override
- public void show() {
- if(component != null){
- component.show();
- }
- }
- }
- package demo5;
- /**
- * 具体服饰类(ConcreteDecorator)
- */
- public class TShirts extends Finery {
- @Override
- public void show() {
- System.out.println("大T恤");
- super.show();
- }
- }
- package demo5;
- /**
- * 具体服饰类(ConcreteDecorator)
- */
- public class Sneakers extends Finery {
- @Override
- public void show() {
- System.out.println("球鞋");
- super.show();
- }
- }
- package demo5;
- /**
- * 具体服饰类(ConcreteDecorator)
- */
- public class BigTrouser extends Finery {
- @Override
- public void show() {
- System.out.println("垮裤");
- super.show();
- }
- }
- package demo5;
- /**
- * 客户端代码
- */
- public class Demo5 {
- public static void main(String[] args) {
- Person xc = new Person("小菜");
- Sneakers pqx = new Sneakers();
- BigTrouser kk = new BigTrouser();
- TShirts dtx = new TShirts();
- pqx.decorate(xc);
- kk.decorate(pqx);
- dtx.decorate(kk);
- dtx.show();
- /*
- 运行结果:
- 大T恤
- 垮裤
- 球鞋
- 装扮的小菜
- */
- }
- }
说明:本文是《大话设计模式》一书的学习文摘和网上相关信息文摘,原书代码例子用C#写,下面用Java改写。
1、装饰模式:在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。
2、装饰模式由4种角色组成:
(1)抽象构件(Component)角色:给出一个抽象接口,以规范准备接收附加职责的对象。
(2)具体构件(Concrete Component)角色:定义一个将要接收附加职责的类。
(3)装饰(Decorator)角色:持有一个构件(Component)对象的实例,并实现一个与抽象构件接口一致的接口,从外类来扩展Component类的功能,但对于Component类来说,是无需知道Decorato的存在的。
(4)具体装饰(Concrete Decorator)角色:负责给构件对象添加上附加的职责。
3、装饰模式的UML类图
4、装饰模式的特点
(1)装饰对象和真实对象有相同的接口。这样客户端对象就能以和真实对象相同的方式和装饰对象交互。
(2)装饰对象包含一个真实对象的引用。
(3)装饰对象接受所有来自客户端的请求。它把这些请求转发给真实的对象。
(4)装饰对象可以在转发这些请求以前或以后增加一些附加功能。这样就确保了在运行时,不用修改给定对象的结构就可以在外部增加附加的功能。在面向对象的设计中,通常是通过继承来实现对给定类的功能扩展。
5、适用性
(1)需要扩展一个类的功能,或给一个类添加附加职责。
(2)需要动态的给一个对象添加功能,这些功能可以再动态的撤销。
(3)需要增加由一些基本功能的排列组合而产生的非常大量的功能,从而使继承关系变的不现实。
(4)当不能采用生成子类的方法进行扩充时。一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。另一种情况可能是因为类定义被隐藏,或类定义不能用于生成子类。
6、模式简化
(1)如果只有一个Concrete Component类而没有抽象的Component接口时,可以让Decorator继承Concrete Component。
(2)如果只有一个Concrete Decorator类时,可以将Decorator和Concrete Decorator合并。
7、代码实现
- package demo4;
- /**
- * 抽象构件角色
- */
- public abstract class Component {
- public abstract void operation();
- }
- package demo4;
- /**
- * 具体构件角色
- */
- public class ConcreteComponent extends Component {
- @Override
- public void operation() {
- System.out.println("具体对象的操作");
- }
- }
- package demo4;
- /**
- * 装饰角色
- */
- public abstract class Decorator extends Component {
- protected Component component;
- public void setComponent(Component component){
- this.component = component;
- }
- @Override
- public void operation() {
- if(component != null){
- component.operation();
- }
- }
- }
- package demo4;
- /**
- * 具体装饰角色A
- */
- public class ConcreteDecoratorA extends Decorator {
- //本类的独有功能
- private String addedState;
- //首先运行原Component的operation(),再执行本类的功能,相当于对原Component进行了装饰
- @Override
- public void operation(){
- super.operation();
- addedState = "New State";
- System.out.println("具体装饰对象A的操作。" + addedState);
- }
- }
- package demo4;
- /**
- * 具体装饰角色B
- */
- public class ConcreteDecoratorB extends Decorator {
- //首先运行原Component的operation(),再执行本类的功能,相当于对原Component进行了装饰
- @Override
- public void operation() {
- super.operation();
- AddedBehavior();
- System.out.println("具体装饰对象B的操作");
- }
- //本类的独有功能
- private void AddedBehavior() {
- System.out.println("我是具体装饰对象B的独有方法");
- }
- }
- package demo4;
- /**
- * 客户端代码
- */
- public class Demo4 {
- public static void main(String[] args) {
- ConcreteComponent c = new ConcreteComponent();
- ConcreteDecoratorA d1 = new ConcreteDecoratorA();
- ConcreteDecoratorB d2 = new ConcreteDecoratorB();
- d1.setComponent(c);
- d2.setComponent(d1);
- d2.operation();
- /*运行输出结果:
- 具体对象的操作
- 具体装饰对象A的操作。New State
- 我是具体装饰对象B的独有方法
- 具体装饰对象B的操作
- */
- }
- }
8、一个例子:给人搭配不同的服饰
- package demo5;
- /**
- *“Person”类(ConcreteComponent)
- */
- public class Person {
- public Person() {
- }
- private String name;
- public Person(String name){
- this.name = name;
- }
- public void show(){
- System.out.println(String.format("装扮的%s", name));
- }
- }
- package demo5;
- /**
- * 服饰类(Decorator)
- */
- public class Finery extends Person {
- protected Person component;
- public void decorate(Person component) {
- this.component = component;
- }
- @Override
- public void show() {
- if(component != null){
- component.show();
- }
- }
- }
- package demo5;
- /**
- * 具体服饰类(ConcreteDecorator)
- */
- public class TShirts extends Finery {
- @Override
- public void show() {
- System.out.println("大T恤");
- super.show();
- }
- }
- package demo5;
- /**
- * 具体服饰类(ConcreteDecorator)
- */
- public class Sneakers extends Finery {
- @Override
- public void show() {
- System.out.println("球鞋");
- super.show();
- }
- }
- package demo5;
- /**
- * 具体服饰类(ConcreteDecorator)
- */
- public class BigTrouser extends Finery {
- @Override
- public void show() {
- System.out.println("垮裤");
- super.show();
- }
- }
- package demo5;
- /**
- * 客户端代码
- */
- public class Demo5 {
- public static void main(String[] args) {
- Person xc = new Person("小菜");
- Sneakers pqx = new Sneakers();
- BigTrouser kk = new BigTrouser();
- TShirts dtx = new TShirts();
- pqx.decorate(xc);
- kk.decorate(pqx);
- dtx.decorate(kk);
- dtx.show();
- /*
- 运行结果:
- 大T恤
- 垮裤
- 球鞋
- 装扮的小菜
- */
- }
- }