JAVA设计模式之【装饰者模式】

简介:

JAVA设计模式之【装饰者模式】

装饰模式    对新房进行装修并没有改变房屋的本质,但它可以让房子变得更漂亮、更温馨、更实用。    在软件设计中,对已有对象(新房)的功能进行扩展(装修)。    把通用功能封装在装饰器中,用到的地方进行调用。    装饰模式是一种用于替代继承的技术,使用对象之间的关联关系取代类之间的继承关系。引入装饰类,扩充新功能。    角色        抽象构件        具体构件        抽象装饰类        具体装饰类

案例一,窗体装饰

1.组件类

package Decorator; // 装饰者模式

/**
 * Created by Jiqing on 2016/10/13.
 */
abstract class Component {
    public abstract void display();
}
AI 代码解读

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

}
AI 代码解读

3.继承类ListBox

package Decorator;

/**
 * Created by Jiqing on 2016/10/13.
 */
public class ListBox extends Component{
    public void display() {
        System.out.println("显示列表框!");
    }
}
AI 代码解读

4.继承类TextBox

package Decorator;

/**
 * Created by Jiqing on 2016/10/13.
 */
public class TextBox extends Component{
    public void display() {
        System.out.println("显示文本框!");
    }
}
AI 代码解读

5.继承类Window

package Decorator;

/**
 * Created by Jiqing on 2016/10/13.
 */
public class Window extends Component{
    public void display() {
        System.out.println("显示窗体!");
    }
}
AI 代码解读

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("为构件增加黑色边框!");

    }
}
AI 代码解读

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("为构件增加滚动条!");
    }
}
AI 代码解读

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();
    }
}
AI 代码解读

执行结果

为构件增加滚动条!
显示窗体!
--------------------
为构件增加黑色边框!
为构件增加滚动条!
显示窗体!
AI 代码解读

 案例二,密文装饰

1.密文接口

package Decorator.sample2;

/**
 * Created by Jiqing on 2016/10/14.
 */
public interface Cipher // 密文接口
{
    public String encrypt(String plainText);
}
AI 代码解读

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);
    }
}
AI 代码解读

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;
    }
}
AI 代码解读

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;
    }
}
AI 代码解读

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;
    }
}
AI 代码解读

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("---------------------");
    }
}
AI 代码解读

执行结果

Powotm9006
---------------------
6009mtowoP
---------------------
0003123532
---------------------
AI 代码解读

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

目录
打赏
0
0
0
0
64
分享
相关文章
Java 设计模式:装饰者模式(Decorator Pattern)
装饰者模式属于结构型设计模式,允许通过动态包装对象的方式为对象添加新功能,提供比继承更灵活的扩展方式。该模式通过组合替代继承,遵循开闭原则(对扩展开放,对修改关闭)。
「全网最细 + 实战源码案例」设计模式——装饰者模式
装饰者模式(Decorator Pattern)是一种结构型设计模式,通过“包装”现有对象来为其添加额外功能,而无需修改原有代码。它通过创建装饰类来扩展对象的功能,而非继承。该模式由抽象构件、具体构件、抽象装饰者和具体装饰者组成,允许在运行时动态组合功能。穿衣服的例子很好地解释了装饰者模式:你可以根据需要一层层添加衣物,如毛衣、夹克和雨衣,每件衣物都扩展了基本行为,但不是你的一部分,可以随时脱掉。 优点包括灵活性、避免子类爆炸和符合开闭原则;缺点是可能增加复杂性和难以理解。适用于希望在不修改代码的情况下为对象新增行为的场景,尤其当继承难以实现或不可行时。
74 15
Java 设计模式——观察者模式:从优衣库不使用新疆棉事件看系统的动态响应
【11月更文挑战第17天】观察者模式是一种行为设计模式,定义了一对多的依赖关系,使多个观察者对象能直接监听并响应某一主题对象的状态变化。本文介绍了观察者模式的基本概念、商业系统中的应用实例,如优衣库事件中各相关方的动态响应,以及模式的优势和实际系统设计中的应用建议,包括事件驱动架构和消息队列的使用。
103 6
Kotlin教程笔记(56) - 改良设计模式 - 装饰者模式
Kotlin教程笔记(56) - 改良设计模式 - 装饰者模式
61 2
Kotlin教程笔记(56) - 改良设计模式 - 装饰者模式
Kotlin教程笔记(56) - 改良设计模式 - 装饰者模式
Java编程中的设计模式:单例模式的深度剖析
【10月更文挑战第41天】本文深入探讨了Java中广泛使用的单例设计模式,旨在通过简明扼要的语言和实际示例,帮助读者理解其核心原理和应用。文章将介绍单例模式的重要性、实现方式以及在实际应用中如何优雅地处理多线程问题。
76 4
Kotlin - 改良设计模式 - 装饰者模式
Kotlin - 改良设计模式 - 装饰者模式
41 4
Kotlin教程笔记(56) - 改良设计模式 - 装饰者模式
Kotlin教程笔记(56) - 改良设计模式 - 装饰者模式
[Java]23种设计模式
本文介绍了设计模式的概念及其七大原则,强调了设计模式在提高代码重用性、可读性、可扩展性和可靠性方面的作用。文章还简要概述了23种设计模式,并提供了进一步学习的资源链接。
122 0
[Java]23种设计模式
Java设计模式:建造者模式详解
建造者模式是一种创建型设计模式,通过将复杂对象的构建过程与表示分离,使得相同的构建过程可以创建不同的表示。本文详细介绍了建造者模式的原理、背景、应用场景及实际Demo,帮助读者更好地理解和应用这一模式。
186 0

热门文章

最新文章