23种设计模式之装饰模式(8)

  • 把所需的功能按正确的顺序串联起来进行控制
  • 装饰模式(Decorator),动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。
  • Component是定义一个对象接口,可以给这些对象动态地添加职责。ConcreteComponent是定义了一个具体的对象,也可以给这个对象添加一些职责。Decorator,装饰抽象类,继承了Component,从外类来扩展Component类的功能,但对于Component来说,是无需知道Decorator的存在的。至于ConcreteDecorator就是具体的装饰对象,起到给Component添加职责的功能。
  • 装饰模式是利用setComponent来对对象进行包装的。这样每个装饰对象的实现就和如何使用这个对象分离开了,每个装饰只关心自己的功能,不需要关心如何被添加到对象链当中。
  • 如果只有一个ConcreteComponent类而没有抽象的Component类,那么decorator类可以是ConcreteComponent的一个子类。同样道理,如果只有一个ConcreteDecorator类那么就没就必要建立一个单独的decorator类,而可以把decorator和ConcreteDecorator的责任合并成一个类。
  • 装饰模式是为已有功能动态的添加更多功能的一种方式。
  • 当系统需要新功能的时候,是向旧的类中添加新的代码。这些新加的代码通常装饰了原有类的核心职责或主要行为。

装饰设计模式

装饰(Decorator)模式的定义:指在不改变现有对象结构的情况下,动态地给该对象增加一些职责(即增加其额外功能)的模式,它属于对象结构型模式。

  • 采用装饰模式扩展对象的功能比采用继承方式更加灵活。
  • 可以设计出多个不同的具体装饰类,创造出多个不同行为的组合。

其主要缺点是:装饰模式增加了许多子类,如果过度使用会使程序变得很复杂。
在这里插入图片描述

  • Component.java

    public abstract class Component {
        public abstract void Opration();
    }
    
  • ConcreteComponent .java

    public class ConcreteComponent extends Component {
        @Override public void Opration() {
            System.out.println("准备出门");
        }
    }
    
  • Decorator.java

    public abstract class Decorator extends Component {
        protected Component component;
    
        public void setComponent(Component component) {
            this.component = component;
        }
    
        @Override
        public void Opration() {
            if (component != null) {
                component.Opration();
            }
        }
    }
    
  • ConcreteDecoratorA .java

    public class ConcreteDecoratorA extends Decorator{
        private String addedState;
    
        @Override public void Opration() {
            super.Opration();
            addedState = "新裙子";
            System.out.println("穿上"+addedState);
        }
    }
    
  • ConcreteDecoratorB .java

    public class ConcreteDecoratorB extends Decorator{
        private String addedState;
    
        @Override public void Opration() {
            super.Opration();
            AddedBehavior();
            System.out.println("配上长剑");
        }
    
        private void AddedBehavior() {
    
        }
    }
    
  • DecoratorMain.java

    public class DecoratorMain {
        public static void main(String[] args) {
            final ConcreteComponent c = new ConcreteComponent();
            final ConcreteDecoratorA d1 = new ConcreteDecoratorA();
            final ConcreteDecoratorB d2 = new ConcreteDecoratorB();
    
            d1.setComponent(c);
            d2.setComponent(d1);
            d2.Opration();
    
            System.out.println("出门!");
        }
    }
    
  • eg:2

    public interface Coder {
        public void code();
    }
    
    public class Student implements Coder {
        @Override
        public void code() {
            System.out.println("javaSE");
            System.out.println("javaWeb");
        }
    }
    
    public class HighStudent implements Coder {
    	//1.获取被包装类的引用
        private Student s;
    	//2.通过构造函数创建对象的时候,传入被包装的对象
        public HighStudent(Student s){
            this.s = s;
        }
    	//对原有功能尽行升级
        @Override
        public void code() {
            s.code();
            System.out.println("MySQL");
            System.out.println("SSM");
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值