这个是《大话设计模式》中的一些简单例子,
简单易懂,所以整理下来,方便大家查看。其实我们大多只要熟悉几种设计模式就好了。
public static void Main(string[] args)
{
//Test简单工厂();
//Test策略模式();
//Test装饰模式();
//Test代理模式();
//Test工厂方法();
Console.Read();
}
#region 模式-简单工厂
public abstract class Operation
{
public double NumA { get; set; }
public double NumB { get; set; }
public abstract double GetResult();
}
public class Add : Operation
{
public override double GetResult()
{
return NumA + NumB;
}
}
public class Mul : Operation
{
public override double GetResult()
{
return NumA - NumB;
}
}
public class OperationFactory
{
public static Operation CreateOperation(string operation)
{
Operation o = null;
switch (operation)
{
case "+": o = new Add(); break;
case "-": o = new Mul(); break;
}
return o;
}
}
public static void Test简单工厂()
{
Operation o = OperationFactory.CreateOperation("+");
o.NumA = double.Parse(Console.ReadLine());
o.NumB = double.Parse(Console.ReadLine());
double result = o.GetResult();
Console.WriteLine(result);
}
#endregion
#region 模式-策略模式
//抽象算法
public abstract class Strategy
{
//算法方法
public abstract void AlgorithmInterface();
}
public class ConcreteStrategyA : Strategy
{
public override void AlgorithmInterface()
{
Console.WriteLine("A");
}
}
public class ConcreteStrategyB : Strategy
{
public override void AlgorithmInterface()
{
Console.WriteLine("B");
}
}
public class ConcreteStrategyC : Strategy
{
public override void AlgorithmInterface()
{
Console.WriteLine("C");
}
}
public class Context
{
Strategy strategy = null;
public Context(Strategy strategy)
{
this.strategy=strategy;
}
public void ConcreteStrategy()
{
strategy.AlgorithmInterface();
}
}
public static void Test策略模式()
{
Context c = new Context(new ConcreteStrategyA());
c.ConcreteStrategy();
c = new Context(new ConcreteStrategyC());
c.ConcreteStrategy();
}
#endregion
#region 模式-装饰模式
abstract class Component
{
public abstract void Operation();
}
class ConcreteComponent : Component
{
public override void Operation()
{
Console.WriteLine("具体对象的操作");
}
}
abstract class Decorator : Component
{
protected Component component;
public void SetComponent(Component component)
{
this.component = component;
}
public override void Operation()
{
if (component != null)
{
component.Operation();
}
}
}
class ConcreteDecoratorA : Decorator
{
private string addedState;
public override void Operation()
{
base.Operation();
addedState = "New State";
Console.WriteLine("具体装饰对象A的操作");
}
}
class ConcreteDecoratorB : Decorator
{
public override void Operation()
{
base.Operation();
AddBehavior();
Console.WriteLine("具体装饰对象B的操作");
}
public void AddBehavior()
{
}
}
public static void Test装饰模式()
{
ConcreteComponent c = new ConcreteComponent();
ConcreteDecoratorA d1 = new ConcreteDecoratorA();
ConcreteDecoratorB d2 = new ConcreteDecoratorB();
d1.SetComponent(c);
d2.SetComponent(d1);
d2.Operation();
}
#endregion
#region 模式-代理模式
abstract class Subject
{
public abstract void Request();
}
class RealSubject : Subject
{
public override void Request()
{
Console.WriteLine("真实请求");
}
}
class Proxy : Subject
{
RealSubject realSubject;
public override void Request()
{
if (realSubject == null)
realSubject = new RealSubject();
realSubject.Request();
}
}
public static void Test代理模式()
{
Proxy proxy = new Proxy();
proxy.Request();
Console.ReadLine();
}
#endregion
#region 模式-工厂方法
//沿用简单工厂抽象类
interface IFactory
{
Operation CreateOperation();
}
class AddFactory : IFactory
{
public Operation CreateOperation()
{
return new Add();
}
}
class MulFactory : IFactory
{
public Operation CreateOperation()
{
return new Mul();
}
}
public static void Test工厂方法()
{
IFactory operFactory = new AddFactory();
Operation oper = operFactory.CreateOperation();
oper.NumA = 1;
oper.NumB = 2;
double result = oper.GetResult();
Console.WriteLine(result);
}
#endregion
#region 模式-原型模式
#endregion