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

执行结果

为构件增加滚动条!
显示窗体!
--------------------
为构件增加黑色边框!
为构件增加滚动条!
显示窗体!

422101-20161014225513875-1745462407.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<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
---------------------

422101-20161014225522812-515626629.png

本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/5962431.html,如需转载请自行联系原作者

相关文章
|
19天前
|
设计模式 Java 开发者
设计模式揭秘:Java世界的七大奇迹
【4月更文挑战第7天】探索Java设计模式:单例、工厂方法、抽象工厂、建造者、原型、适配器和观察者,助你构建健壮、灵活的软件系统。了解这些模式如何提升代码复用、可维护性,以及在特定场景下的应用,如资源管理、接口兼容和事件监听。掌握设计模式,但也需根据实际情况权衡,打造高效、优雅的软件解决方案。
|
20天前
|
设计模式 存储 Java
23种设计模式,享元模式的概念优缺点以及JAVA代码举例
【4月更文挑战第6天】享元模式(Flyweight Pattern)是一种结构型设计模式,旨在通过共享技术有效地支持大量细粒度对象的重用。这个模式在处理大量对象时非常有用,特别是当这些对象中的许多实例实际上可以共享相同的状态时,从而可以减少内存占用,提高程序效率
35 4
|
20天前
|
设计模式 Java 中间件
23种设计模式,适配器模式的概念优缺点以及JAVA代码举例
【4月更文挑战第6天】适配器模式(Adapter Pattern)是一种结构型设计模式,它的主要目标是让原本由于接口不匹配而不能一起工作的类可以一起工作。适配器模式主要有两种形式:类适配器和对象适配器。类适配器模式通过继承来实现适配,而对象适配器模式则通过组合来实现
30 4
|
23天前
|
设计模式 Java 数据库
Java设计模式精讲:让代码更优雅、更可维护
【4月更文挑战第2天】**设计模式是解决软件设计问题的成熟方案,分为创建型、结构型和行为型。Java中的单例模式确保类仅有一个实例,工厂方法模式让子类决定实例化哪个类。适配器模式则协调不兼容接口间的合作。观察者模式实现了一对多依赖,状态变化时自动通知相关对象。学习和适当应用设计模式能提升代码质量和可维护性,但需避免过度使用。设计模式的掌握源于实践与不断学习。**
Java设计模式精讲:让代码更优雅、更可维护
|
27天前
|
设计模式 安全 Java
在Java中即指单例设计模式
在Java中即指单例设计模式
18 0
|
19天前
|
设计模式 监控 Java
设计模式 - 观察者模式(Observer):Java中的战术与策略
【4月更文挑战第7天】观察者模式是构建可维护、可扩展系统的关键,它在Java中通过`Observable`和`Observer`实现对象间一对多的依赖关系,常用于事件处理、数据绑定和同步。该模式支持事件驱动架构、数据同步和实时系统,但需注意避免循环依赖、控制通知粒度,并关注性能和内存泄漏问题。通过明确角色、使用抽象和管理观察者注册,可最大化其效果。
|
1天前
|
设计模式 算法 Java
[设计模式Java实现附plantuml源码~行为型]定义算法的框架——模板方法模式
[设计模式Java实现附plantuml源码~行为型]定义算法的框架——模板方法模式
|
1天前
|
设计模式 JavaScript Java
[设计模式Java实现附plantuml源码~行为型] 对象状态及其转换——状态模式
[设计模式Java实现附plantuml源码~行为型] 对象状态及其转换——状态模式
|
2天前
|
设计模式 存储 JavaScript
[设计模式Java实现附plantuml源码~创建型] 多态工厂的实现——工厂方法模式
[设计模式Java实现附plantuml源码~创建型] 多态工厂的实现——工厂方法模式
|
2天前
|
设计模式 Java Go
[设计模式Java实现附plantuml源码~创建型] 集中式工厂的实现~简单工厂模式
[设计模式Java实现附plantuml源码~创建型] 集中式工厂的实现~简单工厂模式