设计模式-装饰模式

设计模式-装饰模式

装饰模式基本实现

Component

java 复制代码
abstract class Component
{
    public abstract void Operation();
}

ConcreteComponent

java 复制代码
class ConcreteComponent:Component{
    public override void Operation(){
        Console.WriteLine("具体对象的操作")
    }
}

Decorator

java 复制代码
abstract class Decorator:Component
{
    protected Component component;
    public void SetComponent(Component component){
        this.component = component;
    }
    public override void Operation(){
        if(component != null){
            component.Operation();
        }
    }    
}

ConcreteDecorator相关类

java 复制代码
class ConcreteDecoratorA:Decrator
{
    private string addedState;
    public override void Operation(){
        base.Operation();
        addedState = "New State";
        Console.WriteLine("具体装饰对象A的操作");
    }
}

class ConcreteDecoratorB:Decrator
{
    private string addedState;
    public override void Operation(){
        base.Operation();
        AddedBehavior();
        Console.WriteLine("具体装饰对象B的操作");
    }
    public void AddedBehavior(){
        
    }
}

客户端代码

java 复制代码
static void Main(string[] args){
    ConcreteComponent c = new ConcreteComponent();
    ConcreteDecoratorA d1 = new ConcreteDecoratorA();
    ConcreteDecoratorB d2 = new ConcreteDecoratorB();
    d1.SetComponent(c);
    d2.SetComponent(d1);
    d2.Operation();
}

装饰模式实现换装程序

"Person"类 (ConcreteComponent)

java 复制代码
class Person{
    public Person(){}
    private string name;
    public Person(string name){
        this.name = name;
    }
    public virtual void show(){
        Console.WriteLine("装扮的{0}",name);
    }
}

服饰类(Decorator)

java 复制代码
class Finery:Person
{
    protected Person component;
    //打扮
    public void Decorate(Person component){
        this.component = component;
    }
    public override void Show(){
        if(component != null){
            component.Show();
        }
    }
}

具体服饰类(ConcreteDecorator)

java 复制代码
class TShirts:Finery
{
    public override void Show(){
        Console.Write("大T恤");
        base.show();//调用父类的方法            
    }
}

class BigTrouser:Finery
{
    public override void Show(){
        Console.Write("垮裤");
        base.show();//调用父类的方法            
    }
}
// ...

客户端代码

java 复制代码
static void Main(string[] args){
    Person xc = new Person("小菜");
    Console.WriteLine("\n 第一种装扮:");
    Sneakers pqx = new Sneakers();
    BigTrouser kk =new BigTrouser();
    TShirts dtx = new TShirts();
    pqx.Decorate(xc);
    kk.Decorate(pqx);
    dtx.Decorate(kk);
    dtx.show();
}

总结

装饰模式是为已有功能动态地添加更多功能地一种方式。

使用场景:当系统需要新功能的时候,是向旧的类中添加新的代码。这些新加的代码通常装饰了原有类的核心职责或主要行为。

优点:把类中的装饰功能从类中搬移去除,这样可以简化原有的类。有效地把类的核心职责和装饰功能区分开了。而且可以去除相关类中重复的装饰逻辑。

相关推荐
net3m332 分钟前
雅特力单片机用串口USART_INT_TDE中断比用USART_INT_TRAC的 发送效率要高
java·开发语言·算法
爱打代码的小林4 分钟前
python基础(逻辑回归例题)
开发语言·python·逻辑回归
laocooon52385788617 分钟前
C#二次开发中简单块的定义与应用
android·数据库·c#
一过菜只因18 分钟前
JavaWeb后端(spring--boot)
java·开发语言
五仁火烧23 分钟前
安装rust开发环境
开发语言·后端·rust
Yue丶越29 分钟前
【C语言】动态内存管理
c语言·开发语言
Edward1111111131 分钟前
普通java项目转为maven项目 J文件后缀.java变C文件
java·开发语言·maven
赵谨言31 分钟前
基于OpenCV的图像梯度与边缘检测研究
大数据·开发语言·经验分享·python
YJlio32 分钟前
BgInfo 学习笔记(11.5):多种输出方式(壁纸 / 剪贴板 / 文件)与“更新其他桌面”实战
笔记·学习·c#
Zhen (Evan) Wang34 分钟前
.NET 6 API使用Serilog APM
c#·.net