针对问题:在两个集合中的对象期望做到任意组合,而这样的组合的可能性是太多了,在实现起来无疑会导致类爆炸。所以必须有一种方法在运行时动态的创建出我期望的类型,有这样的一种解决方案可以做到:让两个集合的对象(装饰者、被装饰者)实现同一接口,也就是说让装饰者、被装饰者属于同一类型,并且在装饰者中组合这一接口,这样装饰者就可以组合(装饰)任意的被装饰者,更重要的是装饰完成之后的类型又可以作为被装饰者来被装饰,然后就可以一直这样循环装饰下去,用这样类似递归的形式就可以在运行时动态的组合出任意我想要的类型了。
装饰者模式结构图:
装饰者模式实现代码:
/**
* 装饰、被装饰的抽象
* @author bruce
*
*/
public abstract class Component {
public void operation(){};
}
/**
* 装饰者抽象(区分主体)
* @author bruce
*
*/
public abstract class Decorator extends Component{
}
/**
* 主体(被装饰)
* @author bruce
*
*/
public class ConcreteComponent extends Component{
@Override
public void operation() {
// TODO Auto-generated method stub
System.out.println("主体");
}
}
/**
* 装饰者A
* @author bruce
*
*/
public class ConcreteDecoratorA extends Decorator{
/**
* 组合接口
*/
private Component component;
public ConcreteDecoratorA(Component component){
this.component=component;
}
@Override
public void operation() {
System.out.println("为主体装饰A功能");
component.operation();
}
}
/**
* 装饰者B
* @author bruce
*
*/
public class ConcreteDecoratorB extends Decorator{
/**
* 组合接口
*/
private Component component;
public ConcreteDecoratorB(Component component){
this.component=component;
}
@Override
public void operation() {
System.out.println("为主体装饰B功能");
component.operation();
}
}
/**
* 测试
* @author bruce
*
*/
public class Client {
public static void main(String[] args) {
Component component=new ConcreteComponent();//创建主体
Component decoratorA=new ConcreteDecoratorA(component);//创建装饰者A,并装饰主体
System.out.println("----------");
decoratorA.operation();
Component decoratorB=new ConcreteDecoratorB(component);//创建装饰者B,并装饰主体
System.out.println("----------");
decoratorB.operation();
decoratorB=new ConcreteDecoratorB(decoratorA);//创建装饰者B,并装饰decoratorA
System.out.println("----------");
decoratorB.operation();
/**
* output:
* ----------
为主体装饰A功能
主体
----------
为主体装饰B功能
主体
----------
为主体装饰B功能
为主体装饰A功能
主体
*/
}
}
- 大小: 120 KB
分享到:
相关推荐
命令模式-Command Pattern 请求发送者与接收者解耦——命令模式(一) 请求发送者与接收者解耦——命令模式(二) 请求发送者与接收者解耦——命令模式(三) 请求发送者与接收者解耦——命令模式(四) 请求发送者...
DesignPattern_BasicKnowledge设计模式之类关系:设计模式 类关系 基础 依赖 关联
协调多个对象之间的交互——中介者模式(三)20.3 完整解决方案为了协调界面组件对象之间的复杂交互关系,Sunny公司开发人员使用中介者模式来设计客户信息管理窗
23种设计模式(Design Pattern)的C++实现范例,包括下面列出的各种模式,代码包含较详细注释。另外附上“设计模式迷你手册.chm”供参考。 注:项目在 VS2008 下使用。 创建型: 抽象工厂模式(Abstract Factory) 生成...
DesignPattern 设计模式,共10章。内容详细,案例设计齐全。对设计可复用的面向对象的软件很有帮助,有助你写出又质量,并且可重复使用的高品质软件
本书已经成为面向对象技术人员的圣经和词典,书中定义的23个模式逐渐成为开发界技术交流所必备的基础知识和语汇。
C++ 23种设计模式(全_解释+源代码).zip 23种设计模式都是基于以下原则,务必记牢 依赖倒转原则 里氏代换原则 开闭原则 接口隔离原则 合成聚合复用原则 迪米特法则 23种设计模式如下:
装饰模式的核心在于抽象装饰类的设计,其典型代码如下所示://维持一个对抽象构件对象的引用public Decorator(Component component
JAVA design pattern-java设计模式.chm
Design Pattern: Simple Factory 模式 Design Pattern: Abstract Factory 模式 Design Pattern: Builder 模式 Design Pattern: Factory Method 模式 Design Pattern: Prototype 模式 Design Pattern: Singleton...
Design*Pattern*Framework*4.5
深入浅出设计模式 (Head First Design Pattern)_带书签 深入浅出设计模式 (Head First Design Pattern)_带书签
Mobile Design Pattern Gallery : UI Patterns for Mobile Applications原版 全彩 120M大容量《移动应用ui设计模式》是一本移动应用ui 设计模式参考书,分10 大类介绍了70 个移动应用设计模式(包括反模式),用400 ...
Theresa Neil's new book "Mobile Design Pattern Gallery" provides solutions to common design challenges. There is inspiration on every topic from designing your application's navigation to choosing ...
Design Pattern 简明手册,设计模式查阅简明手册
JAVA software design pattern
C语言设计模式 PDF《C Design Pattern》C语言设计模式 PDF《C Design Pattern》C语言设计模式 PDF《C Design Pattern》C语言设计模式 PDF《C Design Pattern》
GoF的经典著作设计模式(Design Pattern)
DesignPattern 新版設計模式(C#) 講述23種設計模式: ◆結構圖 ◆生活例子 ◆意圖 ◆適用性 ◆示例代碼 ◆實際應用