JAVA设计模式之【装饰者模式】
JAVA设计模式之【装饰者模式】
装饰模式 对新房进行装修并没有改变房屋的本质,但它可以让房子变得更漂亮、更温馨、更实用。
在软件设计中,对已有对象(新房)的功能进行扩展(装修)。
把通用功能封装在装饰器中,用到的地方进行调用。
装饰模式是一种用于替代继承的技术,使用对象之间的关联关系取代类之间的继承关系。引入装饰类,扩充新功能。
角色
抽象构件
具体构件
抽象装饰类
具体装饰类
案例一,窗体装饰
1.组件类
package Decorator; // 装饰者模式
/**
* Created by Jiqing on 2016/10/13.
*/
abstract class Component {
public abstract void display();
}
2.组件装饰者
package Decorator;
/**
* Created by Jiqing on 2016/10/13.
*/
public class ComponentDecorator extends Component{
private Component component; // 维持对抽象构件类型对象的引用
public ComponentDecorator(Component component){
this.component = component;
}
public void display() {
component.display();
}
}
3.继承类ListBox
package Decorator;
/**
* Created by Jiqing on 2016/10/13.
*/
public class ListBox extends Component{
public void display() {
System.out.println("显示列表框!");
}
}
4.继承类TextBox
package Decorator;
/**
* Created by Jiqing on 2016/10/13.
*/
public class TextBox extends Component{
public void display() {
System.out.println("显示文本框!");
}
}
5.继承类Window
package Decorator;
/**
* Created by Jiqing on 2016/10/13.
*/
public class Window extends Component{
public void display() {
System.out.println("显示窗体!");
}
}
6.黑框装饰者
package Decorator;
/**
* Created by Jiqing on 2016/10/14.
*/
public class BlackBoarderDecorator extends ComponentDecorator{
public BlackBoarderDecorator(Component component) {
super(component);
}
public void display() {
this.setBlackBoarder();
super.display();
}
public void setBlackBoarder() {
System.out.println("为构件增加黑色边框!");
}
}
7.滚动条装饰者
package Decorator;
/**
* Created by Jiqing on 2016/10/14.
*/
public class ScrollBarDecorator extends ComponentDecorator{
public ScrollBarDecorator (Component component) {
super(component); // 调用父类构造函数
}
public void display() {
this.setScrollBar();
super.display();
}
public void setScrollBar() {
System.out.println("为构件增加滚动条!");
}
}
8.客户端调用
package Decorator; // 装饰者模式
/**
* Created by Jiqing on 2016/10/14.
*/
public class Client {
public static void main(String args[]) {
Component component,componentSB,componentBB;
component = new Window();
componentSB = new ScrollBarDecorator(component);
componentSB.display();
System.out.println("--------------------");
componentBB = new BlackBoarderDecorator(componentSB);
componentBB.display();
}
}
执行结果
为构件增加滚动条!
显示窗体!
--------------------
为构件增加黑色边框!
为构件增加滚动条!
显示窗体!
案例二,密文装饰
1.密文接口
package Decorator.sample2;
/**
* Created by Jiqing on 2016/10/14.
*/
public interface Cipher // 密文接口
{
public String encrypt(String plainText);
}
2.密文装饰者
package Decorator.sample2;
/**
* Created by Jiqing on 2016/10/14.
*/
public class CipherDecorator implements Cipher{
private Cipher cipher;
public CipherDecorator(Cipher cipher) {
this.cipher = cipher;
}
public String encrypt(String plainText) {
return cipher.encrypt(plainText);
}
}
3.密文接口实现类
package Decorator.sample2;
/**
* Created by Jiqing on 2016/10/14.
*/
public final class SimpleCipher implements Cipher // 简单密文继承密文
{
public String encrypt(String plainText)
{
String str="";
for(int i=0;i<plainText.length();i++)
{
char c=plainText.charAt(i);
if(c>='a'&&c<='z')
{
c+=6;
if(c>'z') c-=26;
if(c<'a') c+=26;
}
if(c>='A'&&c<='Z')
{
c+=6;
if(c>'Z') c-=26;
if(c<'A') c+=26;
}
str+=c;
}
return str;
}
}
4.复杂加密装饰者
package Decorator.sample2;
/**
* Created by Jiqing on 2016/10/14.
*/
public class ComplexCipher extends CipherDecorator // 复杂密文
{
public ComplexCipher(Cipher cipher)
{
super(cipher);
}
public String encrypt(String plainText)
{
String result=super.encrypt(plainText);
result= this.reverse(result);
return result;
}
public String reverse(String text)
{
String str="";
for(int i=text.length();i>0;i--)
{
str+=text.substring(i-1,i);
}
return str;
}
}
5.先进加密装饰者
package Decorator.sample2;
/**
* Created by Jiqing on 2016/10/14.
*/
public class AdvancedCipher extends CipherDecorator{
public AdvancedCipher(Cipher cipher) {
super(cipher);
}
public String encrypt(String plainText) { // 加密处理
String result=super.encrypt(plainText);
result=mod(result);
return result;
}
public String mod(String text)
{
String str="";
for(int i=0;i<text.length();i++)
{
String c=String.valueOf(text.charAt(i)%6);
str+=c;
}
return str;
}
}
6.客户端
package Decorator.sample2;
/**
* Created by Jiqing on 2016/10/14.
*/
public class Client {
public static void main(String args[])
{
String password="Jiqing9006"; //明文
String cpassword; //密文
Cipher sc,ac,cc;
sc=new SimpleCipher();
cpassword=sc.encrypt(password);
System.out.println(cpassword);
System.out.println("---------------------");
cc=new ComplexCipher(sc);
cpassword=cc.encrypt(password);
System.out.println(cpassword);
System.out.println("---------------------");
ac=new AdvancedCipher(cc);
cpassword=ac.encrypt(password);
System.out.println(cpassword);
System.out.println("---------------------");
}
}
执行结果
Powotm9006
---------------------
6009mtowoP
---------------------
0003123532
---------------------
JAVA设计模式之【装饰者模式】的更多相关文章
- Java 设计模式泛谈&装饰者模式和单例模式
设计模式(Design Pattern) 1.是一套被反复使用.多人知晓的,经过分类编目 的 代码设计经验总结.使用设计模式是为了可重用代码,让代码更容易维护以及扩展. 2.简单的讲:所谓模式就是得到 ...
- Java设计模式 - - 单例模式 装饰者模式
Java设计模式 单例模式 装饰者模式 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 静态代理模式:https://www.cnblogs.com/StanleyBlogs/p/1 ...
- Java设计模式系列-装饰器模式
原创文章,转载请标注出处:<Java设计模式系列-装饰器模式> 一.概述 装饰器模式作用是针对目标方法进行增强,提供新的功能或者额外的功能. 不同于适配器模式和桥接模式,装饰器模式涉及的是 ...
- 23种java设计模式之装饰者模式及动态代理
设计模式不管对于何种语言都是存在的,这里介绍的是java的模式 装饰者模式是在二次开发中应用比较多的一款模式,当然了用反射也是可以实现的,今天介绍的是装饰模式,有兴趣的朋友可以自己去了解一下反射是怎么 ...
- java设计模式—Decorator装饰者模式
一.装饰者模式 1.定义及作用 该模式以对客户端透明的方式扩展对象的功能. 2.涉及角色 抽象构件角色:定义一个抽象接口,来规范准备附加功能的类. 具体构件角色:将要被附加功能的类,实现抽象 ...
- Java设计模式之装饰者模式
要实现装饰者模式,注意一下几点内容: 1.装饰者类要实现真实类同样的接口 2.装饰者类内有一个真实对象的引用(可以通过装饰者类的构造器传入) 3.装饰类对象在主类中接受请求,将请求发送给真实的对象(相 ...
- java设计模式之七装饰器模式(Decorator)
顾名思义,装饰模式就是给一个对象增加一些新的功能,而且是动态的,要求装饰对象和被装饰对象实现同一个接口,装饰对象持有被装饰对象的实例,关系图如下: Source类是被装饰类,Decorator类是一个 ...
- java设计模式之 装饰器模式
装饰器模式 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构. 这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装 ...
- Java设计模式之装饰器模式
1.装饰器模式的定义(保持接口,扩展功能) Decorate装饰器,顾名思义,就是动态的给一个对象添加一些额外的职责,就好比对房子进行装修一样. 2.装饰器模式的特征 具有一个装饰对象. 必须拥有与被 ...
- java设计模式之装饰者模式学习
装饰者模式 Decorator模式(别名Wrapper):动态将职责附加到对象上,若要扩展功能,装饰者提供了比继承更具弹性的代替方案. 装饰者与被装饰者拥有共同的超类,继承的目的是继承类型,而不是行为 ...
随机推荐
- Reactor-反应器模式
Reactor模式:反应器模式,是高性能网络服务器中最为常用的一种模式,libevent,muduo,libuv等网络库都是以 Reactor模式构建.Reactor模式由同步事件多路分解器和具体事件 ...
- Vue01 Vue介绍、Vue使用、Vue实例的创建、数据绑定、Vue实例的生命周期、差值与表达式、指令与事件、语法糖
1 Vue介绍 1.1 官方介绍 vue是一个简单小巧的渐进式的技术栈,它提供了Web开发中常用的高级功能:视图和数据的解耦.组件的服用.路由.状态管理.虚拟DOM 说明:简单小巧 -> 压缩后 ...
- Struts2的数据封装
在很多的实际开发场景中,页面提交请求参数Action ,在Action中接收参数并对接收的数据进行封装.封装到一个JavaBean中,将JavaBean传递给业务层中.Struts2数据封装分为两类: ...
- CSS3 Tranform 3D 的应用
CSS3 Tranform 3D 的应用 一.perspective 属性 1. 作用: 设置元素被查看位置的视图,类似于眼睛到屏幕的距离,一般跟 perspective-origin 共同作用在一个 ...
- ASP.NET CSS 小结
1.ASP.NET 引用CSS 1.Site.master里面设置webopt <webopt:bundlereferencerunat="server"path=" ...
- 用Mirror,搞定用户画像
Mirror产品概述 Mirror是专为金融行业设计的全面用户画像管理系统.该系统基于星环多年来为多个金融企业客户构建用户画像的经验,深入契合业务需求,实现对用户全方位全维度的刻画.Mirror内置银 ...
- 【ASP.NET Core】根据 Content-Type 头部来筛选 Action
在开始今天的吹牛节目之前,老周先说个破事:每周的周五或者周六,老周会在新浪直播平台(一直播同步)开播 ASP.NET Core 相关的内容.具体的直播时间老周会在微博上发布.直播是免费观看的,当然了, ...
- 【BZOJ2693】jzptab(莫比乌斯反演)
[BZOJ2693]jzptab(莫比乌斯反演) 题面 讨厌权限题,只能跑到别的OJ上交 和这题是一样的 多组数据 求\[\sum_{i=1}^n\sum_{j=1}^mlcm(i,j)\] 题解 前 ...
- 【BZOJ3993】星际战争(网络流,二分答案)
[BZOJ3993]星际战争(网络流,二分答案) 题面 Description 3333年,在银河系的某星球上,X军团和Y军团正在激烈地作战.在战斗的某一阶段,Y军团一共派遣了N个巨型机器人进攻X军团 ...
- Get the Job You Want(大学英语综合教程4课文)
UNIT3-1 Harvey Mackay, who runs his own company, often interviews applicants for jobs. Here he lets ...