可以用抽象工厂模式来造车。
车的品牌有很多,而且车的属性也不少,比如车的类型、排量、门的数量,等等。可以提炼出有关车的一个抽象类:
public abstract class Car{public string Model { get; set; }public string Engine { get; set; }public string Transmission { get; set; }public string Body { get; set; }public int Doors { get; set; }public List<string> accessores = new List<string>();public abstract void ShowCarInfo();}
既然是抽象工厂,提炼出一个造车的接口:
public interface ICarFactory{Car PRoduceCar();}
写一个Car的派生类:
public class MyCar : Car{public MyCar(){Model = "型号001";Engine = "发动机V";Transmission = "排量1";Body = "SUV";Doors = 4;Accessores.Add("内饰");Accessores.Add("外饰");}public override void ShowCarInfo(){Console.WriteLine(Model);Console.WriteLine(Engine);Console.WriteLine(Body);Console.WriteLine("门数量:" + Doors);Console.WriteLine("包括:");foreach (var accessory in Accessores){Console.WriteLine("/t{0}", accessory);}}}
MyCar需要一个对应的具体工厂来造车。写一个具体工厂类,实现ICarFactory接口。
新闻热点
疑难解答