当前位置: 首页 > news >正文

科技节手抄报/百度seo培训班

科技节手抄报,百度seo培训班,做网站推广的公司,.com网站制作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();

}

}

执行结果

为构件增加滚动条!

显示窗体!

--------------------

为构件增加黑色边框!

为构件增加滚动条!

显示窗体!

28f82435be0fd8c10ff4b9037fcec652.png

案例二,密文装饰

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

{

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

{

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

---------------------

a642da845ac4cb94526e84681f73e6a4.png

http://www.jmfq.cn/news/5297185.html

相关文章:

  • php 网站缩略图/丹东网站seo
  • 网站地图对网站有什么意义/微信推广费用一般多少
  • seo做的比较牛的公司/seo公司培训课程
  • 建筑工程网站大全/建站软件
  • 温州市网站建设公司/sem竞价托管代运营
  • 企业服务 免费网站建设/昆明网络营销
  • 深圳三站合一网站建设/google网站
  • 乐清高端网站建设/优化网站的目的
  • java做网站的软件/vue seo优化
  • 网站做微信小程序号码/百度关键词排名价格
  • 大学生做那个视频网站/什么是互联网营销师
  • 网页微信版客户端/如何提高网站seo排名
  • 做摘抄的网站/论坛seo设置
  • 重庆哪里有做淘宝网站推广的/seo就业前景
  • 手机网站制作公司选哪家/最新实时新闻
  • 重庆承越网站制作公司/优化大师网页版
  • 做网站什么语言好/安顺seo
  • 做网站如何可以实现窗口切换功能/最经典最常用的网站推广方式
  • 做图片推广的网站/产品推广策划
  • 京东做代码的网站吗/网站搜索引擎拓客
  • 愿景 做中国最受欢迎的互联网网站/国内重大新闻10条
  • 如何增加网站权重/十大免费cms建站系统介绍
  • 单位的网站的建设/seo职位要求
  • 电子商务网站建设哪本教材比较适合中等专业学校用/免费发布外链
  • 香港特别行政区的区花是什么花/天津seo代理商
  • dedecms做电商网站/全国疫情高峰感染高峰进度
  • 团队如何分工做网站/全国免费发布广告信息平台
  • 黑客软件/开封网站优化公司
  • 小公司企业简介怎么写/黑帽seo优化软件
  • 软件公司网站素材/优化设计七年级下册语文答案