设计模式-装饰模式

设计模式-装饰模式

装饰模式基本实现

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

总结

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

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

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

相关推荐
万邦科技Lafite1 小时前
京东按图搜索京东商品(拍立淘) API (.jd.item_search_img)快速抓取数据
开发语言·前端·数据库·python·电商开放平台·京东开放平台
Never_Satisfied3 小时前
在JavaScript / Node.js / 抖音小游戏中,使用tt.request通信
开发语言·javascript·node.js
爱吃小胖橘3 小时前
Unity资源加载模块全解析
开发语言·unity·c#·游戏引擎
你的人类朋友4 小时前
设计模式有哪几类?
前端·后端·设计模式
时光追逐者5 小时前
全面的 C#/.NET 图表构建解决方案,助力快速实现图表开发需求!
微软·c#·.net·.net core·图表
m5655bj5 小时前
通过 C# 在 Word 文档中添加文字或图片水印
c#·word·visual studio
千里镜宵烛5 小时前
Lua-迭代器
开发语言·junit·lua
渡我白衣5 小时前
C++ 同名全局变量:当符号在链接器中“相遇”
开发语言·c++·人工智能·深度学习·microsoft·语言模型·人机交互
淮北4946 小时前
html + css +js
开发语言·前端·javascript·css·html
你的人类朋友6 小时前
适配器模式:适配就完事了bro!
前端·后端·设计模式