设计模式-装饰模式

设计模式-装饰模式

装饰模式基本实现

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 小时前
别再手动凑 PR 了:这个 AI Skill 会按仓库习惯自动建分支、拆提交、提 PR
人工智能·设计模式·程序员
刀法如飞2 小时前
从程序员到架构师:6大编程范式全解析与实践对比
设计模式·系统架构·编程范式
九狼3 小时前
Flutter + Riverpod +MVI 架构下的现代状态管理
设计模式
静水流深_沧海一粟19 小时前
04 | 别再写几十个参数的构造函数了——建造者模式
设计模式
StarkCoder19 小时前
从UIKit到SwiftUI的迁移感悟:数据驱动的革命
设计模式
阿星AI工作室1 天前
给openclaw龙虾造了间像素办公室!实时看它写代码、摸鱼、修bug、写日报,太可爱了吧!
前端·人工智能·设计模式
_哆啦A梦2 天前
Vibe Coding 全栈专业名词清单|设计模式·基础篇(创建型+结构型核心名词)
前端·设计模式·vibecoding
Scout-leaf3 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
用户298698530143 天前
程序员效率工具:Spire.Doc如何助你一键搞定Word表格排版
后端·c#·.net
mudtools4 天前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net