敲了下代码,不是很理解
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
ConcreteComponenth c = new ConcreteComponenth();
ConcreteDecoratorA ca = new ConcreteDecoratorA();
ConcreteDecoratorB cb = new ConcreteDecoratorB();
ca.SetComponent(c);
cb.SetComponent(ca);
cb.Operation();
Console.ReadLine();
}
}
public abstract class Componenth{
public abstract void Operation();
}
public class ConcreteComponenth:Componenth{
public override void Operation()
{
Console.WriteLine("Concrete Operatiion");
}
}
public abstract class Decorator:Componenth
{
protected Componenth component;
public void SetComponent(Componenth _com)
{
this.component = _com;
}
public override void Operation()
{
if (component!=null)
{
component.Operation();
}
//throw new NotImplementedException();
}
}
public class ConcreteDecoratorA:Decorator{
private string addedState;
public override void Operation()
{
base.Operation();
addedState = "new state";
Console.WriteLine("具体装饰对象A操作!");
}
}
public class ConcreteDecoratorB:Decorator{
public override void Operation()
{
base.Operation();
AddedBehavior();
Console.WriteLine("B operation");
}
void AddedBehavior()
{
}
}
}