您的位置:首页 > 其它

设计模式——装饰模式

2017-02-27 12:18 260 查看
装饰的意思想必大家也都知道,就是起修饰美化作用的物品,比如装修效果图造型的轮廓和雕刻装饰;在身体或物体的表面加些附属的东西,使之更美观。在应用程序里面装饰模式也是很有用武之地的。装饰模式的职责:动态的为一个对象添加新的功能。装饰模式是一种用于代替继承的技术,没有必要通过继承增加子类就能扩展对象的新功能。使用对象的关联关系代替继承关系,更加灵活,同时避免类型体系的快速膨胀。

装饰模式的适用性:

1.在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。

2.处理那些可以撤销的职责。

3.当不能采用生成子类的方法进行扩充时。

比如说我们的手机,以前最新出来的手机也就是打电话功能,在发展的过程中逐渐增加了短信、视频、音乐、聊天等功能。



下面我们就以手机的功能来实现一下功能

public interface MobilePhone {
String function ();
}
class TelephoneMobilePhone implements MobilePhone{

@Override
public String function() {
return "电话功能";
}

}
/**
* 具体的手机对象
*/
class ConcreteMobilePhone implements MobilePhone{
private MobilePhone mobilePhone;
public ConcreteMobilePhone(MobilePhone mobilePhone) {
super();
this.mobilePhone = mobilePhone;
}

@Override
public String function() {
return mobilePhone.function();
}

}
/**
* 增加短信功能
*/
class MessageMobilePhone extends ConcreteMobilePhone{
public MessageMobilePhone(MobilePhone mobilePhone) {
super(mobilePhone);
}

@Override
public String function() {
System.out.println(message());
return super.function();
}
public String message() {
return "发短信功能";
}
}
/**
* 增加音乐功能
*/
class MusicMobilePhone extends ConcreteMobilePhone{

public MusicMobilePhone(MobilePhone mobilePhone) {
super(mobilePhone);
}

@Override
public String function() {
System.out.println(music());
return super.function();
}
public String music() {
return "音乐功能";
}

}
/**
* 增加视频功能
*/
class VideoMobilePhone extends ConcreteMobilePhone{

public VideoMobilePhone(MobilePhone mobilePhone) {
super(mobilePhone);
}

@Override
public String function() {
System.out.println(playVideo());
return super.function();
}
public String playVideo(){
return "视频功能";
}
}
/**
* 增加聊天功能
*/
class ChatMobilePhone extends ConcreteMobilePhone{

public ChatMobilePhone(MobilePhone mobilePhone) {
super(mobilePhone);
}

@Override
public String function() {
System.out.println(chat());
return super.function();
}
public String chat() {
return "聊天功能";
}

}


测试代码

TelephoneMobilePhone tPhone = new TelephoneMobilePhone();
System.out.println(tPhone.function());
System.out.println("《《-------新增功能--------》》");

MessageMobilePhone mPhone = new MessageMobilePhone(tPhone);
System.out.println(mPhone.function());
System.out.println("《《-------新增功能--------》》");

MusicMobilePhone musicMobilePhone = new MusicMobilePhone(tPhone);
System.out.println(musicMobilePhone.function());
System.out.println("《《-------新增功能--------》》");

VideoMobilePhone vPhone = new VideoMobilePhone(tPhone);
System.out.println(vPhone.function());
System.out.println("《《-------新增功能--------》》");

ChatMobilePhone cPhone = new ChatMobilePhone(tPhone);
System.out.println(cPhone.function());


实现效果



将测试代码里面的参数稍微的切换一些试试看,会有什么样的效果

TelephoneMobilePhone tPhone = new TelephoneMobilePhone();
System.out.println(tPhone.function());
System.out.println("《《-------新增功能--------》》");

MessageMobilePhone mPhone = new MessageMobilePhone(tPhone);
System.out.println(mPhone.function());
System.out.println("《《-------新增功能--------》》");

MusicMobilePhone musicMobilePhone = new MusicMobilePhone(mPhone);
System.out.println(musicMobilePhone.function());
System.out.println("《《-------新增功能--------》》");

VideoMobilePhone vPhone = new VideoMobilePhone(musicMobilePhone);
System.out.println(vPhone.function());
System.out.println("《《-------新增功能--------》》");

ChatMobilePhone cPhone = new ChatMobilePhone(vPhone);
System.out.println(cPhone.function());


运行效果图



其实我们的测试代码还可以再次稍微的切换一下

TelephoneMobilePhone tPhone = new TelephoneMobilePhone();
System.out.println(tPhone.function());
System.out.println("《《-------新增功能--------》》");

ChatMobilePhone cPhone = new ChatMobilePhone(new MessageMobilePhone(new MusicMobilePhone(new VideoMobilePhone(tPhone))));
System.out.println(cPhone.function());


运行效果图



通过上面的案例你就会发现我们新增加的每一个功能都是动态的透明的添加的,最后一次的测试代码的样式是不是很熟悉,我们平常使用的IO中输入流和输出流就是这样的设计。到这里装饰功能也就基本结束了,谢谢浏览,如有疑问欢迎留言。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息