设计模式-装饰模式

设计模式-装饰模式

装饰模式基本实现

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

总结

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

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

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

相关推荐
咖啡八杯2 天前
GoF设计模式——备忘录模式
java·后端·spring·设计模式
槑有老呆2 天前
从 Prompt Engineering 到 Harness Engineering:AI 编程的下一次跃迁
设计模式
HjhIron2 天前
从Prompt到Context:大模型应用开发的范式转移
设计模式·aigc·ai编程
咖啡八杯4 天前
GoF设计模式——中介者模式
java·后端·spring·设计模式
hez20104 天前
在 .NET 上构建超大托管数组
c#·.net·.net core·gc·clr
胡萝卜术4 天前
从“分数打架”到“排名投票”:为什么你的ChatBI必须用RRF?
算法·设计模式·面试
亦暖筑序5 天前
Java 8老系统Browser Agent实战:三层拦截把AI操作后台变成可审计流程
java·后端·设计模式
青禾网络7 天前
Web 前端如何接入 AI 音效生成:从零到可用的完整方案
人工智能·设计模式
ZJPRENO8 天前
吃透软件开发六大设计原则,告别烂代码
设计模式
咖啡八杯8 天前
GoF设计模式——命令模式
java·设计模式·架构