装饰模式 :

对新房进行装修并没有改变房屋的本质,但它可以让房子变得更漂亮、更温馨、更实用。    在软件设计中,对已有对象(新房)的功能进行扩展(装修)。    把通用功能封装在装饰器中,用到的地方进行调用。

装饰模式是一种用于替代继承的技术,使用对象之间的关联关系取代类之间的继承关系。引入装饰类,扩充新功能。    角色    、抽象构件   、 具体构件   、 抽象装饰类  、  具体装饰类

案例一,窗体装饰

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;
}
}

5.并发和并行

并发:多个线程 更具CPU的调度情况 执行

并行:多个线程 同时执行

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 的设计模式之一装饰者模式

    刚开始接触装饰者的设计模式,感觉挺难理解的,不够后来花了一个晚上的时间,终于有头绪了 装饰者设计模式:如果想对已经存在的对象进行装饰,那么就定义一个类,在类中对已经有的对象进行功能的增强或添加另外的行 ...

  2. JAVA基础——设计模式之简单工厂模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述简单工厂模式的:简单工厂模式是类的创建模式,又叫做静态工厂方法(Static Factory Method)模式.简单工厂模式是由一个工厂 ...

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

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

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

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

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

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

  6. 设计模式之装饰者模式-java实例

    设计模式之装饰者模式 需求场景 我们有了别人提供的产品,但是别人提供的产品对我们来说还不够完善,我们需要对这个产品的功能进行补强,此时可以考虑使用装饰者模式. 我们已经有了产品,而且这个产品的功能非常 ...

  7. 实践GoF的23种设计模式:装饰者模式

    摘要:装饰者模式通过组合的方式,提供了能够动态地给对象/模块扩展新功能的能力.理论上,只要没有限制,它可以一直把功能叠加下去,具有很高的灵活性. 本文分享自华为云社区<[Go实现]实践GoF的2 ...

  8. Java IO流以及装饰器模式在其上的运用

    流概述 Java中,流是一种有序的字节序列,可以有任意的长度.从应用流向目的地称为输出流,从目的地流向应用称为输入流. Java的流族谱 Java的 java.io 包中囊括了整个流的家族,输出流和输 ...

  9. [转载]Java中继承、装饰者模式和代理模式的区别

    [转载]Java中继承.装饰者模式和代理模式的区别 这是我在学Java Web时穿插学习Java设计模式的笔记 我就不转载原文了,直接指路好了: 装饰者模式和继承的区别: https://blog.c ...

随机推荐

  1. python requests 调用restful api

    #!/usr/bin/python# -*- coding: utf-8 -*- import jsonimport requestsfrom urlparse import urljoin BASE ...

  2. bzoj4806 炮——DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4806 看到这题首先会想到状压什么乱七八糟的,然而很难做: 其实,因为求的是方案数,所以并不需 ...

  3. session关联接口

    #coding:utf-8 import requests,json,re ''' session关联接口,第一步访问登录接口,headers中要有cookie,不然会登录失败,登录成功后,添加随笔保 ...

  4. 关于使用jxl去读写Excel文件

    1.引入maven依赖 <dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifact ...

  5. Ruby模块与类

    Ruby是单一继承,一个类只能有一个直接的母类 Mix-in 模式来处理复合需求 多重包含: 由下往上回溯,重复的只看最上方的      换句话说,程序由上往下执行,由具体到抽象,第二次发现同一个模块 ...

  6. bzoj 3196 Tyvj 1730 二逼平衡树【线段树 套 splay】

    四舍五入就是个暴力. 对于线段树的每个区间都开一棵按权值排序的splay 对于第二个操作,二分一下,每次查询mid的排名,复杂度 $ O(nlog(n)^{3}) $ 其余的操作都是$ O(nlog( ...

  7. bzoj 3675: [Apio2014]序列分割【斜率优化dp】

    首先看这个得分方式,容易发现就相当于分k段,每段的值和两两乘起来. 这样就很容易列出dp方程:设f[i][j]为到j分成分成i段,转移是 \[ f[i][j]=max { f[k][j]+s[k]*( ...

  8. hdu 5201 The Monkey King【容斥原理+组合数学】

    原来我一开始以为的\( O(n^2) \)是调和级数\( O(nlog_2n) \)的! 首先枚举猴王的桃子个数\( x \),然后使用容斥原理,枚举有至少\( k \)个不满足的条件,那么这\( k ...

  9. (五)Python面向对象编程

    根据廖雪峰老师的python教程写一些学习总结! 面向对象编程 面向对象编程--Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元, ...

  10. 《linux就该这么学》学习笔记

    本篇文章是根据刘遄老师的<linux就该这么学>中个人易忘知识点的读书笔记,结合的是个人弱点,可能不适合广大的网友同学,并在此声明本篇文章只是用于学习之用,绝无侵犯版权之意 linux就该 ...