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

执行结果

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

 案例二,密文装饰

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

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

相关文章
|
3月前
|
设计模式 Java Spring
Java 设计模式之责任链模式:优雅处理请求的艺术
责任链模式通过构建处理者链,使请求沿链传递直至被处理,实现发送者与接收者的解耦。适用于审批流程、日志处理等多级处理场景,提升系统灵活性与可扩展性。
415 2
|
3月前
|
设计模式 网络协议 数据可视化
Java 设计模式之状态模式:让对象的行为随状态优雅变化
状态模式通过封装对象的状态,使行为随状态变化而改变。以订单为例,将待支付、已支付等状态独立成类,消除冗长条件判断,提升代码可维护性与扩展性,适用于状态多、转换复杂的场景。
399 0
|
5月前
|
设计模式 缓存 Java
Java设计模式(二):观察者模式与装饰器模式
本文深入讲解观察者模式与装饰器模式的核心概念及实现方式,涵盖从基础理论到实战应用的全面内容。观察者模式实现对象间松耦合通信,适用于事件通知机制;装饰器模式通过组合方式动态扩展对象功能,避免子类爆炸。文章通过Java示例展示两者在GUI、IO流、Web中间件等场景的应用,并提供常见陷阱与面试高频问题解析,助你写出灵活、可维护的代码。
|
3月前
|
设计模式 算法 搜索推荐
Java 设计模式之策略模式:灵活切换算法的艺术
策略模式通过封装不同算法并实现灵活切换,将算法与使用解耦。以支付为例,微信、支付宝等支付方式作为独立策略,购物车根据选择调用对应支付逻辑,提升代码可维护性与扩展性,避免冗长条件判断,符合开闭原则。
466 35
|
3月前
|
设计模式 消息中间件 传感器
Java 设计模式之观察者模式:构建松耦合的事件响应系统
观察者模式是Java中常用的行为型设计模式,用于构建松耦合的事件响应系统。当一个对象状态改变时,所有依赖它的观察者将自动收到通知并更新。该模式通过抽象耦合实现发布-订阅机制,广泛应用于GUI事件处理、消息通知、数据监控等场景,具有良好的可扩展性和维护性。
367 8
|
8月前
|
设计模式 缓存 安全
【高薪程序员必看】万字长文拆解Java并发编程!(8):设计模式-享元模式设计指南
🌟 ​大家好,我是摘星!​ 🌟今天为大家带来的是并发编程中的经典对象复用设计模式-享元模式,废话不多说让我们直接开始。
187 0
|
5月前
|
设计模式 安全 Java
Java设计模式(一):单例模式与工厂模式
本文详解单例模式与工厂模式的核心实现及应用,涵盖饿汉式、懒汉式、双重检查锁、工厂方法、抽象工厂等设计模式,并结合数据库连接池与支付系统实战案例,助你掌握设计模式精髓,提升代码专业性与可维护性。
|
5月前
|
设计模式 XML 安全
Java枚举(Enum)与设计模式应用
Java枚举不仅是类型安全的常量,还具备面向对象能力,可添加属性与方法,实现接口。通过枚举能优雅实现单例、策略、状态等设计模式,具备线程安全、序列化安全等特性,是编写高效、安全代码的利器。
|
8月前
|
设计模式 缓存 安全
【设计模式】【结构型模式】装饰者模式(Decorator)
一、入门 什么是装饰者模式? 装饰者模式(Decorator Pattern)是 Java 中常用的结构型设计模式,它能在不修改原有对象结构的前提下,动态地为对象添加额外的职责。 为什么要装饰者模式?
209 8
|
10月前
|
设计模式 Java 数据安全/隐私保护
Java 设计模式:装饰者模式(Decorator Pattern)
装饰者模式属于结构型设计模式,允许通过动态包装对象的方式为对象添加新功能,提供比继承更灵活的扩展方式。该模式通过组合替代继承,遵循开闭原则(对扩展开放,对修改关闭)。

热门文章

最新文章