设计模式学习总结(一)简单工厂模式
简单工厂模式不是23中设计模式中的一种,但它是我最早接触的一种设计模式!
一、示例展示:
通过学习及总结,以下是我做的一个简单工厂模式的示例:
1. 客户端调用工厂类角色(GetCarBrand)并传入客户需要的产品:
static void Main(string[] args) { Console.WriteLine("Please input the brand of the car that you want!"); string brand = Console.ReadLine(); Car car = GetCarBrand(brand); car.Run(); Console.ReadLine(); }
2. 工厂类角色(GetCarBrand)根据传入的参数,开始生成对应的具体产品类(Concrete Product):
//Factory class role(工厂类角色) public static Car GetCarBrand(string brand) { Car car = null; switch(brand) { case "Buick": car = new Buick(); break; case "Cadillac": car = new Cadillac(); break; case "Chevrolet": car = new Chevrolet(); break; } return car; }