装饰器模式(C#)

装饰器模式(C#)

先举个例子:

我们创建一个Person对象,
然后对他进行装饰:
1.装饰上红色的帽子
2.装饰上黑色的外套

那么我们这样写代码(部分代码)看上去会比较舒服

static void Main(string[] args)
{
    //原本的张三
    Console.WriteLine("********原本的张三*********");
    IPerson person = new Person("张三", 18);
    person.Introduct();

    //装饰上红帽子
    Console.WriteLine("*********装饰上红帽子************");
    person = new PersonRedHatDecorator(person);
    person.Introduct();

    //装饰上黑外套
    Console.WriteLine("*********装饰上黑外套************");
    person = new PersonBlackJacketDecorator(person);
    person.Introduct();
}

运行结果
在这里插入图片描述
这里就用到了装饰器模式
装饰器模式可以给现有的类添加一些装饰(额外的东西),但是又不改变原有的类

装饰器模式体现在PersonRedHatDecorator类的Introduct方法上,该方法实现上其实就是调用了传入的IPerson的方法,在此基础上有增加了别的操作,这样就起到了装饰的效果

重点观察PersonRedHatDecorator类:

class PersonRedHatDecorator : IPerson
{
    private IPerson _person;
    public PersonRedHatDecorator(IPerson person)
    {
        _person = person;
    }
    public void Introduct()
    {
        _person.Introduct();
        Console.WriteLine("我有一顶红色的帽子");
    }
}

完整代码:

using System;

namespace CSharpTest2
{
    internal interface IPerson
    {
        void Introduct();
    }
    class Person : IPerson
    {
        public string Name { get; private set; }
        public int Age { get; private set; }
        public Person(string name, int age)
        {
            Name = name;
            Age = age;
        }
        public void Introduct()
        {
            Console.WriteLine($"我叫{Name},今年{Age}岁");
        }
    }
    class PersonRedHatDecorator : IPerson
    {
        private IPerson _person;
        public PersonRedHatDecorator(IPerson person)
        {
            _person = person;
        }
        public void Introduct()
        {
            _person.Introduct();
            Console.WriteLine("我有一顶红色的帽子");
        }
    }

    class PersonBlackJacketDecorator : IPerson
    {
        private IPerson _person;
        public PersonBlackJacketDecorator(IPerson person)
        {
            _person = person;
        }
        public void Introduct()
        {
            _person.Introduct();
            Console.WriteLine("我有一件黑色的外套");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //原本的张三
            Console.WriteLine("********原本的张三*********");
            IPerson person = new Person("张三", 18);
            person.Introduct();

            //装饰上红帽子
            Console.WriteLine("*********装饰上红帽子************");
            person = new PersonRedHatDecorator(person);
            person.Introduct();

            //装饰上黑外套
            Console.WriteLine("*********装饰上黑外套************");
            person = new PersonBlackJacketDecorator(person);
            person.Introduct();
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值