装饰模式 Decorator

来源:互联网 发布:ubuntu命令行编辑文件 编辑:程序博客网 时间:2024/06/03 23:01

1.装饰模式

装饰模式就是给一个对象增加一些新的功能,而且是动态的,要求装饰对象和被装饰对象实现同一个接口,装饰对象持有被装饰对象的实例.该模式中,要被扩展的类可以是包含抽象方法的抽象类,也可以是包含虚方法的实例类,也可以是普通实例类。装饰模式就是在原有基类上做扩展,至于基类是什么性质并不重要.装饰器模式的应用场景:1、需要扩展一个类的功能。2、动态的为一个对象增加功能,而且还能动态撤销。(继承不能做到这一点,继承的功能是静态的,不能动态增删。)缺点:产生过多相似的对象,不易排错!

2.结构图

这里写图片描述

3.代码

要求是:写一个可以给人搭配不同服饰的系统

package com.hlf.designPatterns.Decorator.src.decorator;/** * 装饰类接口,就直接有一个显示穿什么衣服的方法 * Created by hlf on 2016/8/23. */public interface Person {    void show();}

package com.hlf.designPatterns.Decorator.src.decorator;/** * 人物名:菜鸟,实现穿什么衣服 * Created by hlf on 2016/8/23. */public class CaiNiao implements Person{    @Override    public void show() {        System.out.println("我是一个菜鸟,具有内在美");    }}

package com.hlf.designPatterns.Decorator.src.decorator;/** * 装饰类 * Created by hlf on 2016/8/23. */public abstract class Decorator implements Person{    //装饰对象持有被装饰对象的实例    private Person person;    public Decorator(Person p) {        this.person = p;    }    //接口的方法要在这里实现,不然后面的实现类就没法用super.show    @Override    public void show() {        person.show();    }}

package com.hlf.designPatterns.Decorator.src.decorator;/** * 穿T恤 * Created by hlf on 2016/8/23. */public class TShirt extends Decorator{    public TShirt(Person p) {        super(p);    }    @Override    public void show() {        //原有功能不能变        super.show();        //加上要添加的功能        getTShirt();    }    //这里才是重点,写上要添加的功能    private void getTShirt(){        System.out.println("穿上大T恤");    }}

package com.hlf.designPatterns.Decorator.src.decorator;/** * 穿大垮裤 * Created by hlf on 2016/8/23. */public class BigTrouser extends Decorator{    public BigTrouser(Person p) {        super(p);    }    @Override    public void show() {        //原有功能不能变        super.show();        //加上要添加的功能        getBigTrouser();    }    //这里才是重点,写上要添加的功能    private void getBigTrouser(){        System.out.println("穿上大垮裤");    }}

package com.hlf.designPatterns.Decorator.src.decorator;/** * 穿破球鞋 * Created by hlf on 2016/8/23. */public class WearSneakers extends Decorator{    public WearSneakers(Person p) {        super(p);    }    @Override    public void show() {        //原有功能不能变        super.show();        //加上要添加的功能        getWearSneakers();    }    //这里才是重点,写上要添加的功能    private void getWearSneakers(){        System.out.println("穿上破球鞋");    }}

package com.hlf.designPatterns.Decorator.src.decorator;/** * 测试效果 * Created by hlf on 2016/8/23. */public class TestDecorator {    public static void main(String[] args) {        //首先创建出具体实例,菜鸟        CaiNiao caiNiao = new CaiNiao();        //具体添加什么功能,先穿 T恤        TShirt tShirt = new TShirt(caiNiao);//        tShirt.show();        //再穿裤子        BigTrouser bigTrouser = new BigTrouser(tShirt);//        bigTrouser.show();        //再穿鞋子        WearSneakers wearSneakers = new WearSneakers(bigTrouser);        wearSneakers.show();        //这样显示有点怪,直接输出最后一行就好。        /*        我是一个菜鸟,具有内在美        穿上大T恤        穿上大垮裤        穿上破球鞋         */    }}

0 0
原创粉丝点击