Code Monkey home page Code Monkey logo

design-pattern-java's Issues

装饰模式的疑问

半透明装饰模式可以给系统带来更多的灵活性,设计相对简单,使用起来也非常方便;但是其最大的缺点在于不能实现对同一个对象的多次装饰,而且客户端需要有区别地对待装饰之前的对象和装饰之后的对象。在实现半透明的装饰模式时,我们只需在具体装饰类中增加一个独立的addedBehavior()方法来封装相应的业务处理,由于客户端使用具体装饰类型来定义装饰后的对象,因此可以单独调用addedBehavior()方法来扩展系统功能。
为什么半透明装饰模式不能实现对同一个对象的多次装饰????
对上面这句话问句不是很理解!!!
我的测试代码如下:

//Encryption.java
public abstract class Encryption {
    abstract String enc(String str);
}
//NormalEncrypt.java
public class NormalEncrypt extends Encryption {

    @Override
    String enc(String str) {
        System.out.println("move bit algorithm encryption");
        return str;
    }
}
//EncryptionDecorate.java
public class EncryptionDecorate extends Encryption {

    private Encryption encryption;

    public EncryptionDecorate(Encryption e) {
        this.encryption = e;
    }

    @Override
    String enc(String str) {
        encryption.enc(str);
        return str;
    }
}
//TwoEncryption.java
public class TwoEncryption extends EncryptionDecorate {

    public TwoEncryption(Encryption e) {
        super(e);
    }

    @Override
    String enc(String str) {
//        String s = super.enc(str);
//        return twoEnc(s);
        return super.enc(str);
    }

    private String twoEnc(String str) {
        System.out.println("twice encrypt");
        return str;
    }

    public void display(){
        System.out.println("show TwoEncryption");
    }
}
//ThreeEncryption.java
public class ThreeEncryption extends EncryptionDecorate {

    public ThreeEncryption(Encryption e) {
        super(e);
    }

    @Override
    String enc(String str) {
        String s = super.enc(str);
        return threeEnc(s);
    }

    private String threeEnc(String str) {
        System.out.println("third encrypt");
        return str;
    }
}

//App.java
public class App {

    public static void main(String[] args) {
        Encryption ne = new NormalEncrypt();
        TwoEncryption ed = new TwoEncryption(ne);
        //ed.enc("123");
        ed.display();
        Encryption et = new ThreeEncryption(ed);
        et.enc("123");
    }
}

以上代码打印的运行结果是 :
show TwoEncryption
move bit algorithm encryption
third encrypt

et对象还是依旧对ne原始对象进行装饰了的,请麻烦解惑下“半透明装饰模式不能实现对同一个对象的多次装饰”这句话,不胜感激!!!

关于Builder Pattern的疑问

文中介绍的Builder Pattern的实现方式好像并不是很常用到。

http://www.jianshu.com/p/d263d6972870

上面的链接中,也介绍的是Builder模式,但实现方式似乎完全不一样。平时使用的Builder的典型使用方式是类似以下这种:

HttpClient httpClient = new StdHttpClient.Builder()
                                .host("mychouchdbhost")
                                .port(4455)
                                .build();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.