直接上代码:
internal class Program { private static void Main(string[] args) { //简单工厂模式 if (false) { Operate op = SimpleFactory.CreateOperate("+"); op.NumberA = 20; op.NumberB = 30; var result = op.GetResult(); Console.WriteLine(result); } //策略模式 if (false) { StrategyPattern sp = new StrategyPattern("-"); var result = sp.GetResult(30, 20); Console.WriteLine(result); } //装饰模式 if (false) { OuStyle os = new OuStyle(); os.AddStyle(new PingFang()); os.Show(); // 欧式风格 这是平房 ChineseStyle cs = new ChineseStyle(); cs.AddStyle(os); //用欧式风格装饰中国风 cs.Show();// 中国分隔 欧式风格 这是平房 } if (false) { IGiveGifts sender = new Persuit(); Agent agent = new Agent(sender); agent.GiveDolls(); } if (true) { IFactory factory = new AddOperateFactory(); Operate op= factory.CreateOperate(); op.NumberA = 20; op.NumberB = 30; Console.WriteLine(op.GetResult()); } Console.ReadKey(); } } #region 1、简单工厂模式 public abstract class Operate { public int NumberA { get; set; } public int NumberB { get; set; } public virtual int GetResult() { return 0; } } internal class AddOperate : Operate { public override int GetResult() { return this.NumberA + this.NumberB; } } internal class SubOperate : Operate { public override int GetResult() { return this.NumberA - this.NumberB; } } /// <summary> /// 1、简单工厂模式 /// </summary> internal class SimpleFactory { public static Operate CreateOperate(string flag) { switch (flag) { case "+": return new AddOperate(); break; case "-": return new SubOperate(); break; default: return null; } } } #endregion #region 2、策略模式 /// <summary> /// 在简单工厂模式的基础上,在工厂(类)中,对对象方法进行了再封装 而不是返回对象 /// </summary> internal class StrategyPattern { private Operate op = null; public StrategyPattern(string flag) { switch (flag) { case"+": op = new AddOperate(); break; case"-": op = new SubOperate(); break; default: op = null; break; } } public int GetResult(int a,int b) { op.NumberA = a; op.NumberB = b; return op.GetResult(); } } #endregion #region 3、装饰模式 /// <summary> /// 抽象接口 /// </summary> internal abstract class House { public abstract void Show(); } internal class PingFang : House { public override void Show() { Console.WriteLine("这是平房"); } } internal class LouFang : House { public override void Show() { Console.WriteLine("这是楼房"); } } /// <summary> /// 装饰模式:在当前类中定义继承的类成员,再加个初始该成员的方法,用实现继承类的其它类对象来武装装饰自己 /// </summary> internal class OuStyle:House { /// <summary> /// 将定义用来装饰的对象方法的接口 定义在当前(需要装饰的)类中 /// </summary> private House home = null; public override void Show() { if (null != this.home) { home.Show(); } Console.WriteLine("欧式风格"); } /// <summary> /// 动态添加特定的实现了接口House的类,以便给本类来调用其方法来装饰(武装)自己, /// </summary> /// <param name="h"></param> public void AddStyle(House h) { this.home = h; } } internal class ChineseStyle : House { private House h = null; public override void Show() { if (null!=h) { h.Show(); } Console.WriteLine("这是中国风"); } public void AddStyle(House house) { this.h = house; } } #endregion #region 4、代理模式 public interface IGiveGifts { void GiveDolls(); void GiveFlowers(); } public class Persuit : IGiveGifts { public void GiveDolls() { Console.WriteLine("送芭比娃娃"); } public void GiveFlowers() { Console.WriteLine("送花"); } } /// <summary> /// 什么是代理模式:本来有一个类A可以直接执行自己的方法就可以实现一个功能,现在先将这个类A作为一个属性传递给一个代理类,代理类在通过自己的方法调用A对象的方法,同时可以添加一些新的功能 /// </summary> public class Agent : IGiveGifts { private IGiveGifts sender; public Agent(IGiveGifts ig) { this.sender = ig; } public void GiveDolls() { sender.GiveDolls(); Console.WriteLine("我是**代理 送芭比娃娃"); } public void GiveFlowers() { sender.GiveFlowers(); Console.WriteLine("我是**代理 送花"); } } #endregion #region 5、工厂方法模式 /// <summary> /// 工厂模式存在类与switch语句的高耦合,增加新的类 需要去增加case分支,违背了开放-封闭原则 工厂方法模式可以解决这个问题。 /// </summary> public interface IFactory { Operate CreateOperate(); } public class AddOperateFactory:IFactory { public Operate CreateOperate() { return new AddOperate(); } } public class SubOperateFactory : IFactory { public Operate CreateOperate() { return new SubOperate(); } } #endregion
时间: 2024-10-21 18:47:04