装饰模式设计模式

**装饰模式:**动态地给一个对象添加一些额外地职责,就增加功能来说,装饰模式比生成地之类更加灵活。
如:衣服、鞋子、领带、披风其实都可以理解为对人地装饰。
其结构图:
在这里插入图片描述
Component是定义一个对象的接口,可以给这些对象动态地添加职责。ConcreteComponent是定义一个具体地对象,也可以给这个对象添加一些职责
Decorator装饰抽象类,继承了Component从外类扩展Component的功能,对于Component无需知道Decore=ator的存在。至于ConcreteDecorator就是具体的装饰对象,起到给Componen添加职责。

public  abstract  class Component {
    public abstract  void Operation();
}
public class ConcreteComponent  extends  Component{
    @Override
    public void Operation() {
        System.out.println("具体对象地操作");
    }
}
public class Decorator extends Component{
    private Component component;
    @Override
    public void Operation() {
        if(component!=null){
            this.component.Operation();
        }
    }

    public Component getComponent() {
        return component;
    }

    public void setComponent(Component component) {
        this.component = component;
    }
}```

```java
public class ConcreteDecoratorA extends  Decorator{
    private String addedState;
    @Override
    public void Operation() {
        super.Operation();
        this.addedState="NEW STATE";
        System.out.println("具体装饰对象A的操作");
    }
}
public class ConcreteDecoratorB extends  Decorator{
    @Override
    public void Operation() {
        super.Operation();
        this.AddedBehavior();
        System.out.println("具体装饰对象B的操作");
    }
    private void AddedBehavior(){
        System.out.println("具体装饰对象B调用AddedBehavior方法");

    }
}
public class Client {
    public static void main(String[] args) {
        ConcreteComponent c=new ConcreteComponent();
        ConcreteDecoratorA a=new ConcreteDecoratorA();
        ConcreteDecoratorB b=new ConcreteDecoratorB();
        a.setComponent(c);
        b.setComponent(a);
        b.Operation();
    }
}

以上例子装饰模式利用SetComponent来对对象进行包装。这样每个装饰对象的实现就和使用这个对象分离开了。每个装饰对象只关心自己的功能,不需要关心如何被添加到对象链当中。
例如穿衣服的例子:

public class Person {
    public Person(String name) {
        this.name = name;
    }

    public Person() {
    }

    private String name;
    public void Show(){
        System.out.println("装饰的"+name);
    }

}
 * 服饰类
 *****************************************************************/
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();
        }
    }
}
public class BigTrouser extends Finery{

    @Override
    public void Show() {
      System.out.println("跨库");
      super.Show();
    }
}
public class Sneakers extends Finery{

    @Override
    public void Show() {
      System.out.println("破球鞋");
      super.Show();
    }
}
public class Tshirts extends Finery{


    @Override
    public void Show() {
      System.out.println("大T血");
        super.Show();

    }
}
public class Client {
    public static void main(String[] args) {
        Person xc=new Person("小菜");
        System.out.println("第一种装扮");
        Sneakers sneakers=new Sneakers();
        Tshirts tshirts=new Tshirts();
        BigTrouser bigTrouser=new BigTrouser();
        sneakers.Decorate(xc);
        tshirts.Decorate(sneakers);
        bigTrouser.Decorate(tshirts);
        bigTrouser.Show();
    }
}

装饰模式可以有效的把类的核心职责和装饰功能区分开,可以去除相关类中重复的装饰逻辑。解决了新加入的代码那些仅仅为了满足一些只有在某种情况下才会执行的特殊行为需要,客户代码就可以在运行时候根据需要选择地,按顺序地使用装饰功能包装对象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值