设计模式-抽象工厂模式
https://mp.weixin.qq.com/s/n0pBULUsE3MKN07rVPaJAA
using System; namespace AbstractFactory_DesignPattern { /// <summary> /// 公司接口 /// </summary> public interface Company { /// <summary> /// 生产电脑 /// </summary> /// <param name="Parameter"></param> /// <returns></returns> Computer BulidComputer(String Parameter); /// <summary> /// 生产电话 /// </summary> /// <param name="Parameter"></param> /// <returns></returns> Telephone BulidTelephone(String Parameter); } /// <summary> /// 电脑 /// </summary> public abstract class Computer { /// <summary> /// 使用电脑 /// </summary> abstract public void DoUse(); } /// <summary> /// 电话 /// </summary> public abstract class Telephone { /// <summary> /// 使用手机 /// </summary> abstract public void DoUse(); } /// <summary> /// 公司A /// </summary> public class CompanyA : Company { public Computer BulidComputer(String Parameter) { if (Parameter == "个人电脑") return new PersonalComputer(); else if (Parameter == "笔记本电脑") return new NotebookComputer(); else return null; } public Telephone BulidTelephone(String Parameter) { if (Parameter == "座机电话") return new DesktopPhone(); else if (Parameter == "手机") return new Mobile(); else return null; } } public class CompanyB : Company { public Computer BulidComputer(String Parameter) { if (Parameter == "个人电脑") return new PersonalComputer(); else if (Parameter == "笔记本电脑") return new NotebookComputer(); else return null; } public Telephone BulidTelephone(String Parameter) { if (Parameter == "座机") return new DesktopPhone(); else if (Parameter == "手机") return new Mobile(); else return null; } } /// <summary> /// 台机 /// </summary> public class DesktopPhone : Telephone { override public void DoUse() { Console.WriteLine("这是座机电话的功能"); } } /// <summary> /// 手机 /// </summary> public class Mobile : Telephone { override public void DoUse() { Console.WriteLine("这是手机的功能"); } } public class NotebookComputer : Computer { override public void DoUse() { Console.WriteLine("这是笔记本电脑的功能"); } } public class PersonalComputer : Computer { override public void DoUse() { Console.WriteLine("这是个人计算机的功能"); } } public class Client { public static void Main(String[] args) { Company company1 = new CompanyA(); Computer computer1 = company1.BulidComputer("个人电脑"); computer1.DoUse(); Computer computer2 = company1.BulidComputer("笔记本电脑"); computer2.DoUse(); Telephone telephone1 = company1.BulidTelephone("座机电话"); telephone1.DoUse(); Telephone telephone2 = company1.BulidTelephone("手机"); telephone2.DoUse(); } } }