美文网首页
大话设计模式-装饰模式-2020-09-17-周四

大话设计模式-装饰模式-2020-09-17-周四

作者: 勇往直前888 | 来源:发表于2020-09-22 17:30 被阅读0次

定义

动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。

结构图

装饰模式
  • 如果只有一个ConcreteComponent类而没有抽象的Component类,那么Decorator类可以是ConcreteComponent的一个子类。

  • 如果只有一个ConcreteDecorator类,那么就没必要建立单独的Decorator类,而可以把Decorator和ConcreteDecorator的责任合并成一个类。

  • 装饰模式就是为已有功能动态添加更多功能的一种方式。

  • 把类中的装饰功能从类中搬移去除,这样可以简化原有的类。有效的把类的核心职责和装饰功能区分开了。

设计模式之六(装饰模式)

大话设计模式第六章之:装饰者模式

换装的例子

  • 结构图
image.png

换装需要有一个人类用于指定是谁换装、一个服装类为具体服装类的父类、以及服装类下的各种具体服装的类。

  • 界面
image.png
  • Person类(ConcreteComponent)
    核心功能只有一个类,所以不需要父类Component,直接用具体的子类就可以了。
class Person {
    private String name;
    protected String description;

    Person() {}

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

    public void show() {
        description = name + "的装扮:\n";
        Log.v("装饰模式", name + "的装扮:");
    }
}
  • Finery服饰类(Decorator)
    由于有多个具体的服饰类,所以要引入一个公共的父类。
class Finery extends Person {
    protected Person component;

    public void decorate(Person component) {
        this.component = component;
    }

    @Override
    public void show() {
        if (null != component) {
            component.show();
            description = component.description;
        }
    }
}
  • 具体服饰类(ConcreteDecorator)
class BigTrouser extends Finery {
    @Override
    public void show() {
        super.show();
        description = super.description;
        description += "垮裤====";
        Log.v("装饰模式", "垮裤");
    }
}

class LeatherShoe extends Finery {
    @Override
    public void show() {
        super.show();
        description = super.description;
        description += "皮鞋====";
        Log.v("装饰模式", "皮鞋");
    }
}

class Sneaker extends Finery {
    @Override
    public void show() {
        super.show();
        description = super.description;
        description += "破球鞋====";
        Log.v("装饰模式", "破球鞋");
    }
}

class Suit extends Finery {
    @Override
    public void show() {
        super.show();
        description = super.description;
        description += "西装====";
        Log.v("装饰模式", "西装");
    }
}

class Tie extends Finery {
    @Override
    public void show() {
        super.show();
        description = super.description;
        description += "领带====";
        Log.v("装饰模式", "领带");
    }
}

class TShirt extends Finery {
    @Override
    public void show() {
        super.show();
        description = super.description;
        description += "大T恤====";
        Log.v("装饰模式", "大T恤");
    }
}
  • 界面调用,一级一级有点像链式调用的样子。
public void onShowButtonClick(View view) {
        CheckBox checkBoxTShirt = (CheckBox)findViewById(R.id.checkBoxTShirt);
        CheckBox checkBoxTrouser = (CheckBox)findViewById(R.id.checkBoxTrouser);
        CheckBox checkBoxTie = (CheckBox)findViewById(R.id.checkBoxTie);
        CheckBox checkBoxSuit = (CheckBox)findViewById(R.id.checkBoxSuit);
        CheckBox checkBoxSneaker = (CheckBox)findViewById(R.id.checkBoxSneaker);
        CheckBox checkBoxLeatherShoe = (CheckBox)findViewById(R.id.checkBoxLeatherShoe);
        EditText personName = (EditText)findViewById(R.id.editTextPersonName);
        TextView descriptionTextView = (TextView)findViewById(R.id.textViewDescription);

        /* 人物创建 */
        String name = personName.getText().toString();
        Person personXXX = new Person(name);

        TShirt tShirt = new TShirt();
        BigTrouser trouser = new BigTrouser();
        Tie tie = new Tie();
        Suit suit = new Suit();
        Sneaker sneaker = new Sneaker();
        LeatherShoe leatherShoe = new LeatherShoe();

        /* 装饰过程 */
        Person current = personXXX;
        if (checkBoxTShirt.isChecked()) {
            tShirt.decorate(current);
            current = tShirt;
        }
        if (checkBoxTrouser.isChecked()) {
            trouser.decorate(current);
            current = trouser;
        }
        if (checkBoxTie.isChecked()) {
            tie.decorate(current);
            current = tie;
        }
        if (checkBoxSuit.isChecked()) {
            suit.decorate(current);
            current = suit;
        }
        if (checkBoxSneaker.isChecked()) {
            sneaker.decorate(current);
            current = sneaker;
        }
        if (checkBoxLeatherShoe.isChecked()) {
            leatherShoe.decorate(current);
            current = leatherShoe;
        }

        /* 展示 */
        current.show();
        descriptionTextView.setText(current.description);
    }

Demo地址

https://gitee.com/zhangxusong888/Android/tree/master/design_pattern

相关文章

  • 大话设计模式-装饰模式-2020-09-17-周四

    定义 动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。 结构图 如果只有一个Con...

  • iOS开发之设计模式 - 装饰模式

    由《大话设计模式 - 装饰模式》的OC和部分Swift的语言转义 装饰模式 继上一篇《策略模式》 装饰模式,动态地...

  • iOS开发之设计模式 - 装饰模式

    由《大话设计模式 - 装饰模式》的OC和部分Swift的语言转义 装饰模式 继上一篇《策略模式》[https://...

  • 大话设计模式——装饰模式

    需求 写一个给人模拟搭配不同服饰的程序,可以给人换各种各样的衣服裤子的形象。 初步实现 需求比较简单,直接上代码:...

  • 大话设计模式 装饰模式

    一层继承一层。装饰模式是为已有功能动态地添加更多功能的一种方式。关键词是添加更多功能。 当系统需要新功能时,是向旧...

  • iOS开发之设计模式 - 代理模式

    由《大话设计模式 - 代理模式》的OC和部分Swift的语言转义 代理模式 继上一篇《装饰模式》[https://...

  • iOS开发之设计模式 - 代理模式

    由《大话设计模式 - 代理模式》的OC和部分Swift的语言转义 代理模式 继上一篇《装饰模式》 代理模式 小明追...

  • 大话设计模式之装饰模式

    装饰模式 装饰模式(Decorator)动态的给一个对象添加一些额外的职责。就增加功能来说,装饰模式比生成子类更为...

  • 2018-01-07

    大话设计模式——开篇 1、什么是设计模式? 设计模式(Design Pattern)...

  • 9、结构型模式-装饰器设计模式

    1、如虎添翼的设计模式-装饰器设计模式 简介:讲解-装饰器设计模式介绍和应用场景 装饰器设计模式(Decorato...

网友评论

      本文标题:大话设计模式-装饰模式-2020-09-17-周四

      本文链接:https://www.haomeiwen.com/subject/bmakyktx.html