quanke / design-pattern-java Goto Github PK
View Code? Open in Web Editor NEW设计模式 Java版本
Home Page: got.quanke.name
设计模式 Java版本
Home Page: got.quanke.name
半透明装饰模式可以给系统带来更多的灵活性,设计相对简单,使用起来也非常方便;但是其最大的缺点在于不能实现对同一个对象的多次装饰,而且客户端需要有区别地对待装饰之前的对象和装饰之后的对象。在实现半透明的装饰模式时,我们只需在具体装饰类中增加一个独立的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的实现方式好像并不是很常用到。
http://www.jianshu.com/p/d263d6972870
上面的链接中,也介绍的是Builder模式,但实现方式似乎完全不一样。平时使用的Builder的典型使用方式是类似以下这种:
HttpClient httpClient = new StdHttpClient.Builder()
.host("mychouchdbhost")
.port(4455)
.build();
图片地址失效了
您好,
这里面getIgoChessman函数为什么用static? 如果使用instance 应该调用非静态类比较好。 我在java 8 里面instance 无法调用静态方法。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.