简单工厂 简单工厂不是GoF 23种设计模式中的一员,但是比较常见。将产品抽象出一个接口或基类。然后通过传递不同参数生产不同对象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 class Program { static void Main (string [] args ) { BallFactory factory = new BallFactory(); factory.Create("football" ); factory.Create("basketball" ); } } public interface IBall { }public class FootBall : IBall { public FootBall () { Console.WriteLine("Create football" ); } } public class Basketball : IBall { public Basketball () { Console.WriteLine("Create basketball" ); } } public class BallFactory { public IBall Create (string ballType ) { IBall result = null ; if (ballType == "football" ) result = new FootBall(); else if (ballType == "basketball" ) result = new Basketball(); return result; } }
问题 如果要新增一个产品排球(baseball),那就要修改BallFactory
这个类,增加一个else if
判断,这样就违背了开放-封闭原则
。那我们就可以将工厂抽离出来,需要什么样的球就去调用对用的工厂生产,这就是工厂模式
了。
工厂模式 定义一个用于创建对象的接口(IFactory), 由其子类决定创建实例。工厂模式将类的实例化延迟到其子类中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 class Program { static void Main (string [] args ) { IFactory basketBallFactory = new BasketBallFactory(); IBall basketball = basketBallFactory.Create(); IFactory footBallFactory = new FootBallFactory(); IBall football = footBallFactory.Create(); } } public interface IBall { }public class FootBall : IBall { public FootBall () { Console.WriteLine("Create football" ); } } public class Basketball : IBall { public Basketball () { Console.WriteLine("Create basketball" ); } } public interface IFactory { IBall Create () ; }public class BasketBallFactory : IFactory { public IBall Create () { return new Basketball(); } } public class FootBallFactory : IFactory { public IBall Create () { return new FootBall(); } }
问题 如果产品更多呢?乒乓球,羽毛球…,总不能每增加一种球就新建一个工厂吧。这就可以将产品,工厂抽象出来:A工厂可以生产篮球,也可以生产排球,羽毛球等等,当然B工厂也可以。A,B工厂生产的各种球可能外观,耐用程度等属性不同,但是都可以用来打。这样根据不同的工厂去分别生产对应不同的产品。这就是抽象工厂
。
抽象工厂 提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 class Program { static void Main (string [] args ) { IFactory factoryA = new FactoryA(); factoryA.CreateBasketball(); factoryA.CreateFootBall(); IFactory factoryB = new FactoryB(); factoryB.CreateBasketball(); factoryB.CreateFootBall(); } } public interface IFootBall { }public class FootBallA :IFootBall { public FootBallA () { Console.WriteLine("Create football by A factory" ); } } public class FootBallB : IFootBall { public FootBallB () { Console.WriteLine("Create football by B factory" ); } } public interface IBasketBall { }public class BasketballA :IBasketBall { public BasketballA () { Console.WriteLine("Create basketball by A Factory" ); } } public class BasketballB : IBasketBall { public BasketballB () { Console.WriteLine("Create basketball by B Factory" ); } } public interface IFactory { IBasketBall CreateBasketball () ; IFootBall CreateFootBall () ; } public class FactoryA : IFactory { public IBasketBall CreateBasketball () { return new BasketballA(); } public IFootBall CreateFootBall () { return new FootBallA(); } } public class FactoryB : IFactory { public IBasketBall CreateBasketball () { return new BasketballB(); } public IFootBall CreateFootBall () { return new FootBallB(); } }