博客
关于我
设计模式-装饰模式Decorator JAVA示例
阅读量:676 次
发布时间:2019-03-16

本文共 2661 字,大约阅读时间需要 8 分钟。

声明:本博客里面设计模式相关的文章,均是学习 《大话设计模式》 的笔记。

装饰模式Decorator,动态的给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更灵活。下面是装饰模式的结构图:

基本的代码实现:

abstract class Component{

public abstract void operation();

}

class ConcreteComponent : Component{

public override void operation(){

具体对象的操作

}

}

abstract class Decorator : Component{

protected Component mComponent;

public void setComponent(Component component){

mComponent = component;

}

public override void operation(){

if(mComponent !=null){

mComponent.operation();

}

}

}

class ConcreteDecoratorA : Decorator{

private String addedState;本类的独有功能,区别与ConcreteDecoratorB

public override void operation(){

base.operation();

addedState = "New State";

具体装饰对象A的操作

}

}

class ConcreteDecoratorB :Decorator{

public override void operation(){

具体装饰对象B的操作

}

private void addeBehavior(){

}

}

客户端代码:

public static void main(String[] args){

ConcreteComponent mConcreteComponent = new ConcreteComponent();

ConcreteDecoratorA mDecoratorA = new ConcreteDecoratorA();

ConcreteDecoratorB mDecoratorB = new ConcreteDecoratorB();

mDecoratorA .setComponent(mConcreteComponent );

mDecoratorB .setComponent(mDecoratorA );

mDecoratorB .operation();

}

装饰模式是利用setComponent来对对象进行包装的,这样每个装饰对象的实现,就和如何使用这个对象分开了,每个装饰对象只关心自己的功能,不需要关心如何添加到对象链当中。

下面以装饰模式来模拟给人穿衣服的功能的具体代码:

package component.decorator;

public class Person {
private String name=null;
public Person(){
}
public Person(String name){
this.name = name;
}
public void show(){
System.out.println("dress up :"+name);
}
}

服饰类

public class Finery extends Person{

protected Person mPerson =null;
public void decorator(Person person){
this.mPerson = person;
}
@Override
public void show() {
if(mPerson != null){
mPerson.show();
}
}
}

具体服饰类

public class Tshirts extends Finery {

@Override
public void show() {
super.show();
System.out.println("dress Tshirts !");
}
}

public class BigTrouser extends Finery{

@Override
public void show(){
super.show();
System.out.println("dress big trouser!");
}
}

public class DuckHat extends Finery{

@Override
public void show(){
super.show();
System.out.println("dress Duck hat!");
}
}

客户端类

public class DressUpBaby {

public static void main(String[] args){
Person mPerson = new Person("helloBaby");
Tshirts mTshirts = new Tshirts();
BigTrouser mBigtrouser = new BigTrouser();
DuckHat mDuckHat = new DuckHat();
mTshirts.decorator(mPerson);
mBigtrouser.decorator(mTshirts);
mDuckHat.decorator(mBigtrouser);
mDuckHat.show();这里实际上是通过super.show()做的一个递归调用!
}
}

输出结果:

dress up :helloBaby

dress Tshirts !

dress big trouser!
dress Duck hat!

装饰模式使为已有功能动态的添加更多功能的一种方式。它把每个要装饰的功能放在单独的类中,并让这个类包装它所要装饰的对象,客户端代码可以在运行时根据需要有选择的,按顺序的使用装饰功能包装对象。

优点就是把类中的装饰功能从类中搬移出去,这样可以简化原有的类。有效的把类的核心职责和装饰功能区分开了,而且去除了相关类中重复的装饰逻辑。

你可能感兴趣的文章
nanoGPT 教程:从零开始训练语言模型
查看>>
NASA网站曝严重漏洞,或将沦为黑客钓鱼网站?
查看>>
Nash:轻量级、安全且可靠的脚本语言
查看>>
NAS、SAN和DAS的区别
查看>>
NAS个人云存储服务器搭建
查看>>
NAS服务器有哪些优势
查看>>
NAT PAT故障排除实战指南:从原理到技巧的深度探索
查看>>
nat 网卡间数据包转发_你是不是从来没有了解过光纤网卡,它跟普通网卡有什么区别?...
查看>>
NAT-DDNS内网穿透技术,快解析DDNS的优势
查看>>
NAT-DDNS内网穿透技术,快解析DDNS的优势
查看>>
NAT-DDNS内网穿透技术,解决动态域名解析难题
查看>>
natapp搭建外网服务器
查看>>
NativePHP:使用PHP构建跨平台桌面应用的新框架
查看>>
nativescript(angular2)——ListView组件
查看>>
NativeWindow_01
查看>>
Native方式运行Fabric(非Docker方式)
查看>>
Nature | 电子学“超构器件”, 从零基础到精通,收藏这篇就够了!
查看>>
Nature和Science同时报道,新疆出土四千年前遗骸完成DNA测序,证实并非移民而是土著...
查看>>
Nature封面:只低一毫米,时间也会变慢!叶军团队首次在毫米尺度验证广义相对论...
查看>>
Nat、端口映射、内网穿透有什么区别?
查看>>