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;
}
}
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基础——设计模式之装饰者模式的更多相关文章
- Java 的设计模式之一装饰者模式
刚开始接触装饰者的设计模式,感觉挺难理解的,不够后来花了一个晚上的时间,终于有头绪了 装饰者设计模式:如果想对已经存在的对象进行装饰,那么就定义一个类,在类中对已经有的对象进行功能的增强或添加另外的行 ...
- JAVA基础——设计模式之简单工厂模式
在阎宏博士的<JAVA与模式>一书中开头是这样描述简单工厂模式的:简单工厂模式是类的创建模式,又叫做静态工厂方法(Static Factory Method)模式.简单工厂模式是由一个工厂 ...
- Java 设计模式泛谈&装饰者模式和单例模式
设计模式(Design Pattern) 1.是一套被反复使用.多人知晓的,经过分类编目 的 代码设计经验总结.使用设计模式是为了可重用代码,让代码更容易维护以及扩展. 2.简单的讲:所谓模式就是得到 ...
- Java设计模式 - - 单例模式 装饰者模式
Java设计模式 单例模式 装饰者模式 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 静态代理模式:https://www.cnblogs.com/StanleyBlogs/p/1 ...
- Java设计模式之装饰器模式
1.装饰器模式的定义(保持接口,扩展功能) Decorate装饰器,顾名思义,就是动态的给一个对象添加一些额外的职责,就好比对房子进行装修一样. 2.装饰器模式的特征 具有一个装饰对象. 必须拥有与被 ...
- 设计模式之装饰者模式-java实例
设计模式之装饰者模式 需求场景 我们有了别人提供的产品,但是别人提供的产品对我们来说还不够完善,我们需要对这个产品的功能进行补强,此时可以考虑使用装饰者模式. 我们已经有了产品,而且这个产品的功能非常 ...
- 实践GoF的23种设计模式:装饰者模式
摘要:装饰者模式通过组合的方式,提供了能够动态地给对象/模块扩展新功能的能力.理论上,只要没有限制,它可以一直把功能叠加下去,具有很高的灵活性. 本文分享自华为云社区<[Go实现]实践GoF的2 ...
- Java IO流以及装饰器模式在其上的运用
流概述 Java中,流是一种有序的字节序列,可以有任意的长度.从应用流向目的地称为输出流,从目的地流向应用称为输入流. Java的流族谱 Java的 java.io 包中囊括了整个流的家族,输出流和输 ...
- [转载]Java中继承、装饰者模式和代理模式的区别
[转载]Java中继承.装饰者模式和代理模式的区别 这是我在学Java Web时穿插学习Java设计模式的笔记 我就不转载原文了,直接指路好了: 装饰者模式和继承的区别: https://blog.c ...
随机推荐
- bootstrap复选框和单选按钮
复选框和单选按钮标签包含在<Label>标签中<div class="checkbox"> <label><input type=&quo ...
- log4j的1.2.15版本,在pom.xml中的顶层project报错错误: Failure to transfer javax.jms:jms:jar:1.1 from https://maven-repository.dev.java.net/nonav/repository......
在动态网站工程中,添加了Pom依赖,当添加log4j的1.2.15版本依赖时,在pom.xml中的顶层project报错错误: Failure to transfer javax.jms:jms:ja ...
- bzoj 1597: [Usaco2008 Mar]土地购买【斜率优化】
按xy降序排序,把能被完全包含的去掉 然后就得到了x升序y降序的一个数组 然后方程就显然了:f[i]=min(f[j]+y[j+1]x[i]) 斜率优化转移 说起来我还不会斜率优化呢是不是该学一下了 ...
- bzoj 2115: [Wc2011] Xor【线性基+dfs】
-老是想到最长路上 其实可以这样:把每个环的xor和都存起来,然后任选一条1到n的路径的xor和ans,答案就是这个ans在环的线性基上跑贪心. 为什么是对的--因为可以重边而且是无相连通的,并且对于 ...
- 创建swagger的springboot-stater,并在spring cloud zuul网关中引入
Swagger 是一款RESTFUL接口的.基于YAML.JSON语言的文档在线自动生成.代码自动生成的工具. 通过在controller中添加注解,即可轻易实现代码文档化. Swagger提供ui界 ...
- Luogu P1330 封锁阳光大学【Dfs】 By cellur925
题目传送门 这道题我们很容易去想到二分图染色,但是这个题好像又不是一个严格的二分图. 开始的思路:dfs每个点,扫与他相邻的每个点,如果没访问,染相反颜色:如果访问过,进行检查,如果不可行,直接结束程 ...
- Luogu P1970 花匠 【线性Dp】 By cellur925
题目描述 Description 花匠栋栋种了一排花,每株花都有自己的高度.花儿越长越大,也越来越挤.栋栋决定把这排中的一部分花移走,将剩下的留在原地,使得剩下的花能有空间长大,同时,栋栋希望剩下的花 ...
- ROS学习笔记五:创建和使用ROS msg和srv
1 msg和srv简介 1.1 msg文件 msg文件就是一个简单的text文件,其中每行有一个类型和名称,可用的类型如下: int8, int16, int32, int64 (plus uint* ...
- Cenos7 切换单用户模式
CentOS 7在进入单用户的时候和6.x做了很多改变,下面让我们来看看如何进入单用户. 1.重启服务器,在选择内核界面使用上下箭头移动 2.选择内核并按“e” 3.修改参数 将rhgb quiet ...
- 转】Spark SQL 之 DataFrame
原博文出自于: http://www.cnblogs.com/BYRans/p/5003029.html 感谢! Spark SQL 之 DataFrame 转载请注明出处:http://www.cn ...

