在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
产品抽象产品一 //鼠标抽象类 public abstract class Mouse { public abstract void Print(); } 具体产品 //联想鼠标 public class LenovoMouse : Mouse { public override void Print() { Console.WriteLine("生产了一个联想鼠标"); } } //惠普鼠标 public class HPMouse : Mouse { public override void Print() { Console.WriteLine("生产了一个惠普鼠标"); } } 抽象产品二 //键盘抽象类 public abstract class Keybo { public abstract void Print(); } 具体产品 //惠普键盘 public class HpKeybo : Keybo { public override void Print() { Console.WriteLine("生产了一个惠普键盘!"); } } //戴尔键盘类 public class DellKeybo : Keybo { public override void Print() { Console.WriteLine("生产了一个戴尔键盘!"); } } 工厂抽象工厂 /// <summary> /// Pc产品工厂抽象类 /// </summary> public abstract class PcFactory { public abstract Mouse CreateMouse(); public abstract Keybo CreateKeybo(); } 具体工厂 //戴尔pc工厂 public class DellPcFactroy : PcFactory { public override Keybo CreateKeybo() { return new DellKeybo(); } public override Mouse CreateMouse() { return new DellMouse(); } } //惠普pc工厂 public class HpPcFactory : PcFactory { public override Mouse CreateMouse() { return new HPMouse(); } public override Keybo CreateKeybo() { return new HpKeybo(); } } 运行测试//生产一个戴尔鼠标 MouseFactory dellMouseFactory = new DellMouseFactroy(); _04工厂模式.Mouse dellMouse = dellMouseFactory.CreateMouse(); dellMouse.Print(); //生产一个惠普鼠标 MouseFactory hpMouseFactory = new HpMouseFactory(); _04工厂模式.Mouse hpMouse = hpMouseFactory.CreateMouse(); hpMouse.Print(); //生产一个联想鼠标 LenovoMouseFactory lenovoMouseFactory = new LenovoMouseFactory (); _04工厂模式.Mouse lenovoMouse = lenovoMouseFactory.CreateMouse(); lenovoMouse.Print();
|
请发表评论