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设计模式之【装饰者模式】的更多相关文章

  1. Java 设计模式泛谈&装饰者模式和单例模式

    设计模式(Design Pattern) 1.是一套被反复使用.多人知晓的,经过分类编目 的 代码设计经验总结.使用设计模式是为了可重用代码,让代码更容易维护以及扩展. 2.简单的讲:所谓模式就是得到 ...

  2. Java设计模式 - - 单例模式 装饰者模式

    Java设计模式 单例模式 装饰者模式 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 静态代理模式:https://www.cnblogs.com/StanleyBlogs/p/1 ...

  3. Java设计模式系列-装饰器模式

    原创文章,转载请标注出处:<Java设计模式系列-装饰器模式> 一.概述 装饰器模式作用是针对目标方法进行增强,提供新的功能或者额外的功能. 不同于适配器模式和桥接模式,装饰器模式涉及的是 ...

  4. 23种java设计模式之装饰者模式及动态代理

    设计模式不管对于何种语言都是存在的,这里介绍的是java的模式 装饰者模式是在二次开发中应用比较多的一款模式,当然了用反射也是可以实现的,今天介绍的是装饰模式,有兴趣的朋友可以自己去了解一下反射是怎么 ...

  5. java设计模式—Decorator装饰者模式

    一.装饰者模式 1.定义及作用 该模式以对客户端透明的方式扩展对象的功能. 2.涉及角色      抽象构件角色:定义一个抽象接口,来规范准备附加功能的类. 具体构件角色:将要被附加功能的类,实现抽象 ...

  6. Java设计模式之装饰者模式

    要实现装饰者模式,注意一下几点内容: 1.装饰者类要实现真实类同样的接口 2.装饰者类内有一个真实对象的引用(可以通过装饰者类的构造器传入) 3.装饰类对象在主类中接受请求,将请求发送给真实的对象(相 ...

  7. java设计模式之七装饰器模式(Decorator)

    顾名思义,装饰模式就是给一个对象增加一些新的功能,而且是动态的,要求装饰对象和被装饰对象实现同一个接口,装饰对象持有被装饰对象的实例,关系图如下: Source类是被装饰类,Decorator类是一个 ...

  8. java设计模式之 装饰器模式

    装饰器模式 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构. 这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装 ...

  9. Java设计模式之装饰器模式

    1.装饰器模式的定义(保持接口,扩展功能) Decorate装饰器,顾名思义,就是动态的给一个对象添加一些额外的职责,就好比对房子进行装修一样. 2.装饰器模式的特征 具有一个装饰对象. 必须拥有与被 ...

  10. java设计模式之装饰者模式学习

    装饰者模式 Decorator模式(别名Wrapper):动态将职责附加到对象上,若要扩展功能,装饰者提供了比继承更具弹性的代替方案. 装饰者与被装饰者拥有共同的超类,继承的目的是继承类型,而不是行为 ...

随机推荐

  1. eclipse 修改默认的author

    1. 在eclipse.ini中添加 -vmargs -Duser.name={author name} 记得一定要在-vmargs之后,否则无效. 2. 设置eclipse参数 windows--& ...

  2. CF368 E - Garlands

    主席树 其实暴力二维树状还更快 #include<bits/stdc++.h> using namespace std; typedef long long ll; const int M ...

  3. Visual Studio2010重新安装后,冲突问题

    http://www.docin.com/p-685665064.html VS2010重装后出错解决办法 一般在卸载VS2010之后,也重新安装了一遍,有的时候可能会出现如下问题: 1. 未能正确加 ...

  4. C# 图解教程 第三章 类型、存储和变量

    类型.存储和变量 C#程序是一组类型声明类型是一种模板实例化类型数据成员和函数成员预定义类类型用户定义类型栈和堆 栈堆 值类型和引用类型 存储引用类型对象的成员C#类型的分类 变量静态类型和dynam ...

  5. position:absolute的发现。

    使用.box{ width:100px; height:100px; background:red; position:absolute; left:0; right:0; top:0; bottom ...

  6. 【SPOJ】Substrings(后缀自动机)

    [SPOJ]Substrings(后缀自动机) 题面 Vjudge 题意:给定一个长度为\(len\)的串,求出长度为1~len的子串中,出现最多的出现了多少次 题解 出现次数很好处理,就是\(rig ...

  7. [BZOJ3110][ZJOI2013]K大数查询(整体二分)

    BZOJ Luogu sol 整体二分,其实很简单的啦. 对所有询问二分一个答案mid,把所有修改操作中数字大于mid的做一个区间覆盖(区间加1) 查询就是区间查询 然后左右分一分即可 注意是第k大 ...

  8. 清橙A1212:剪枝

    题面 清橙 Sol 一种新的树上\(DP\)姿势 从左往右按链\(DP\) 做法: 维护两个栈\(S1\),\(S2\) \(S1\)存当前的链 \(S2\)存分叉点以下要改的链 \(Dfs\),弄一 ...

  9. UVA10692:Huge Mods

    题面 传送门 题意 输入正整数a1,a2,a3..an和模m,求a1^a2^...^an mod m Sol 首先有\[ a^b\equiv \begin{cases} a^{b\%\phi(p)}~ ...

  10. [BZOJ3207] 花神的嘲讽计划Ⅰ (主席树)

    Description 背景 花神是神,一大癖好就是嘲讽大J,举例如下: “哎你傻不傻的![hqz:大笨J]” “这道题又被J屎过了!!” “J这程序怎么跑这么快!J要逆袭了!” …… 描述 这一天D ...