【设计模式】装饰器模式(C#)
2023-09-14 09:16:26 时间
【设计模式】装饰器模式
1、概述
背景
快餐店有炒面、炒饭这些快餐,可以额外附加鸡蛋、火腿、培根这些配菜,当然加配菜需要额外加钱,每个配菜的价钱通常不太一样,那么计算总价就会显得比较麻烦。
使用继承的方式存在的问题:
-
扩展性不好
如果要再加一种配料(火腿肠),我们就会发现需要给FriedRice和FriedNoodles分别定义一个子类。如果要新增一个快餐品类(炒河粉)的话,就需要定义更多的子类。
-
产生过多的子类
定义
指在不改变现有对象结构的情况下,动态地给该对象增加一些职责(即增加其额外功能)的模式。
结构
装饰(Decorator)模式中的角色:
- 抽象构件(Component)角色 :定义一个抽象接口以规范准备接收附加责任的对象。
- 具体构件(Concrete Component)角色 :实现抽象构件,通过装饰角色为其添加一些职责。
- 抽象装饰(Decorator)角色 : 继承或实现抽象构件,并包含具体构件的实例,可以通过其子类扩展具体构件的功能。
- 具体装饰(ConcreteDecorator)角色 :实现抽象装饰的相关方法,并给具体构件对象添加附加的责任。
2、实现思路
我们使用装饰者模式对快餐店案例进行改进,体会装饰者模式的精髓。
/// <summary>
/// 快餐
/// </summary>
public abstract class FastFood
{
public double Price;
public string Desc;
public FastFood(double price, string desc)
{
this.Price = price;
this.Desc = desc;
}
public abstract double Cost(); //获取价格
}
/// <summary>
/// 炒饭
/// </summary>
public class FriedRice : FastFood
{
public FriedRice(double price, string desc) :base(price, desc)
{
}
public override double Cost()
{
return this.Price;
}
}
/// <summary>
/// 炒面
/// </summary>
public class FriedNoodles:FastFood
{
public FriedNoodles(double price, string desc) : base(price, desc)
{
}
public override double Cost()
{
return this.Price;
}
}
/// <summary>
/// 配料
/// </summary>
public abstract class Garnish:FastFood
{
protected FastFood _fastFood;
public Garnish(FastFood fastFood, double price, string desc):base(price,desc)
{
this._fastFood = fastFood;
this.Desc = base.Desc + fastFood.Desc;
}
}
public class Egg:Garnish
{
public Egg(FastFood fastFood, double price, string desc):base(fastFood, price, desc)
{
}
public override double Cost()
{
return this.Price + _fastFood.Price;
}
}
public class Bacon : Garnish
{
public Bacon(FastFood fastFood, double price, string desc) : base(fastFood, price, desc)
{
}
public override double Cost()
{
return this.Price + _fastFood.Price;
}
}
class Program
{
static void Main(string[] args)
{
//点一份炒饭
FastFood food = new FriedRice(10,"炒饭");
//花费的价格
Console.WriteLine($"{food.Desc}:{food.Cost()}元");
Console.WriteLine("========");
//点一份加鸡蛋的炒饭
FastFood food1 = new FriedRice(10, "炒饭");
food1 = new Egg(food1,1,"鸡蛋");
//花费的价格
Console.WriteLine(food1.Desc + ":" + food1.Cost() + "元");
Console.WriteLine("========");
//点一份加培根的炒面
FastFood food2 = new FriedNoodles(12,"炒面");
food2 = new Bacon(food2,5,"培根");
//花费的价格
Console.WriteLine(food2.Desc + ":" + food2.Cost() + "元");
}
}
===============================
炒饭:10元
========
鸡蛋炒饭:11元
========
培根炒面:17元
3、优缺点
优点
- 饰者模式可以带来比继承更加灵活性的扩展功能,使用更加方便,可以通过组合不同的装饰者对象来获取具有不同行为状态的多样化的结果。装饰者模式比继承更具良好的扩展性,完美的遵循开闭原则,继承是静态的附加责任,装饰者则是动态的附加责任。
- 装饰类和被装饰类可以独立发展,不会相互耦合,装饰模式是继承的一个替代模式,装饰模式可以动态扩展一个实现类的功能。
4、使用场景
-
当不能采用继承的方式对系统进行扩充或者采用继承不利于系统扩展和维护时。
不能采用继承的情况主要有两类:
- 第一类是系统中存在大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长;
- 第二类是因为类定义不能继承(如密封类)
-
在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。
-
当对象的功能要求可以动态地添加,也可以再动态地撤销时。
相关文章
- C#实现Xml序列化与反序列化的方法
- C#设计模式——单件模式(Singleton Pattern)
- C#设计模式——生成器模式(Builder Pattern)
- C#设计模式(17)——观察者模式(Observer Pattern)
- C#设计模式——原型模式(Prototype Pattern)
- C#设计模式——观察者模式(Observer Pattern)1
- C#设计模式——抽象工厂模式(Abstract Factory Pattern)
- C#设计模式——适配器模式(Adapter Pattern)
- C#设计模式——装饰者模式(Decorator Pattern)
- C#设计模式——观察者模式(Observer Pattern)
- C# 单例模式的五种写法
- C# ToString()格式化时间,常用模式
- C#设计模式之建造者模式
- 【设计模式】迭代器模式(C#)
- 【设计模式】观察者模式(C#)