国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
《Head First設(shè)計(jì)模式》閱讀筆記.第四章
1.簡(jiǎn)單工廠(Simple Factory)部分

*針對(duì)接口編程可以隔離掉系統(tǒng)以后可能發(fā)生的一大堆改變。

*用靜態(tài)方法定義的工廠被成為靜態(tài)工廠,這樣就不用使用創(chuàng)建對(duì)象的方法來(lái)實(shí)例化對(duì)象,使用方便。但是這樣做的缺點(diǎn)是無(wú)法通過(guò)繼承來(lái)改變創(chuàng)建方法的行為。

*簡(jiǎn)單工廠不是一種設(shè)計(jì)模式,但是它比較常用。

2.工廠方法(Factory Method)模式部分

----芝加哥風(fēng)味匹薩店----
Java代碼
  1. public class ChicagoPizzaStore extends PizzaStore {   
  2.     Pizza createPizza(String item) {   
  3.         if ("cheese".equals(item)) {   
  4.             return ChicagoStyleCheesePizza();   
  5.         } else if ("veggie".equals(item)) {   
  6.             return ChicagoStyleVeggiePizza();   
  7.         } else if ("clam".equals(item)) {   
  8.             return ChicagoStyleClamPizza();   
  9.         } else if ("pepperoni".equals(item)) {   
  10.             return ChicagoStylePepperoniPizza();   
  11.         } else  
  12.             return null;   
  13.     }   
  14. }  

------------

----加州風(fēng)味匹薩店----
Java代碼
  1. public class CaliforniaPizzaStore extends PizzaStore {   
  2.     Pizza createPizza(String item) {   
  3.         if ("cheese".equals(item)) {   
  4.             return CaliforniaStyleCheesePizza();   
  5.         } else if ("veggie".equals(item)) {   
  6.             return CaliforniaStyleVeggiePizza();   
  7.         } else if ("clam".equals(item)) {   
  8.             return CaliforniaStyleClamPizza();   
  9.         } else if ("pepperoni".equals(item)) {   
  10.             return CaliforniaStylePepperoniPizza();   
  11.         } else  
  12.             return null;   
  13.     }   
  14. }  

------------

*工廠方法用來(lái)處理對(duì)象的創(chuàng)建,并將這樣的行為封裝在子類(lèi)中。這樣,客戶程序中關(guān)于超類(lèi)的代碼就和子類(lèi)對(duì)象的創(chuàng)建代碼解耦(Decouple)了。
工廠方法的定義:abstract Product factoryMethod(String type);

*所有工廠模式都用來(lái)封裝對(duì)象的創(chuàng)建。

*工廠方法模式(Factory Method Pattern)通過(guò)讓子類(lèi)來(lái)決定該創(chuàng)建的對(duì)象是什么,來(lái)達(dá)到將對(duì)象的創(chuàng)建過(guò)程封裝的目的。

*在工廠方法模式中包括創(chuàng)建者(Creator)類(lèi)和產(chǎn)品(Product)類(lèi)兩種類(lèi)型的類(lèi)。

----設(shè)計(jì)謎題解答----
引用
1、與NYPizzaStore和ChicagoPizzaStore平行的是CaliforniaPizzaStore類(lèi),繼承自PizzaStore類(lèi)。
2、與NYStyleCheesePizza和ChicagoStyleCheesePizza平行的是CaliforniaStyleCheesePizza、CaliforniaStylePepperoniPizza、CaliforniaStyleClamPizza、CaliforniaStyleVeggiePizza類(lèi),繼承自Pizza類(lèi),被CaliforniaPizzaStore類(lèi)所依賴。

------------

工廠(Factory Method Pattern)方法模式:定義了一個(gè)創(chuàng)建對(duì)象的接口,但是由子類(lèi)來(lái)決定要實(shí)例化的類(lèi)是哪一個(gè)。它讓類(lèi)把實(shí)例化推遲到了子類(lèi)。

*創(chuàng)建者(Creator)類(lèi)實(shí)現(xiàn)了所有操縱產(chǎn)品的方法,但是不實(shí)現(xiàn)工廠方法。

*工廠方法模式可以和策略(Strategy)模式結(jié)合起來(lái),在運(yùn)行時(shí)動(dòng)態(tài)地更換工廠類(lèi),從而創(chuàng)建不同的產(chǎn)品對(duì)象,這是簡(jiǎn)單工廠所不具有的彈性。

*工廠方法模式可以讓客戶在實(shí)例化對(duì)象時(shí),只依賴接口,而不依賴具體的實(shí)現(xiàn)類(lèi)。這符合“針對(duì)接口編程,而不是針對(duì)實(shí)現(xiàn)編程”的軟件設(shè)計(jì)原則。

*如果類(lèi)A的改變會(huì)影響到類(lèi)B,那么我們說(shuō)類(lèi)B“依賴于”類(lèi)A。

軟件設(shè)計(jì)原則:要依賴抽象,不要依賴具體類(lèi)。這個(gè)原則又被稱為“依賴倒置原則(Dependency Inversion Principle)”。

*依賴倒置原則說(shuō)明不能讓高層組件依賴于底層組件,而且它們都應(yīng)該依賴于抽象。

*遵循依賴倒置原則的三個(gè)指導(dǎo)方針:
(1)變量不可以持有具體類(lèi)的引用。這可以通過(guò)使用工廠避開(kāi)。
(2)不要讓類(lèi)派生自具體類(lèi)。否則就會(huì)依賴具體類(lèi),違反了“針對(duì)接口編程,而不是針對(duì)現(xiàn)實(shí)編程”的軟件設(shè)計(jì)原則。
(3)不要覆蓋基類(lèi)中已實(shí)現(xiàn)的方法。出現(xiàn)這樣的情況就說(shuō)明基類(lèi)設(shè)計(jì)的有問(wèn)題。

3.抽象工廠(Abstract Factory)模式部分

抽象工廠模式:提供一個(gè)接口,用于創(chuàng)建相關(guān)或者依賴對(duì)象的家族,而不需要明確指定具體類(lèi)。

*抽象工廠模式是工廠方法模式的演變。工廠方法模式中,創(chuàng)建者只生產(chǎn)一種類(lèi)型的產(chǎn)品,而抽象工廠模式中,創(chuàng)建者生產(chǎn)一組不同類(lèi)型的產(chǎn)品。

----ChicagoPizzaIngredientFactory代碼----
Java代碼
  1. public class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory {   
  2.     public Dough createDough() {   
  3.         return new ThickCrustDough();   
  4.     }   
  5.        
  6.     public Sause createSause() {   
  7.         return new  FlumTomatoSause();   
  8.     }   
  9.        
  10.     public Cheese createCheese() {   
  11.         return new Mozzarella();   
  12.     }   
  13.        
  14.     public Veggies[] createVeggies() {   
  15.         return new Beggies[] {new BlackOlives(), new Spinach(), new EggPlant()};   
  16.     }   
  17.        
  18.     public Pepperoni createPepperoni() {   
  19.         return new SlicedPepperoni();   
  20.     }   
  21.        
  22.     public Clam createClam() {   
  23.         return new FrozenClams();   
  24.     }   
  25. }  

------------

*抽象工廠(Abstract Factory)模式同時(shí)結(jié)合了工廠方法(Factory Method)模式和策略(Strategy)模式。

*工廠方法使用繼承:把對(duì)象的創(chuàng)建委托(Delegate)給子類(lèi),讓子類(lèi)實(shí)現(xiàn)工廠方法創(chuàng)建對(duì)象。

*抽象工廠使用對(duì)象的組合:對(duì)象的創(chuàng)建被實(shí)現(xiàn)在工廠接口所暴露出來(lái)的方法中。

*所有工廠模式都通過(guò)減少應(yīng)用程序和具體類(lèi)之間的依賴來(lái)促進(jìn)松耦合,即解耦(Decouple)。

4.簡(jiǎn)單工廠(Simple Factory)實(shí)例
Java代碼
  1. public abstract class Car {   
  2.     protected String name;// 名稱   
  3.   
  4.     protected String engines;// 發(fā)動(dòng)機(jī)   
  5.   
  6.     protected String wheels;// 車(chē)輪   
  7.   
  8.     protected String lamps;// 車(chē)燈   
  9.   
  10.     public void prepare() {   
  11.         System.out.println("Preparing " + name);   
  12.         System.out.println("Get engines ready...");   
  13.         System.out.println("Get wheels ready...");   
  14.         System.out.println("Get lamps ready...");   
  15.     }   
  16.   
  17.     // 組裝   
  18.     public void fabricate() {   
  19.         System.out.println("Adding engines.");   
  20.         System.out.println("Adding wheels.");   
  21.         System.out.println("Adding lamps.");   
  22.     }   
  23.   
  24.     // 檢測(cè)   
  25.     public void detect() {   
  26.         System.out.println("Detect the car.");   
  27.     }   
  28.   
  29.     public String getName() {   
  30.         return name;   
  31.     }   
  32. }   
  33.   
  34. public class BmwX3Car extends Car {   
  35.     public BmwX3Car() {   
  36.         name = "BMW X3";   
  37.         engines = "One Engine";   
  38.         wheels = "Four Wheels";   
  39.         lamps = "Two Lamps";   
  40.     }   
  41. }   
  42.   
  43. public class BmwX5Car extends Car {   
  44.     public BmwX5Car() {   
  45.         name = "BMW X5";   
  46.         engines = "Two Engines";   
  47.         wheels = "Four Wheels";   
  48.         lamps = "Four Lamps";   
  49.     }   
  50. }   
  51.   
  52. public class BmwX7Car extends Car {   
  53.     public BmwX7Car() {   
  54.         name = "BMW X7";   
  55.         engines = "Four Engine";   
  56.         wheels = "Four Wheels and One Spare Tyre";   
  57.         lamps = "Six Lamps";   
  58.     }   
  59. }   
  60.   
  61. public class SimpleCarFactory {   
  62.     // 簡(jiǎn)單工廠   
  63.     public Car createCar(String type) {   
  64.         if ("bmwx3".equals(type)) {   
  65.             return new BmwX3Car();   
  66.         } else if ("bmwx5".equals(type)) {   
  67.             return new BmwX5Car();   
  68.         } else if ("bmwx7".equals(type)) {   
  69.             return new BmwX7Car();   
  70.         } else  
  71.             return null;   
  72.     }   
  73. }   
  74.   
  75. // 汽車(chē)銷(xiāo)售店   
  76. public class CarSalesShop {   
  77.     public Car orderCar(String type) {   
  78.         SimpleCarFactory factory = new SimpleCarFactory();   
  79.         Car car = factory.createCar(type);   
  80.         car.prepare();   
  81.         car.fabricate();   
  82.         car.detect();   
  83.   
  84.         System.out.println("A " + car.getName() + " car is ready.");   
  85.         return car;   
  86.     }   
  87. }  


5.靜態(tài)工廠實(shí)例

在上例的基礎(chǔ)上實(shí)現(xiàn):
Java代碼
  1. public class StaticCarFactory {   
  2.     // 靜態(tài)工廠   
  3.     public static Car createCar(String type) {   
  4.         if ("bmwx3".equals(type)) {   
  5.             return new BmwX3Car();   
  6.         } else if ("bmwx5".equals(type)) {   
  7.             return new BmwX5Car();   
  8.         } else if ("bmwx7".equals(type)) {   
  9.             return new BmwX7Car();   
  10.         } else  
  11.             return null;   
  12.     }   
  13. }  


修改CarSalesShop的代碼:
Java代碼
  1. public class CarSalesShop {   
  2.     public Car orderCar(String type) {   
  3.         Car car = StaticCarFactory.createCar(type);// 與簡(jiǎn)單工廠的區(qū)別在這里   
  4.         car.prepare();   
  5.         car.fabricate();   
  6.         car.detect();   
  7.   
  8.         System.out.println("A " + car.getName() + " car is ready.");   
  9.         return car;   
  10.     }   
  11. }  


6.工廠方法(Factory Method)模式實(shí)例

在上例的基礎(chǔ)上實(shí)現(xiàn):
Java代碼
  1. public class BeijingBmwX3Car extends Car {   
  2.     public BeijingBmwX3Car() {   
  3.         name = "Beijing BMW X3";   
  4.         engines = "One Engine";   
  5.         wheels = "Four Wheels";   
  6.         lamps = "Two Lamps";   
  7.     }   
  8. }   
  9.   
  10. public class BeijingBmwX5Car extends Car {   
  11.     public BeijingBmwX5Car() {   
  12.         name = "Beijing BMW X5";   
  13.         engines = "Two Engines";   
  14.         wheels = "Four Wheels";   
  15.         lamps = "Four Lamps";   
  16.     }   
  17. }   
  18.   
  19. public class BeijingBmwX7Car extends Car {   
  20.     public BeijingBmwX7Car() {   
  21.         name = "Beijing BMW X7";   
  22.         engines = "Four Engine";   
  23.         wheels = "Four Wheels and One Spare Tyre";   
  24.         lamps = "Six Lamps";   
  25.     }   
  26. }   
  27.   
  28. public class ShanghaiBmwX3Car extends Car {   
  29.     public ShanghaiBmwX3Car() {   
  30.         name = "Shanghai BMW X3";   
  31.         engines = "One Engine";   
  32.         wheels = "Four Wheels";   
  33.         lamps = "Two Lamps";   
  34.     }   
  35. }   
  36.   
  37. public class ShanghaiBmwX5Car extends Car {   
  38.     public ShanghaiBmwX5Car() {   
  39.         name = "Shanghai BMW X5";   
  40.         engines = "Two Engines";   
  41.         wheels = "Four Wheels";   
  42.         lamps = "Four Lamps";   
  43.     }   
  44. }   
  45.   
  46. public class ShanghaiBmwX7Car extends Car {   
  47.     public ShanghaiBmwX7Car() {   
  48.         name = "Shanghai BMW X7";   
  49.         engines = "Four Engine";   
  50.         wheels = "Four Wheels and One Spare Tyre";   
  51.         lamps = "Six Lamps";   
  52.     }   
  53. }   
  54.   
  55. public abstract class CarSalesShop {   
  56.     public Car orderCar(String type) {   
  57.         Car car = createCar(type);   
  58.         car.prepare();   
  59.         car.fabricate();   
  60.         car.detect();   
  61.   
  62.         System.out.println("A " + car.getName() + " car is ready.");   
  63.         return car;   
  64.     }   
  65.   
  66.     // 工廠方法接口   
  67.     public abstract Car createCar(String type);   
  68. }   
  69.   
  70. public class BeijingCarSalesShop extends CarSalesShop {   
  71.     @Override  
  72.     public Car createCar(String type) {   
  73.         if ("bmwx3".equals(type)) {   
  74.             return new BeijingBmwX3Car();   
  75.         } else if ("bmwx5".equals(type)) {   
  76.             return new BeijingBmwX5Car();   
  77.         } else if ("bmwx7".equals(type)) {   
  78.             return new BeijingBmwX7Car();   
  79.         } else  
  80.             return null;   
  81.     }   
  82. }   
  83.   
  84. public class ShanghaiCarSalesShop extends CarSalesShop {   
  85.     @Override  
  86.     public Car createCar(String type) {   
  87.         if ("bmwx3".equals(type)) {   
  88.             return new ShanghaiBmwX3Car();   
  89.         } else if ("bmwx5".equals(type)) {   
  90.             return new ShanghaiBmwX5Car();   
  91.         } else if ("bmwx7".equals(type)) {   
  92.             return new ShanghaiBmwX7Car();   
  93.         } else  
  94.             return null;   
  95.     }   
  96. }  


7.抽象工廠(Abstract Factory)模式實(shí)例
Java代碼
  1. // 發(fā)動(dòng)機(jī)接口   
  2. public interface Engine {   
  3.     public int getHorsepower();   
  4. }   
  5.   
  6. public class BeijingEngine implements Engine {   
  7.     public int getHorsepower() {   
  8.         return 200;   
  9.     }   
  10. }   
  11.   
  12. public class ShanghaiEngine implements Engine {   
  13.     public int getHorsepower() {   
  14.         return 230;   
  15.     }   
  16. }   
  17.   
  18. // 車(chē)輪接口   
  19. public interface Wheel {   
  20.     public double getDiameter();   
  21. }   
  22.   
  23. public class BeijingWheel implements Wheel {   
  24.     public double getDiameter() {   
  25.         return 80;   
  26.     }   
  27. }   
  28.   
  29. public class ShanghaiWheel implements Wheel {   
  30.     public double getDiameter() {   
  31.         return 95;   
  32.     }   
  33. }   
  34.   
  35. // 車(chē)燈接口   
  36. public interface Lamp {   
  37.     public int getPower();   
  38. }   
  39.   
  40. public class BeijingLamp implements Lamp {   
  41.     public int getPower() {   
  42.         return 60;   
  43.     }   
  44. }   
  45.   
  46. public class ShanghaiLamp implements Lamp {   
  47.     public int getPower() {   
  48.         return 80;   
  49.     }   
  50. }   
  51.   
  52. // 汽車(chē)零件制造工廠   
  53. public interface PartsFactory {   
  54.     public Engine createEngine();   
  55.   
  56.     public Wheel createWheel();   
  57.   
  58.     public Lamp createLamp();   
  59. }   
  60.   
  61. public class BeijingPartsFactory implements PartsFactory {   
  62.     public Engine createEngine() {   
  63.         return new BeijingEngine();   
  64.     }   
  65.   
  66.     public Lamp createLamp() {   
  67.         return new BeijingLamp();   
  68.     }   
  69.   
  70.     public Wheel createWheel() {   
  71.         return new BeijingWheel();   
  72.     }   
  73. }   
  74.   
  75. public class ShanghaiPartsFactory implements PartsFactory {   
  76.     public Engine createEngine() {   
  77.         return new ShanghaiEngine();   
  78.     }   
  79.   
  80.     public Lamp createLamp() {   
  81.         return new ShanghaiLamp();   
  82.     }   
  83.   
  84.     public Wheel createWheel() {   
  85.         return new ShanghaiWheel();   
  86.     }   
  87. }   
  88.   
  89. public abstract class Car {   
  90.     protected String name;// 名稱   
  91.   
  92.     protected Engine[] engines;// 發(fā)動(dòng)機(jī)   
  93.   
  94.     protected Wheel[] wheels;// 車(chē)輪   
  95.   
  96.     protected Lamp[] lamps;// 車(chē)燈   
  97.   
  98.     public void prepare() {   
  99.         System.out.println("Preparing " + name);   
  100.         System.out.println("Get engines ready...");   
  101.         System.out.println("Get wheels ready...");   
  102.         System.out.println("Get lamps ready...");   
  103.     }   
  104.   
  105.     // 組裝   
  106.     public void fabricate() {   
  107.         System.out.println("Adding engines.");   
  108.         System.out.println("Adding wheels.");   
  109.         System.out.println("Adding lamps.");   
  110.     }   
  111.   
  112.     // 檢測(cè)   
  113.     public void detect() {   
  114.         System.out.println("Detect the car.");   
  115.     }   
  116.   
  117.     public String getName() {   
  118.         return name;   
  119.     }   
  120. }   
  121.   
  122. public class BmwX3Car extends Car {   
  123.     private PartsFactory factory;   
  124.   
  125.     public BmwX3Car(PartsFactory factory) {   
  126.         this.factory = factory;   
  127.   
  128.         name = "BMW X3";   
  129.         // 一個(gè)發(fā)動(dòng)機(jī)   
  130.         engines = new Engine[] { this.factory.createEngine() };   
  131.         // 四個(gè)車(chē)輪   
  132.         wheels = new Wheel[] { this.factory.createWheel(), this.factory.createWheel(), this.factory.createWheel(),   
  133.                 this.factory.createWheel() };   
  134.         // 兩個(gè)車(chē)燈   
  135.         lamps = new Lamp[] { this.factory.createLamp(), this.factory.createLamp() };   
  136.     }   
  137. }   
  138.   
  139. public class BmwX5Car extends Car {   
  140.     private PartsFactory factory;   
  141.   
  142.     public BmwX5Car(PartsFactory factory) {   
  143.         this.factory = factory;   
  144.   
  145.         name = "BMW X5";   
  146.         // 兩個(gè)發(fā)動(dòng)機(jī)   
  147.         engines = new Engine[] { this.factory.createEngine(), this.factory.createEngine() };   
  148.         // 四個(gè)車(chē)輪   
  149.         wheels = new Wheel[] { this.factory.createWheel(), this.factory.createWheel(), this.factory.createWheel(),   
  150.                 this.factory.createWheel() };   
  151.         // 四個(gè)車(chē)燈   
  152.         lamps = new Lamp[] { this.factory.createLamp(), this.factory.createLamp(), this.factory.createLamp(),   
  153.                 this.factory.createLamp() };   
  154.     }   
  155. }   
  156.   
  157. public class BmwX7Car extends Car {   
  158.     private PartsFactory factory;   
  159.   
  160.     public BmwX7Car(PartsFactory factory) {   
  161.         this.factory = factory;   
  162.   
  163.         name = "BMW X7";   
  164.         // 四個(gè)發(fā)動(dòng)機(jī)   
  165.         engines = new Engine[] { this.factory.createEngine(), this.factory.createEngine(), this.factory.createEngine(),   
  166.                 this.factory.createEngine() };   
  167.         // 四個(gè)車(chē)輪和一個(gè)備用胎   
  168.         wheels = new Wheel[] { this.factory.createWheel(), this.factory.createWheel(), this.factory.createWheel(),   
  169.                 this.factory.createWheel(), this.factory.createWheel() };   
  170.         // 六個(gè)車(chē)燈   
  171.         lamps = new Lamp[] { this.factory.createLamp(), this.factory.createLamp(), this.factory.createLamp(),   
  172.                 this.factory.createLamp() };   
  173.     }   
  174. }   
  175.   
  176. public abstract class CarSalesShop {   
  177.     public Car orderCar(String type) {   
  178.         Car car = createCar(type);   
  179.         car.prepare();   
  180.         car.fabricate();   
  181.         car.detect();   
  182.   
  183.         System.out.println("A " + car.getName() + " car is ready.");   
  184.         return car;   
  185.     }   
  186.   
  187.     // 工廠方法接口   
  188.     public abstract Car createCar(String type);   
  189. }   
  190.   
  191. public class BeijingCarSalesShop extends CarSalesShop {   
  192.     @Override  
  193.     public Car createCar(String type) {   
  194.         PartsFactory factory = new BeijingPartsFactory();   
  195.         if ("bmwx3".equals(type)) {   
  196.             return new BmwX3Car(factory);   
  197.         } else if ("bmwx5".equals(type)) {   
  198.             return new BmwX5Car(factory);   
  199.         } else if ("bmwx7".equals(type)) {   
  200.             return new BmwX7Car(factory);   
  201.         } else  
  202.             return null;   
  203.     }   
  204. }   
  205.   
  206. public class ShanghaiCarSalesShop extends CarSalesShop {   
  207.     @Override  
  208.     public Car createCar(String type) {   
  209.         PartsFactory factory = new ShanghaiPartsFactory();   
  210.         if ("bmwx3".equals(type)) {   
  211.             return new BmwX3Car(factory);   
  212.         } else if ("bmwx5".equals(type)) {   
  213.             return new BmwX5Car(factory);   
  214.         } else if ("bmwx7".equals(type)) {   
  215.             return new BmwX7Car(factory);   
  216.         } else  
  217.             return null;   
  218.     }   
  219. }  


8.結(jié)合策略模式優(yōu)化抽象工廠實(shí)例

修改CarSalesShop類(lèi)如下:
Java代碼
  1. public class CarSalesShop {   
  2.     private PartsFactory factory;   
  3.   
  4.     public CarSalesShop(PartsFactory factory) {   
  5.         this.factory = factory;   
  6.     }   
  7.   
  8.     public Car orderCar(String type) {   
  9.         Car car = createCar(type);   
  10.         car.prepare();   
  11.         car.fabricate();   
  12.         car.detect();   
  13.   
  14.         System.out.println("A " + car.getName() + " car is ready.");   
  15.         return car;   
  16.     }   
  17.   
  18.     // 原有的工廠方法接口,現(xiàn)在實(shí)現(xiàn)了它   
  19.     public Car createCar(String type) {   
  20.         if ("bmwx3".equals(type)) {   
  21.             return new BmwX3Car(factory);   
  22.         } else if ("bmwx5".equals(type)) {   
  23.             return new BmwX5Car(factory);   
  24.         } else if ("bmwx7".equals(type)) {   
  25.             return new BmwX7Car(factory);   
  26.         } else  
  27.             return null;   
  28.     }   
  29. }  


這樣修改后,就不再需要BeijingCarSalesShop和ShanghaiCarSalesShop這兩個(gè)類(lèi)。使用方法如下:
Java代碼
  1. public class Test {   
  2.     public static void main(String[] args) {   
  3.         PartsFactory factory = new BeijingPartsFactory();   
  4.         CarSalesShop shop = new CarSalesShop(factory);   
  5.         shop.orderCar("bmwx7");   
  6.     }   
  7. }  


這樣修改后,由于引入了策略模式,消除了兩個(gè)兩個(gè)子類(lèi),并且可以通過(guò)增加setPartsFactory()方法達(dá)到運(yùn)行時(shí)改變零件生產(chǎn)工廠的目的。

--END--
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Java中的簡(jiǎn)單工廠模式
工廠模式講解, 引入Spring IOC
終于,月薪過(guò)5萬(wàn)了!
設(shè)計(jì)模式----------工廠(Factory)模式
面向?qū)ο螅ɡ^承中的面試題)
spring的ioc實(shí)現(xiàn)和例子
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服