C# 设计模式 装饰器模式

请添加图片描述

1.抽象接口 Bike

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace decorator
{
    // 自行车组件
    interface Bike
    {
        string GetDescription();

        int GetPrice();
    }
}

2.被装饰者 BigBike

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace decorator
{
    class BigBike : Bike
    {
        private string description = "大号自行车";

        public string GetDescription()
        {
            return description;
        }

        // 大号自行车 200元
        public int GetPrice()
        {
            return 200;
        }
    }
}

3.装饰者 BikeDecorator

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace decorator
{
    class BikeDecorator : Bike
    {
        private string description = "这是个装饰器";

        public virtual string GetDescription()
        {
            return description;
        }

        public virtual int GetPrice()
        {
            return 0;
        }
    }
}

4.防爆胎装饰器(具体装饰器)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace decorator
{
    // 防爆胎装饰器 (具体装饰器)
    class RSCBikeDecorator : BikeDecorator
    {
        private string description = "增加一个防爆胎";
        private Bike bike;
        
        public RSCBikeDecorator(Bike bike)
        {
            this.bike = bike;
        }

        public override string GetDescription()
        {
            return this.bike.GetDescription() + "," + this.description;
        }

        public override int GetPrice()
        {
            return this.bike.GetPrice() + 100; // 防爆胎的价格
        }
    }
}

5.喇叭装饰器(具体装饰器)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace decorator
{
    class SuonaBikeDecorator : BikeDecorator
    {
        private string description = "增加一个喇叭";
        private Bike bike;

        public SuonaBikeDecorator(Bike bike)
        {
            this.bike = bike;
        }

        public override string GetDescription()
        {
            return this.bike.GetDescription() + "," + this.description;
        }

        public override int GetPrice()
        {
            return this.bike.GetPrice() + 50; // 防爆胎的价格
        }
    }
}

6、测试

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace decorator
{
    class Program
    {
        static void Main(string[] args)
        {
            // 选个自行车
            Bike bigBike = new BigBike();

            // 搭配防爆胎
            bigBike = new RSCBikeDecorator(bigBike);
            // 搭配个喇叭
            bigBike = new SuonaBikeDecorator(bigBike);
            // 搭配个喇叭
            bigBike = new SuonaBikeDecorator(bigBike);

            
            Console.WriteLine(bigBike.GetDescription());
            Console.WriteLine("价格:" + bigBike.GetPrice().ToString());

            Console.ReadLine();
        }
    }
}

在这里插入图片描述

7.装饰器优点

  • 装饰模式比继承有更多的灵活性
  • 可以使用不同的装饰器 排列组合,符合开闭原则。

8.装饰器缺点

  • 增加了许多子类,过度使用使得程序很复杂。
  • 增加系统的复杂度,加大学习理解的难度。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

廷益--飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值