简单工厂模式 工厂模式 抽象工厂模式
Posted 老大对不起八戒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单工厂模式 工厂模式 抽象工厂模式相关的知识,希望对你有一定的参考价值。
下面例子中鼠标,键盘,耳麦为产品,惠普,戴尔为工厂
简单工厂模式
简单工厂模式不是23种里的一种,简而言之,就是有一个专门生产某个产品的类。
比如下图中的鼠标工厂,专业生产鼠标,给参数0,生产戴尔鼠标,给参数1,生产惠普鼠标。
【C#代码示例】
1 public interface Mouse 2 { 3 void sayHi(); 4 } 5 static void Main(string[] args) 6 { 7 Mouse m1 = MouseFactory.createMouse(0); 8 m1.sayHi(); 9 Mouse m2 = MouseFactory.createMouse(1); 10 m2.sayHi(); 11 Console.ReadLine(); 12 } 13 14 public static class MouseFactory 15 { 16 private static DellMouse dellMouse = new DellMouse(); 17 public static DellMouse DelMouse 18 { 19 get 20 { 21 return dellMouse; 22 } 23 } 24 private static HpMouse hpMouse = new HpMouse(); 25 public static HpMouse HPMouse 26 { 27 get 28 { 29 return hpMouse; 30 } 31 } 32 33 public static Mouse createMouse(int i) 34 { 35 switch (i) 36 { 37 case 0: 38 return DelMouse; 39 case 1: 40 return HPMouse; 41 default: 42 return null; 43 } 44 45 } 46 47 public class DellMouse : Mouse 48 { 49 public void sayHi() 50 { 51 Console.WriteLine("DellMouse says HI"); 52 } 53 } 54 public class HpMouse : Mouse 55 { 56 public void sayHi() 57 { 58 Console.WriteLine("HPMouse says HI"); 59 } 60 } 61 }
【C#输出示例】
工厂模式
工厂模式也就是鼠标工厂是个父类,有生产鼠标这个接口。
戴尔鼠标工厂,惠普鼠标工厂继承它,可以分别生产戴尔鼠标,惠普鼠标。
生产哪种鼠标不再由参数决定,而是创建鼠标工厂时,由戴尔鼠标工厂创建。
后续直接调用鼠标工厂.生产鼠标()即可
【C#代码示例】
1 class Program 2 { 3 public interface Mouse 4 { 5 void sayHi(); 6 } 7 8 public class DellMouse : Mouse 9 { 10 public void sayHi() 11 { 12 Console.WriteLine("DellMouse says HI"); 13 } 14 } 15 public class HpMouse : Mouse 16 { 17 public void sayHi() 18 { 19 Console.WriteLine("HPMouse says HI"); 20 } 21 } 22 static void Main(string[] args) 23 { 24 MouseFactory mf1 = new DellFactory(); 25 Mouse m1 = mf1.createMouse(); 26 m1.sayHi(); 27 28 MouseFactory mf2 = new HPFactory(); 29 Mouse m2 = mf2.createMouse(); 30 m2.sayHi(); 31 32 Console.ReadLine(); 33 } 34 35 public abstract class MouseFactory 36 { 37 private static DellMouse dellMouse = new DellMouse(); 38 public static DellMouse DelMouse 39 { 40 get 41 { 42 return dellMouse; 43 } 44 } 45 private static HpMouse hpMouse = new HpMouse(); 46 public static HpMouse HPMouse 47 { 48 get 49 { 50 return hpMouse; 51 } 52 } 53 54 public abstract Mouse createMouse(); 55 56 } 57 public class DellFactory:MouseFactory 58 { 59 public override Mouse createMouse() 60 { 61 return MouseFactory.DelMouse; 62 } 63 } 64 public class HPFactory : MouseFactory 65 { 66 public override Mouse createMouse() 67 { 68 return MouseFactory.HPMouse; 69 } 70 } 71 72 }
【输出示例】
示例同上
抽象工厂模式
抽象工厂模式也就是不仅生产鼠标,同时生产键盘。
也就是PC厂商是个父类,有生产鼠标,生产键盘两个接口。
戴尔工厂,惠普工厂继承它,可以分别生产戴尔鼠标+戴尔键盘,和惠普鼠标+惠普键盘。
创建工厂时,由戴尔工厂创建。
后续工厂.生产鼠标()则生产戴尔鼠标,工厂.生产键盘()则生产戴尔键盘。
【C#代码示例】
1 class Program 2 { 3 public interface Mouse 4 { 5 void sayHi(); 6 } 7 public interface KeyBoard 8 { 9 void sayHi(); 10 } 11 public class DellKeyBo : KeyBoard 12 { 13 public void sayHi() 14 { 15 Console.WriteLine("DellKeyBo says HI"); 16 } 17 } 18 public class HPKeyBo : KeyBoard 19 { 20 public void sayHi() 21 { 22 Console.WriteLine("HPKeyBo says hi"); 23 } 24 } 25 26 public class DellMouse : Mouse 27 { 28 public void sayHi() 29 { 30 Console.WriteLine("DellMouse says HI"); 31 } 32 } 33 public class HpMouse : Mouse 34 { 35 public void sayHi() 36 { 37 Console.WriteLine("HPMouse says HI"); 38 } 39 } 40 static void Main(string[] args) 41 { 42 PCFactory pcFac1 = new DellFactory(); 43 Mouse m1 = pcFac1.createMouse(); 44 KeyBoard k1 = pcFac1.createKeyBo(); 45 m1.sayHi(); 46 k1.sayHi(); 47 48 Console.WriteLine(); 49 50 PCFactory pcFac2 = new DellFactory(); 51 Mouse m2 = pcFac2.createMouse(); 52 KeyBoard k2= pcFac2.createKeyBo(); 53 m2.sayHi(); 54 k2.sayHi(); 55 56 Console.ReadLine(); 57 } 58 59 public abstract class PCFactory 60 { 61 //戴尔鼠标 62 private static DellMouse dellMouse = new DellMouse(); 63 public static DellMouse DelMouse 64 { 65 get 66 { 67 return dellMouse; 68 } 69 } 70 //惠普鼠标 71 private static HpMouse hpMouse = new HpMouse(); 72 public static HpMouse HPMouse 73 { 74 get 75 { 76 return hpMouse; 77 } 78 } 79 //戴尔键盘 80 private static DellKeyBo dellKeyBo = new DellKeyBo(); 81 public static DellKeyBo DelKeyBo 82 { 83 get 84 { 85 return dellKeyBo; 86 } 87 } 88 //惠普键盘 89 private static HPKeyBo hpKeyBo = new HPKeyBo(); 90 public static HPKeyBo HpKeyBo 91 { 92 get 93 { 94 return hpKeyBo; 95 } 96 } 97 98 public abstract Mouse createMouse(); 99 public abstract KeyBoard createKeyBo(); 100 } 101 public class DellFactory : PCFactory 102 { 103 public override Mouse createMouse() 104 { 105 return PCFactory.DelMouse; 106 } 107 public override KeyBoard createKeyBo() 108 { 109 return PCFactory.DelKeyBo; 110 } 111 } 112 public class HPFactory : PCFactory 113 { 114 public override Mouse createMouse() 115 { 116 return PCFactory.HPMouse; 117 } 118 public override KeyBoard createKeyBo() 119 { 120 return PCFactory.HpKeyBo; 121 } 122 123 } 124 125 }
【输出示例】
在抽象工厂模式中,假设我们需要增加一个工厂
假设我们增加华硕工厂,则我们需要增加华硕工厂,和戴尔工厂一样,继承PC厂商。
之后创建华硕鼠标,继承鼠标类。创建华硕键盘,继承键盘类。
即可。
【C#代码示例】
1 class Program 2 { 3 public interface Mouse 4 { 5 void sayHi(); 6 } 7 public interface KeyBoard 8 { 9 void sayHi(); 10 } 11 public class DellKeyBo : KeyBoard 12 { 13 public void sayHi() 14 { 15 Console.WriteLine("DellKeyBo says HI"); 16 } 17 } 18 public class HPKeyBo : KeyBoard 19 { 20 public void sayHi() 21 { 22 Console.WriteLine("HPKeyBo says hi"); 23 } 24 } 25 public class AsusKeyBo : KeyBoard 26 { 27 public void sayHi() 28 { 29 Console.WriteLine("AsusKeyBo says hi"); 30 } 31 } 32 33 public class DellMouse : Mouse 34 { 35 public void sayHi() 36 { 37 Console.WriteLine("DellMouse says HI"); 38 } 39 } 40 public class HpMouse : Mouse 41 { 42 public void sayHi() 43 { 44 Console.WriteLine("HPMouse says HI"); 45 } 46 } 47 public class AsusMouse : Mouse 48 { 49 public void sayHi() 50 { 51 Console.WriteLine("AsusMouse says HI"); 52 } 53 } 54 static void Main(string[] args) 55 { 56 PCFactory pcFac1 = new DellFactory(); 57 Mouse m1 = pcFac1.createMouse(); 58 KeyBoard k1 = pcFac1.createKeyBo(); 59 m1.sayHi(); 60 k1.sayHi(); 61 62 Console.WriteLine(); 63 64 PCFactory pcFac2 = new HPFactory(); 65 Mouse m2 = pcFac2.createMouse(); 66 KeyBoard k2= pcFac2.createKeyBo(); 67 m2.sayHi(); 68 k2.sayHi(); 69 70 Console.WriteLine(); 71 72 PCFactory pcFac3 = new AsusFactory(); 73 Mouse m3 = pcFac3.createMouse(); 74 KeyBoard k3 = pcFac3.createKeyBo(); 75 m3.sayHi(); 76 k3.sayHi(); 77 78 79 Console.ReadLine(); 80 } 81 82 public abstract class PCFactory 83 { 84 //戴尔鼠标 85 private static DellMouse dellMouse = new DellMouse(); 86 public static DellMouse DelMouse 87 { 88 get 89 { 90 return dellMouse; 91 } 92 } 93 //惠普鼠标 94 private static HpMouse hpMouse = new HpMouse(); 95 public static HpMouse HPMouse 96 { 97 get 98 { 99 return hpMouse; 100 } 101 } 102 //华硕鼠标 103 private static AsusMouse asusMouse = new AsusMouse(); 104 public static AsusMouse ASusMouse 105 { 106 get 107 { 108 return asusMouse; 109 } 110 } 111 //戴尔键盘 112 private static DellKeyBo dellKeyBo = new DellKeyBo(); 113 public static DellKeyBo DelKeyBo 114 { 115 get 116 { 117 return dellKeyBo; 118 } 119 } 120 //惠普键盘 121 private static HPKeyBo hpKeyBo = new HPKeyBo(); 122 public static HPKeyBo HpKeyBo 123 { 124 get 125 { 126 return hpKeyBo; 127 } 128 } 129 //华硕键盘 130 private static AsusKeyBo asusKeyBo = new AsusKeyBo(); 131 public static AsusKeyBo ASusKeyBo 132 { 133 get 134 { 135 return asusK以上是关于简单工厂模式 工厂模式 抽象工厂模式的主要内容,如果未能解决你的问题,请参考以下文章