装饰模式(Decorator)

装饰模式(Decorator):动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式(Decorator)比生成子类更为灵活。

Component:定义一个对象接口,可以给这些对象动态地添加职责;

ConcreteComponent:定义了一个具体的对象,可以给这个对象动态地添加一些职责;

Decorator:装饰抽象类,继承了Component,从外类来扩张Component类的功能,但对Component来说,是无需知道Decorator的存在的;

ConcreteDecoratorA和ConcreteDecoratorB:具体的装饰类,起到给Component对象添加职责的功能;

基本的代码实现:

基本代码实现
/// <summary>
/// Component定义一个对象接口,可以给这些对象动态地添加职 责(功能之类的)
/// </summary>
abstract class Component
{
public abstract void Operation();
}
/// <summary>
/// 定义了一个具体的对象,可以给这个对象添加一项绝提的功能
/// </summary>
class ConcreteComponent:Component
{
public override void Operation()
{
Console.WriteLine(
"具体的操作对象");
}
}

abstract class Decorator:Component
{
/// <summary>
/// 被修饰对象
/// </summary>
protected Component component;

/// <summary>
/// 设置Component
/// </summary>
/// <param name="component"></param>
public void SetComponent(Component component)
{
this.component = component;
}

public override void Operation()
{
if (component != null)
{
component.Operation();
}
}
}

class ConcreteDecoratorA:Decorator
{
public ConcreteDecoratorA()
{
addState
= "装饰1";
}
private string addState;

public override void Operation()
{
base.Operation();
Console.WriteLine(addState);
}
}

class ConcreteDecoratorB:Decorator
{
public override void Operation()
{
base.Operation();
Behiver();
}

private void Behiver()
{
Console.WriteLine(
"装饰B的特有功能");
}
}

class Program
{
static void Main(string[] args)
{
///装饰模式(Decorator)代码介绍,客户端(Client) ---Begin---
Component com = new ConcreteComponent();
ConcreteDecoratorA decA
= new ConcreteDecoratorA();
ConcreteDecoratorB decB
= new ConcreteDecoratorB();
decA.SetComponent(com);
decB.SetComponent(decA);
decB.Operation();
Console.ReadKey();
//装饰模式(Decorator)代码介绍,客户端(Client) ---End---
}
}

 

实例:模拟人的穿着:一个穿衣服的人 较某某某,戴帽子,穿西服,穿皮鞋

代码
/// <summary>
/// 定义人这个抽象类
/// </summary>
abstract class Person
{
public abstract void Show();
}

/// <summary>
/// 这个人吧,有穿衣服的,有不穿的(原始),就继承人这个类后具体添加个穿衣服的特点
/// </summary>
class WareClothesPerson:Person
{
private string personName;

public WareClothesPerson(string PersonName)
{
this.personName = PersonName;
}

public override void Show()
{
if (string.IsNullOrEmpty(personName))
{
Console.WriteLine(
"此人竟然没有名字");
}
else
{
Console.WriteLine(
"大家好,我叫{0}", personName);
}
}
}

/// <summary>
/// 人穿的服饰抽象类
/// </summary>
abstract class Finery:Person
{
private Person person;

/// <summary>
/// 装载被修饰的人
/// </summary>
/// <param name="person">人的抽象类对象</param>
public void SetPerson(Person person)
{
this.person = person;
}

public override void Show()
{
if (person != null)
{
person.Show();
}
}
}

/// <summary>
/// 戴帽子
/// </summary>
class Hat:Finery
{
public override void Show()
{
base.Show();
Console.WriteLine(
"戴了个帽子");
}
}

/// <summary>
/// 穿皮鞋
/// </summary>
class LeatherShoes:Finery
{
public override void Show()
{
base.Show();
Console.WriteLine(
"穿了双皮鞋");
}
}

/// <summary>
/// 穿西服
/// </summary>
class Suit:Finery
{
public override void Show()
{
base.Show();
Console.WriteLine(
"穿了件西服");
}
}

class Program
{
static void Main(string[] args)
{
//装饰模式(Decorator)使用实例,客户端(Client) ---Begin---
Person ps = new WareClothesPerson("黑矿");
Hat hat
= new Hat();
Suit suit
= new Suit();
LeatherShoes leatherShoes
= new LeatherShoes();

hat.SetPerson(ps);
suit.SetPerson(hat);
leatherShoes.SetPerson(suit);

leatherShoes.Show();
Console.ReadKey();
//装饰模式(Decorator)使用实例,客户端(Client) ---End---
}
}

 

思考变通:如果只有一个ConcreteComponent类而没有Component类,那么Decorator类可以是ConcreteComponent类的一个子类。同理,如果只有一个ConcreteDecorator类,那么就没有必要单独建立Decorator类,可以把Decorator类和ConcreteComponent类合并成一个类。

总结:

装饰模式(Decorator)主要功能:动态地为已有功能添加更多功能的一种方法;它把每一个要装饰的功能放在单独的类中,并让这个类包装它要装饰的对象,因此,当药执行特殊行为时,客户端代码可以根据需要有选择余地地、顺序地使用装饰功能包装对象。

好处:有效地把类的核心职责和装饰功能区分开,可以出去相关类中重复的装饰逻辑。

posted @ 2011-01-13 14:45  煤矿  阅读(238)  评论(0)    收藏  举报