java设计模式,装修模式

来源:互联网 发布:傻妹妹雾化器完美数据 编辑:程序博客网 时间:2024/04/28 15:23
package com.dasenlin.decoration;/** * 装修模式 * @author Administrator * 1、需要扩展一个类的功能。 * 2、动态的为一个对象增加功能,而且还能动态撤销。(继承不能做到这一点,继承的功能是静态的,不能动态增删。) * 缺点:产生过多相似的对象,不易排错! */public interface Sourceable {    public void method1();}/** * 此类为我们要装修的类 * @author Administrator * */class Source implements Sourceable{    @Override    public void method1() {        System.out.println("装修前");    }}/** * 此类为装修类 * @author Administrator * */class Decorator implements Sourceable{    private Sourceable source;    public Decorator(Sourceable source) {        super();        this.source = source;    }    @Override    public void method1() {        source.method1();        System.out.println("这就是装修后的方法,有属性可以在这里增加");    }} package com.dasenlin.decoration;/** * 调试方法 * @author Administrator * */public class Test {    public static void main(String[] args) {        Sourceable so=new Source();        Decorator de =new Decorator(so);                  de.method1();    }}
0 0