下面例子中鼠标,键盘,耳麦为产品,惠普,戴尔为工厂
简单工厂模式
简单工厂模式不是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 asusKeyBo; 136 } 137 } 138 public abstract Mouse createMouse(); 139 public abstract KeyBoard createKeyBo(); 140 141 } 142 public class DellFactory : PCFactory 143 { 144 public override Mouse createMouse() 145 { 146 return PCFactory.DelMouse; 147 } 148 public override KeyBoard createKeyBo() 149 { 150 return PCFactory.DelKeyBo; 151 } 152 } 153 public class HPFactory : PCFactory 154 { 155 public override Mouse createMouse() 156 { 157 return PCFactory.HPMouse; 158 } 159 public override KeyBoard createKeyBo() 160 { 161 return PCFactory.HpKeyBo; 162 } 163 164 } 165 public class AsusFactory : PCFactory 166 { 167 public override Mouse createMouse() 168 { 169 return PCFactory.ASusMouse; 170 } 171 public override KeyBoard createKeyBo() 172 { 173 return PCFactory.ASusKeyBo; 174 } 175 } 176 177 }
【输出示例】
在抽象工厂模式中,假设我们需要增加一个产品
假设我们增加耳麦这个产品,则首先我们需要增加耳麦这个父类,再加上戴尔耳麦,惠普耳麦这两个子类。
之后在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 interface Mic 12 { 13 void sayHi(); 14 } 15 16 public class DellKeyBo : KeyBoard 17 { 18 public void sayHi() 19 { 20 Console.WriteLine("DellKeyBo says HI"); 21 } 22 } 23 public class HPKeyBo : KeyBoard 24 { 25 public void sayHi() 26 { 27 Console.WriteLine("HPKeyBo says hi"); 28 } 29 } 30 public class AsusKeyBo : KeyBoard 31 { 32 public void sayHi() 33 { 34 Console.WriteLine("AsusKeyBo says hi"); 35 } 36 } 37 38 public class DellMouse : Mouse 39 { 40 public void sayHi() 41 { 42 Console.WriteLine("DellMouse says HI"); 43 } 44 } 45 public class HpMouse : Mouse 46 { 47 public void sayHi() 48 { 49 Console.WriteLine("HPMouse says HI"); 50 } 51 } 52 public class AsusMouse : Mouse 53 { 54 public void sayHi() 55 { 56 Console.WriteLine("AsusMouse says HI"); 57 } 58 } 59 60 public class DellMic : Mic 61 { 62 public void sayHi() 63 { 64 Console.WriteLine("DellMic says Hello"); 65 } 66 } 67 public class HPMic : Mic 68 { 69 public void sayHi() 70 { 71 Console.WriteLine("HPMic says Hello"); 72 } 73 } 74 public class AsusMic : Mic 75 { 76 public void sayHi() 77 { 78 Console.WriteLine("AsusMic says hello"); 79 } 80 } 81 82 static void Main(string[] args) 83 { 84 PCFactory pcFac1 = new DellFactory(); 85 Mouse m1 = pcFac1.createMouse(); 86 KeyBoard k1 = pcFac1.createKeyBo(); 87 Mic mc1 = pcFac1.createMic(); 88 m1.sayHi(); 89 k1.sayHi(); 90 mc1.sayHi(); 91 92 Console.WriteLine(); 93 94 PCFactory pcFac2 = new HPFactory(); 95 Mouse m2 = pcFac2.createMouse(); 96 KeyBoard k2 = pcFac2.createKeyBo(); 97 Mic mc2=pcFac2.createMic(); 98 m2.sayHi(); 99 k2.sayHi(); 100 mc2.sayHi(); 101 102 Console.WriteLine(); 103 104 PCFactory pcFac3 = new AsusFactory(); 105 Mouse m3 = pcFac3.createMouse(); 106 KeyBoard k3 = pcFac3.createKeyBo(); 107 Mic mc3 = pcFac3.createMic(); 108 m3.sayHi(); 109 k3.sayHi(); 110 mc3.sayHi(); 111 112 Console.ReadLine(); 113 } 114 115 public abstract class PCFactory 116 { 117 //戴尔鼠标 118 private static DellMouse dellMouse = new DellMouse(); 119 public static DellMouse DelMouse 120 { 121 get 122 { 123 return dellMouse; 124 } 125 } 126 //惠普鼠标 127 private static HpMouse hpMouse = new HpMouse(); 128 public static HpMouse HPMouse 129 { 130 get 131 { 132 return hpMouse; 133 } 134 } 135 //华硕鼠标 136 private static AsusMouse asusMouse = new AsusMouse(); 137 public static AsusMouse ASusMouse 138 { 139 get 140 { 141 return asusMouse; 142 } 143 } 144 //戴尔键盘 145 private static DellKeyBo dellKeyBo = new DellKeyBo(); 146 public static DellKeyBo DelKeyBo 147 { 148 get 149 { 150 return dellKeyBo; 151 } 152 } 153 //惠普键盘 154 private static HPKeyBo hpKeyBo = new HPKeyBo(); 155 public static HPKeyBo HpKeyBo 156 { 157 get 158 { 159 return hpKeyBo; 160 } 161 } 162 //华硕键盘 163 private static AsusKeyBo asusKeyBo = new AsusKeyBo(); 164 public static AsusKeyBo ASusKeyBo 165 { 166 get 167 { 168 return asusKeyBo; 169 } 170 } 171 //戴尔耳麦 172 private static DellMic dellMic = new DellMic(); 173 public static DellMic DEllMic { get { return dellMic; } } 174 //惠普耳麦 175 private static DellMic hpMic = new DellMic(); 176 public static DellMic HpMic { get { return hpMic; } } 177 //华硕耳麦 178 private static DellMic asusMic = new DellMic(); 179 public static DellMic ASusMic { get { return asusMic; } } 180 181 public abstract Mouse createMouse(); 182 public abstract KeyBoard createKeyBo(); 183 184 public abstract Mic createMic(); 185 } 186 public class DellFactory : PCFactory 187 { 188 public override Mouse createMouse() 189 { 190 return PCFactory.DelMouse; 191 } 192 public override KeyBoard createKeyBo() 193 { 194 return PCFactory.DelKeyBo; 195 } 196 public override Mic createMic() 197 { 198 return PCFactory.DEllMic; 199 } 200 } 201 public class HPFactory : PCFactory 202 { 203 public override Mouse createMouse() 204 { 205 return PCFactory.HPMouse; 206 } 207 public override KeyBoard createKeyBo() 208 { 209 return PCFactory.HpKeyBo; 210 } 211 public override Mic createMic() 212 { 213 return PCFactory.HpMic; 214 } 215 216 } 217 public class AsusFactory : PCFactory 218 { 219 public override Mouse createMouse() 220 { 221 return PCFactory.ASusMouse; 222 } 223 public override KeyBoard createKeyBo() 224 { 225 return PCFactory.ASusKeyBo; 226 } 227 public override Mic createMic() 228 { 229 return PCFactory.ASusMic; 230 } 231 } 232 233 }
【输出示例】
【C#真实项目的工厂类示例】
》》工厂类
》》OrderRepository相当于 Mouse,KeyBo,Mic其中一种
==》OrderRepository 继承自IOrderRepository ,OrderRepository里面涉及到多种sayHi的方法;其他产品类和OrderRepository类似。
工厂方法为系统结构提供了非常灵活强大的动态扩展机制,只要我们更换一下具体的工厂方法,系统其他地方无需一点变换,就有可能将系统功能进行改头换面的变化。
工厂类一般用于使用针对接口编程和依赖倒置原则的地方,其他地方,如果代码调用的都是具体类的方法,就无需使用工厂类,直接new就行。
“需求总是变化”、“世界上没有一个软件是不变的”,这些言论是对软件需求最经典的表白。从中透射出一个关键的意思就是,对于软件设计者来说,必须在不需要对原有的
系统进行修改的情况下,实现灵活的系统扩展,这也是工厂模式的终极目标。希望本篇文章能对你有所帮助,觉得好的可以在右下角点个赞。
代码示例:
- interface IProduct1 {
- public void show();
- }
- interface IProduct2 {
- public void show();
- }
- class Product1 implements IProduct1 {
- public void show() {
- System.out.println("这是1型产品");
- }
- }
- class Product2 implements IProduct2 {
- public void show() {
- System.out.println("这是2型产品");
- }
- }
- interface IFactory {
- public IProduct1 createProduct1();
- public IProduct2 createProduct2();
- }
- class Factory implements IFactory{
- public IProduct1 createProduct1() {
- return new Product1();
- }
- public IProduct2 createProduct2() {
- return new Product2();
- }
- }
- public class Client {
- public static void main(String[] args){
- IFactory factory = new Factory();
- factory.createProduct1().show();
- factory.createProduct2().show();
- }
- }
原文链接地址:http://ichennan.com/2016/08/09/DesignPattern.html