JAVA的23种设计模式---装饰模式

概要:

该文章参考了《设计模式之禅》一书及一些前辈的博客文章

1.该文章阐述了装饰模式的基础原理及示例代码;
2.该文章适合初学设计模式的技术人员研习;
3.该文章有许多不足之处,请各位大咖指正,喷子绕道;

正文:

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

1.装饰模式示例代码实现:

package com.csdn;
/**
 * 毛坯房
 * @author Administrator
 *
 */
public interface RoughHouse {
    //毛坯房的装修报告
    public void RenovationReport();
    //房主的装修通过确认签字
    public void sign(String name);
}
package com.csdn;
/**
 * 基础装修完成了的房子
 * @author Administrator
 *
 */
public class WellAppointedHouse implements RoughHouse {
    //基础装修完成了的房子的装修报告
    @Override
    public void RenovationReport() {
        System.out.println("房间的基础装修都已经完成");
    }
    //房主的装修通过确认签字
    @Override
    public void sign(String name) {
        System.out.println("户主签名:"+name);
    }
}
package com.csdn;
/**
 * 装饰类(可理解为代理类)
 * @author Administrator
 *
 */
public abstract class Decorator implements RoughHouse {
    private RoughHouse roughHouse;
    public Decorator(RoughHouse _roughHouse) {
        this.roughHouse = _roughHouse;
    }
    @Override
    public void RenovationReport() {
        this.roughHouse.RenovationReport();
    }
    @Override
    public void sign(String name) {
        this.roughHouse.sign(name);
    }
}
package com.csdn;
/**
 * 用花做装饰
 * @author Administrator
 *
 */
public class FlowerDecorator extends Decorator{
    public FlowerDecorator(RoughHouse _roughHouse) {
        super(_roughHouse);
    }
    //用花做装饰
    public void decorateWithFlowers(){
        System.out.println("屋子里摆上花");
    }
    @Override
    public void RenovationReport() {
        super.RenovationReport();
        this.decorateWithFlowers();
    }
}
package com.csdn;
/**
 * 用空气净化器做装饰
 * @author Administrator
 *
 */
public class AirCleanerDecorator extends Decorator{
    public AirCleanerDecorator(RoughHouse _roughHouse) {
        super(_roughHouse);
    }
    //用空气净化器做装饰
    public void decorateWithAirCleaner(){
        System.out.println("屋子里放上空气净化器");
    }
    @Override
    public void RenovationReport() {
        super.RenovationReport();
        this.decorateWithAirCleaner();
    }
}
package com.csdn;
/**
 * 模拟户主收房签字
 * @author Administrator
 *
 */
public class HouseholderSignClient {
    public static void main(String[] args) {
        //毛期房
        RoughHouse house;
        //基础装修完成
        house = new WellAppointedHouse();
        //摆上花
        house = new FlowerDecorator(house);
        //放好空气净化器
        house = new AirCleanerDecorator(house);
        //呈现给户主
        house.RenovationReport();
        //户主满意的签字
        house.sign("小明");
    }
}

输出:

房间的基础装修都已经完成
屋子里摆上花
屋子里放上空气净化器
户主签名:小明

注:
a:本示例模拟了房子的装修过程,毛坯房在基本装修完成后,装修队觉得这样直接交给户主不好看,所以用花和空气净化器给房子做了装饰,户主接收基础装修完成并装饰过后的房子,非常开心的签了字。
b:装饰模式包含了抽象构件、具体构件、装饰角色、具体装饰角色
c:装饰模式可以理解为一种特殊的代理模式

2.通用装饰模式模板代码实现:

package com.csdn;
/**
 * 抽象构件
 * @author Administrator
 *
 */
public abstract class Component {
    public abstract void operate();
}
package com.csdn;
/**
 * 具体构件
 * @author Administrator
 *
 */
public class ConcreteComponent extends Component{
    //具体实现
    @Override
    public void operate() {
        System.out.println("do something");
    }
}
package com.csdn;
/**
 * 抽象装饰者
 * @author Administrator
 *
 */
public abstract class Decorator extends Component {
    private Component component = null;
    //通过构造函数传递被修饰者
    public Decorator(Component _component) {
        this.component = _component;
    }
    //委托给被修饰者执行
    @Override
    public void operate() {
        this.component.operate();
    }
}
package com.csdn;
/**
 * 具体装饰类1
 * @author Administrator
 *
 */
public class ConcreteDecorator1 extends Decorator{
    //定义被修饰者
    public ConcreteDecorator1(Component _component) {
        super(_component);
    }
    //定义自己的修饰方法
    private void method1(){
        System.out.println("method1修饰");
    }
    //重写父类的方法
    @Override
    public void operate() {
        this.method1();
        super.operate();
    }
}
package com.csdn;
/**
 * 具体装饰类2
 * @author Administrator
 *
 */
public class ConcreteDecorator2 extends Decorator{
    //定义被修饰者
    public ConcreteDecorator2(Component _component) {
        super(_component);
    }
    //定义自己的修饰方法
    private void method2(){
        System.out.println("method2修饰");
    }
    //重写父类的方法
    @Override
    public void operate() {
        this.method2();
        super.operate();
    }
}
package com.csdn;
/**
 * 场景类
 * @author Administrator
 *
 */
public class Client {
    public static void main(String[] args) {
        Component component = new ConcreteComponent();
        //第一次修饰
        component = new ConcreteDecorator1(component);
        //第二次修饰
        component = new ConcreteDecorator2(component);
        //修饰后运行
        component.operate();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值