装饰模式——添衣加被

  代码例子背景:在人物穿衣程序里,现有的服饰有上衣、裤子、鞋子,如果想给人物加上一顶帽子,装饰者模式中只需要在Finery类底部加上一个帽子类,然后在帽子类添加各种各样款式的帽子,不需要再把每一种帽子都单独做一个类。
  装饰者模式:主要就是用来扩展类,扩展开放,修改关闭。类的继承虽然也能实现扩展行为,但是它的粒度没有装饰者模式细,而且当有多个类需要被一个行为装饰时,你可能需要写多个继承对象。 装饰者模式必然有一个公共的接口或抽象类,用来作为对象的传递。你需要根据接口实现基本的被装饰类(Person),以及装饰类的公共接口(Decorator ),以后所有的装饰类都是继承自公共的装饰类接口,内部实现。

  装饰(Decorator)模式的定义:指在不改变现有对象结构的情况下,动态地给该对象增加一些职责(即增加其额外功能)的模式,它属于对象结构型模式。

  装饰(Decorator)模式的主要优点有:

  • 采用装饰模式扩展对象的功能比采用继承方式更加灵活。

  • 可以设计出多个不同的具体装饰类,创造出多个不同行为的组合。

  其主要缺点是:装饰模式增加了许多子类,如果过度使用会使程序变得很复杂。

  装饰模式的结构与实现:
  通常情况下,扩展一个类的功能会使用继承方式来实现。但继承具有静态特征,耦合度高,并且随着扩展功能的增多,子类会很膨胀。如果使用组合关系来创建一个包装对象(即装饰对象)来包裹真实对象,并在保持真实对象的类结构不变的前提下,为其提供额外的功能,这就是装饰模式的目标。下面来分析其基本结构和实现方法。

  1. 模式的结构
    装饰模式主要包含以下角色。
  • 抽象构件(Component)角色:定义一个抽象接口以规范准备接收附加责任的对象。

  • 具体构件(Concrete Component)角色:实现抽象构件,通过装饰角色为其添加一些职责。

  • 抽象装饰(Decorator)角色:继承抽象构件,并包含具体构件的实例,可以通过其子类扩展具体构件的功能。

  • 具体装饰(ConcreteDecorator)角色:实现抽象装饰的相关方法,并给具体构件对象添加附加的责任。

    扫描二维码关注公众号,回复: 12559365 查看本文章

在这里插入图片描述
Component类

//抽象父类——控件
    abstract  class Component
    {
    
    
        public abstract void Operation();
    }

    //子类——具体组件
    class ConcreteComponent : Component
    {
    
    
        public override void Operation()    //父类方法重载
        {
    
    
            Console.WriteLine("具体对象的操作");
        }
    }

Decorator类

 //抽象子类——装饰者
    abstract class Decorator : Component
    {
    
    
        protected Component component;
        public void SetComponent(Component component)
        {
    
    
            this.component = component;
        }

        public override void Operation()    //父类方法重载
        {
    
    
            if (component != null)
            {
    
    
                component.Operation();
            }
        }
    }

  

Finery类

 class Finery : Person
    {
    
    
        protected Person component;

        //打扮
        public void Decorate(Person component)
        {
    
    
            this.component = component;
        }
        public override void Show()
        {
    
    
            if (component != null)
            {
    
    
                component.Show();
            }
        }
    }

    //子类——大T恤
    class TShirts : Finery
    {
    
    
        public override void Show()
        {
    
    
            Console.Write("大T恤");
            base.Show();
        }
    }
    //子类——垮裤
    class BigTrouser : Finery
    {
    
    
        public override void Show()
        {
    
    
            Console.Write("垮裤");
            base.Show();
        }
    }
    //子类——板鞋
    class Shoes : Finery
    {
    
    
        public override void Show()
        {
    
    
            Console.Write("板鞋");
            base.Show();
        }
    }

Person类

class Person
    {
    
    
        public Person()
        {
    
     }

        private string _name;
        public Person (string name)
        {
    
    
            this._name = name;
        }
        public virtual void Show()
        {
    
    
            Console.WriteLine("装扮的{0}",_name);
        }
    }

客户端代码

class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            Person xc = new Person("小菜");

            Console.WriteLine("\n第一种装扮");

            Shoes  pqx = new Shoes();
            BigTrouser kk = new BigTrouser();
            TShirts dtx = new TShirts();

            pqx.Decorate(xc);
            kk.Decorate(pqx);
            dtx.Decorate(kk);
            dtx.Show();
            Console.ReadLine();
        }
    }

猜你喜欢

转载自blog.csdn.net/CharmaineXia/article/details/109994617