装饰
1:简介
装饰(Decorator)模式又名包装(Wrapper)模式[GOF95]。装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案。
2:类图
抽象构件(Component)角色:给出一个抽象接口,以规范准备接收附加责任的对象。
具体构件(Concrete Component)角色:定义一个将要接收附加责任的类。
装饰(Decorator)角色:持有一个构件(Component)对象的实例,并定义一个与抽象构件接口一致的接口。
具体装饰(Concrete Decorator)角色:负责给构件对象"贴上"附加的责任。
装饰(Decorator)模式又名包装(Wrapper)模式[GOF95]。装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案。
2:类图
3:代码
//抽象构件(Component)角色
public abstract class Tank
{
public abstract void Shot();
}
//具体构件(Concrete Component)角色
public class T50 : Tank
{
public override void Shot()
{
}
}
//装饰(Decorator)角色
public abstract class Decorator : Tank //Do Like
{
private Tank tank; //Has-a
public void SetComponent(Tank tanks)
{
this.tank = tanks;
}
public override void Shot()
{
this.tank.Shot();
}
}
//具体装饰(Concrete Decorator)角色
public class ConcreteDecoratorA : Decorator
{
public override void Shot()
{
System.Console.WriteLine("增加夜视能力");
base.Shot();
}
}
//具体装饰(Concrete Decorator)角色
public class ConcreteDecoratorB : Decorator
{
public override void Shot()
{
System.Console.WriteLine("增加卫星定位能力");
base.Shot();
}
}
/*
public abstract class Tank
{
public abstract void Shot();
}
public class T50 : Tank
{
public override void Shot()
{
}
}
public abstract class Decorator : Tank
{
private Tank tank;
public Decorator(Tank _tank)
{
this.tank = _tank;
}
public override void Shot()
{
}
}
public class DecoratorA : Decorator
{
public DecoratorA(Tank tank)
: base(tank)
{
}
public override void Shot()
{
System.Console.WriteLine("增加夜视能力");
base.Shot();
}
}
public class DecoratorB : Decorator
{
public DecoratorB(Tank tank)
: base(tank)
{
}
public override void Shot()
{
System.Console.WriteLine("增加夜视能力");
base.Shot();
}
}
*/
//调用
Bll.Decorator.Tank t50 = new Bll.Decorator.T50();
Bll.Decorator.ConcreteDecoratorA concreteDecoratorA = new Bll.Decorator.ConcreteDecoratorA();
Bll.Decorator.ConcreteDecoratorB concreteDecoratorB = new Bll.Decorator.ConcreteDecoratorB();
concreteDecoratorA.SetComponent(t50);
concreteDecoratorB.SetComponent(concreteDecoratorA);
concreteDecoratorB.Shot();
public abstract class Tank
{
public abstract void Shot();
}
//具体构件(Concrete Component)角色
public class T50 : Tank
{
public override void Shot()
{
}
}
//装饰(Decorator)角色
public abstract class Decorator : Tank //Do Like
{
private Tank tank; //Has-a
public void SetComponent(Tank tanks)
{
this.tank = tanks;
}
public override void Shot()
{
this.tank.Shot();
}
}
//具体装饰(Concrete Decorator)角色
public class ConcreteDecoratorA : Decorator
{
public override void Shot()
{
System.Console.WriteLine("增加夜视能力");
base.Shot();
}
}
//具体装饰(Concrete Decorator)角色
public class ConcreteDecoratorB : Decorator
{
public override void Shot()
{
System.Console.WriteLine("增加卫星定位能力");
base.Shot();
}
}
/*
public abstract class Tank
{
public abstract void Shot();
}
public class T50 : Tank
{
public override void Shot()
{
}
}
public abstract class Decorator : Tank
{
private Tank tank;
public Decorator(Tank _tank)
{
this.tank = _tank;
}
public override void Shot()
{
}
}
public class DecoratorA : Decorator
{
public DecoratorA(Tank tank)
: base(tank)
{
}
public override void Shot()
{
System.Console.WriteLine("增加夜视能力");
base.Shot();
}
}
public class DecoratorB : Decorator
{
public DecoratorB(Tank tank)
: base(tank)
{
}
public override void Shot()
{
System.Console.WriteLine("增加夜视能力");
base.Shot();
}
}
*/
//调用
Bll.Decorator.Tank t50 = new Bll.Decorator.T50();
Bll.Decorator.ConcreteDecoratorA concreteDecoratorA = new Bll.Decorator.ConcreteDecoratorA();
Bll.Decorator.ConcreteDecoratorB concreteDecoratorB = new Bll.Decorator.ConcreteDecoratorB();
concreteDecoratorA.SetComponent(t50);
concreteDecoratorB.SetComponent(concreteDecoratorA);
concreteDecoratorB.Shot();