您现在的位置是:主页 > news > 太原做企业网站/seo手机关键词网址
太原做企业网站/seo手机关键词网址
admin2025/5/7 1:34:51【news】
简介太原做企业网站,seo手机关键词网址,想自己建一个公司网站怎么做,佛山网站商城建设JAVA设计模式之【装饰者模式】装饰模式 对新房进行装修并没有改变房屋的本质,但它可以让房子变得更漂亮、更温馨、更实用。 在软件设计中,对已有对象(新房)的功能进行扩展(装修)。 把通用功能封装在装饰器中,用到的地方进行调用。 装饰模式是…
JAVA设计模式之【装饰者模式】
装饰模式 对新房进行装修并没有改变房屋的本质,但它可以让房子变得更漂亮、更温馨、更实用。 在软件设计中,对已有对象(新房)的功能进行扩展(装修)。 把通用功能封装在装饰器中,用到的地方进行调用。 装饰模式是一种用于替代继承的技术,使用对象之间的关联关系取代类之间的继承关系。引入装饰类,扩充新功能。 角色 抽象构件 具体构件 抽象装饰类 具体装饰类
案例一,窗体装饰
1.组件类
package Decorator; // 装饰者模式
/*** Created by Jiqing on 2016/10/13.*/
abstract classComponent{
publicabstractvoiddisplay();
}
2.组件装饰者
package Decorator;
/*** Created by Jiqing on 2016/10/13.*/
public classComponentDecoratorextendsComponent{
private Component component; // 维持对抽象构件类型对象的引用
publicComponentDecorator(Component component){
this.component = component;
}
publicvoiddisplay(){
component.display();
}
}
3.继承类ListBox
package Decorator;
/*** Created by Jiqing on 2016/10/13.*/
public classListBoxextendsComponent{
publicvoiddisplay(){
System.out.println("显示列表框!");
}
}
4.继承类TextBox
package Decorator;
/*** Created by Jiqing on 2016/10/13.*/
public classTextBoxextendsComponent{
publicvoiddisplay(){
System.out.println("显示文本框!");
}
}
5.继承类Window
package Decorator;
/*** Created by Jiqing on 2016/10/13.*/
public classWindowextendsComponent{
publicvoiddisplay(){
System.out.println("显示窗体!");
}
}
6.黑框装饰者
package Decorator;
/*** Created by Jiqing on 2016/10/14.*/
public classBlackBoarderDecoratorextendsComponentDecorator{
publicBlackBoarderDecorator(Component component){
super(component);
}
publicvoiddisplay(){
this.setBlackBoarder();
super.display();
}
publicvoidsetBlackBoarder(){
System.out.println("为构件增加黑色边框!");
}
}
7.滚动条装饰者
package Decorator;
/*** Created by Jiqing on 2016/10/14.*/
public classScrollBarDecoratorextendsComponentDecorator{
publicScrollBarDecorator(Component component){
super(component); // 调用父类构造函数
}
publicvoiddisplay(){
this.setScrollBar();
super.display();
}
publicvoidsetScrollBar(){
System.out.println("为构件增加滚动条!");
}
}
8.客户端调用
package Decorator; // 装饰者模式
/*** Created by Jiqing on 2016/10/14.*/
public classClient{
publicstaticvoidmain(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 interfaceCipher// 密文接口{
publicStringencrypt(String plainText);
}
2.密文装饰者
package Decorator.sample2;
/*** Created by Jiqing on 2016/10/14.*/
public classCipherDecoratorimplementsCipher{
private Cipher cipher;
publicCipherDecorator(Cipher cipher){
this.cipher = cipher;
}
publicStringencrypt(String plainText){
return cipher.encrypt(plainText);
}
}
3.密文接口实现类
package Decorator.sample2;
/*** Created by Jiqing on 2016/10/14.*/
public final classSimpleCipherimplementsCipher// 简单密文继承密文{
publicStringencrypt(String plainText){
String str="";
for(int i=0;i
{
char c=plainText.charAt(i);
if(c>='a'&&c<='z')
{
c+=6;
if(c>'z') c-=26;
if(c
}
if(c>='A'&&c<='Z')
{
c+=6;
if(c>'Z') c-=26;
if(c
}
str+=c;
}
return str;
}
}
4.复杂加密装饰者
package Decorator.sample2;
/*** Created by Jiqing on 2016/10/14.*/
public classComplexCipherextendsCipherDecorator// 复杂密文{
publicComplexCipher(Cipher cipher){
super(cipher);
}
publicStringencrypt(String plainText){
String result=super.encrypt(plainText);
result= this.reverse(result);
return result;
}
publicStringreverse(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 classAdvancedCipherextendsCipherDecorator{
publicAdvancedCipher(Cipher cipher){
super(cipher);
}
publicStringencrypt(String plainText){ // 加密处理
String result=super.encrypt(plainText);
result=mod(result);
return result;
}
publicStringmod(String text){
String str="";
for(int i=0;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 classClient{
publicstaticvoidmain(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
---------------------
本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/5962431.html,如需转载请自行联系原作者