设计模式:
装饰器模式(Decorator Pattern)
简单介绍:
装饰器模式(Decorator Pattern):
假设有一部手机你购买来以后只有手机,然后你需要再给其添加一些装饰,比如保护壳,钢化膜或者手机贴画等,这个就是装饰者模式的思想
装饰器模式主要组成部分:
Component:定义一个对象接口,可以给这些对象动态地添加职责
ConcreteComponent:定义一个对象,可以给这个对象添加一些职责
Decorator:维持一个指向Component的指针,并定义一个与Component接口一致的接口
ConcreteDecorator:负责向ConcreteComponent添加功能
在装饰模式中,Decorator定义了一个装饰接口类。因为Decorator与ConcreteComponent继承同一个接口,所以继承Decorator的类ConcreteDecorator可以使用ConcreteComponent的方法,再在ConcreteDecorator里面加入一些新的方法,也就是装饰,就成为了一个包装好的装饰类。
装饰器模式类图:
装饰者模式C#代码举例:
Component 对象类
/// <summary>
/// 对象类
/// </summary>
public abstract class Component
{
public abstract void Print();
}
ConcreteComponenet 类 对象的修饰实现类
public class ConcreteComponent:Component
{
/// <summary>
/// 对象修饰类
/// </summary> public override void Print()
{
Console.WriteLine("我是ConcreteComponent!");
}
}
Decorator类 装饰器类
/// <summary>
/// 装饰器类
/// </summary>
public class Decorator:Component
{
protected Component _component; public void SetComponent(Component component)
{
this._component = component;
}
public override void Print()
{
if (_component != null)
{
_component.Print();
}
}
}
ConcreteDecoratorA类 装饰子类A
/// <summary>
/// 装饰子类A
/// </summary>
public class ConcreteDecoratorA: Decorator
{
public override void Print()
{
base.Print();
Console.WriteLine("我是ConcreteDecoratorA!");
}
}
ConcreteDecoratorB 装饰子类B
/// <summary>
/// 装饰子类B
/// </summary>
public class ConcreteDecoratorB:Decorator
{
public override void Print()
{
base.Print();
Console.WriteLine("我是ConcreteDecoratorB!");
}
}
用户测试类:
class Client
{
static void Main(string[] args)
{
ConcreteComponent concreteComponent = new ConcreteComponent();
ConcreteDecoratorA concreteDecoratorA = new ConcreteDecoratorA();
ConcreteDecoratorB concreteDecoratorB = new ConcreteDecoratorB(); concreteDecoratorA.SetComponent(concreteComponent);
concreteDecoratorA.Print();
Console.WriteLine("--------------------------------");
concreteDecoratorB.SetComponent(concreteDecoratorA); concreteDecoratorB.Print();
Console.Read(); }
}
运行结果:
源代码工程文件下载
装饰者模式实际生活举例
举例说明
假设有一部手机你购买来以后只有手机,然后你需要再给其添加一些装饰,比如保护壳,钢化膜或者手机贴画等
mobilephone 原手机接口
/// <summary>
/// 手机接口
/// </summary>
public abstract class MobilePhone
{
public abstract void Print();
}
AppleMobilePhone类 手机的修饰类
/// <summary>
/// 手机的修饰
/// </summary>
public class AppleMobilePhone :MobilePhone
{
public override void Print()
{
Console.WriteLine("我是一部苹果手机!");
}
}
Decorator类 装饰器类
/// <summary>
/// 装饰器
/// </summary>
public class Decorator:MobilePhone
{
protected MobilePhone mobliePhone;
public void SetMobilePhone(MobilePhone phone)
{
this.mobliePhone = phone;
} public override void Print()
{
if (mobliePhone != null)
{
mobliePhone.Print();
}
}
}
MobilePhoneShell类 手机壳修饰类
/// <summary>
/// 手机壳修饰类
/// </summary>
public class MobilePhoneShell:Decorator
{
public override void Print()
{
base.Print();
Console.WriteLine("我是一个手机保护壳!");
}
}
MobilePhoneStickers类 手机贴画修饰类
/// <summary>
/// 手机贴画修饰类
/// </summary>
public class MobilePhoneStickers:Decorator
{
public override void Print()
{
base.Print();
Console.WriteLine("我是一个手机贴画!");
}
}
用户测试类
class Client
{
static void Main(string[] args)
{
Console.WriteLine("----------------手机-----------------");
AppleMobilePhone appleMobilePhone = new AppleMobilePhone();
appleMobilePhone.Print(); Console.WriteLine("---------加了手机壳的手机----------------");
MobilePhoneShell mobilePhoneShell = new MobilePhoneShell();
mobilePhoneShell.SetMobilePhone(appleMobilePhone);
mobilePhoneShell.Print(); Console.WriteLine("----------加了手机贴画的手机---------------");
MobilePhoneStickers mobilePhoneStickers = new MobilePhoneStickers();
mobilePhoneStickers.SetMobilePhone(appleMobilePhone);
mobilePhoneStickers.Print();
Console.WriteLine("----------既加了手机壳又加了手机贴画的手机--------"); mobilePhoneStickers.SetMobilePhone(mobilePhoneShell);
mobilePhoneStickers.Print(); Console.Read(); }
}