public class Program { static void Main(string[] args) { Console.WriteLine("类适配器模式测试:"); ITarget target = new ClassAdapter(); target.Request(); Console.ReadLine(); } }
public interface ITarget { void Request(); }
public class Adaptee { public void SpecificRequest() { Console.WriteLine("适配者中的业务代码被调用!"); } }
public class ClassAdapter : Adaptee,ITarget { public void Request() { SpecificRequest(); } }
public class Program { static void Main(string[] args) { Console.WriteLine("类适配器模式测试:"); Adaptee adaptee = new Adaptee(); ITarget target = new ObjectAdapter(adaptee); target.Request(); Console.ReadLine(); } }
class ObjectAdapter : ITarget { private Adaptee adaptee; public ObjectAdapter(Adaptee adaptee) { this.adaptee=adaptee; } public void Request() { adaptee.SpecificRequest(); } }
public abstract class Mediator { public abstract void Register(Colleague colleague); public abstract void Relay(Colleague colleague); }
public class ConcreteMediator : Mediator { private List<Colleague> colleagues=new List<Colleague>(); public override void Register(Colleague colleague) { if(!colleagues.Contains(colleague)) { colleagues.Add(colleague); colleague.SetMediator(this); } } public override void Relay(Colleague colleague) { foreach (var item in colleagues) { if (!item.Equals(colleague)) { item.Receive(); } } } }
public abstract class Colleague { protected Mediator mediator; public void SetMediator(Mediator mediator) { this.mediator=mediator; } public abstract void Receive(); public abstract void Send(); }
public class ConcreteColleague1 : Colleague { public override void Receive() { Console.WriteLine("具体同事类1收到请求。"); } public override void Send() { Console.WriteLine("具体同事类1发出请求。"); mediator.Relay(this); } }
public class ConcreteColleague2 : Colleague { public override void Receive() { Console.WriteLine("具体同事类2收到请求。"); } public override void Send() { Console.WriteLine("具体同事类2发出请求。"); mediator.Relay(this); } }
public interface IAbstractCommand { void Execute(); }
public class ConcreteCommand1 : IAbstractCommand { private CompositeReceiver receiver; public ConcreteCommand1() { receiver=new CompositeReceiver(); } public void Execute() { receiver.Action1(); } }
public class ConcreteCommand2 : IAbstractCommand { private CompositeReceiver receiver; public ConcreteCommand2() { receiver=new CompositeReceiver(); } public void Execute() { receiver.Action2(); } }
public class CompositeInvoker : IAbstractCommand { private List<IAbstractCommand> children = new List<IAbstractCommand>(); public void Add(IAbstractCommand c) { children.Add(c); } public void Remove(IAbstractCommand c) { children.Remove(c); } public IAbstractCommand GetChild(int i) { return children[i]; } public void Execute() { foreach (var child in children) { child.Execute(); } } }
public class CompositeReceiver { public void Action1() { Console.WriteLine("接收者的Action1()方法被调用..."); } public void Action2() { Console.WriteLine("接收者的Action2()方法被调用..."); } }
class Program { static void Main(string[] args) { AbstractClass tm=new ConcreteClass(); tm.TemplateMethod(); Console.Read(); } }
public abstract class AbstractClass { public void TemplateMethod() { SpecificMethod(); AbstractMethod1(); AbstractMethod2(); } public void SpecificMethod() { Console.WriteLine("抽象类中的具体方法被调用..."); } public abstract void AbstractMethod1(); public abstract void AbstractMethod2(); }
public class ConcreteClass : AbstractClass { public override void AbstractMethod1() { Console.WriteLine("抽象方法1的实现被调用..."); } public override void AbstractMethod2() { Console.WriteLine("抽象方法2的实现被调用..."); } }
class Program { static void Main(string[] args) { Context context=new Context(); context.Handle(); context.Handle(); context.Handle(); context.Handle(); Console.Read(); } }
public class Context { private State state; public Context() { this.state=new ConcreteStateA(); } public void SetState(State state) { this.state=state; } public State GetState() { return(state); } public void Handle() { state.Handle(this); } }
public abstract class State { public abstract void Handle(Context context); }
public class ConcreteStateA : State { public override void Handle(Context context) { Console.WriteLine("当前状态是 A."); context.SetState(new ConcreteStateB()); } }
public class ConcreteStateB : State { public override void Handle(Context context) { Console.WriteLine("当前状态是 B."); context.SetState(new ConcreteStateA()); } }
class Program { static void Main(string[] args) { LocationTracker provider = new LocationTracker(); LocationReporter reporter1 = new LocationReporter("FixedGPS"); reporter1.Subscribe(provider); LocationReporter reporter2 = new LocationReporter("MobileGPS"); reporter2.Subscribe(provider);
public virtual void Unsubscribe() { unsubscriber.Dispose(); } }
public class LocationTracker : IObservable<Location> { public LocationTracker() { observers = new List<IObserver<Location>>(); }
private List<IObserver<Location>> observers;
public IDisposable Subscribe(IObserver<Location> observer) { if (!observers.Contains(observer)) observers.Add(observer); return new Unsubscriber(observers, observer); }