标签:getname opp minutes void arraylist design http tin bst
PizzaStore类
package com.DesignPatterns.ad.factory2; public abstract class PizzaStore { abstract Pizza createPizza(String item); public Pizza orderPizza(String type) { Pizza pizza = createPizza(type); System.out.println("--- Making a " + pizza.getName() + " ---"); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } } |
纽约披萨店工厂类
package com.DesignPatterns.ad.factory2; public class NYPizzaStore extends PizzaStore {
Pizza createPizza(String item) {
if (item.equals("cheese")) {
return new NYStyleCheesePizza();
|
| } else if (item.equals("veggie")) { return new NYStyleVeggiePizza(); } else if (item.equals("clam")) { return new NYStyleClamPizza(); } else if (item.equals("pepperoni")) { return new NYStylePepperoniPizza(); } else return null; |
} | } |
芝加哥披萨店工厂类
package com.DesignPatterns.ad.factory2; public class ChicagoPizzaStore extends PizzaStore { Pizza createPizza(String item) { if (item.equals("cheese")) { return new ChicagoStyleCheesePizza(); } else if (item.equals("veggie")) { return new ChicagoStyleVeggiePizza(); } else if (item.equals("clam")) { return new ChicagoStyleClamPizza(); } else if (item.equals("pepperoni")) { return new ChicagoStylePepperoniPizza(); } else return null; } } |
pizza类
import java.util.ArrayList;
public abstract class Pizza {
String name;
String dough;
String sauce;
ArrayList<String> toppings = new ArrayList<String>();
void prepare() {
System.out.println("Prepare " + name);
System.out.println("Tossing dough...");
System.out.println("Adding sauce...");
System.out.println("Adding toppings: ");
for (String topping : toppings) {
System.out.println(" " + topping);
}
}
} | void bake() { System.out.println("Bake for 25 minutes at 350"); } void cut() { System.out.println("Cut the pizza into diagonal slices"); } void box() { System.out.println("Place pizza in official PizzaStore box"); } public String getName() { return name; } public String toString() { StringBuffer display = new StringBuffer(); display.append("---- " + name + " ----\n"); display.append(dough + "\n"); display.append(sauce + "\n"); for (String topping : toppings) { display.append(topping + "\n"); } return display.toString(); } |
纽约披萨工厂实现类1 NYStyleCheesePizza
package com.DesignPatterns.ad.factory2; public class NYStyleCheesePizza extends Pizza { public NYStyleCheesePizza() { name = "NY Style Sauce and Cheese Pizza"; dough = "Thin Crust Dough"; sauce = "Marinara Sauce";
toppings.add("Grated Reggiano Cheese"); } } |
纽约披萨工厂实现类2 NYStyleClamPizza
package com.DesignPatterns.ad.factory2; public class NYStyleClamPizza extends Pizza {
public NYStyleClamPizza() {
name = "NY Style Clam Pizza";
dough = "Thin Crust Dough";
sauce = "Marinara Sauce";
toppings.add("Grated Reggiano Cheese");
toppings.add("Fresh Clams from Long Island Sound");
}
}
芝加哥披萨工厂实现1 ChicagoStyleCheesePizza
package com.DesignPatterns.ad.factory2; public class ChicagoStyleCheesePizza extends Pizza { public ChicagoStyleCheesePizza() { name = "Chicago Style Deep Dish Cheese Pizza"; dough = "Extra Thick Crust Dough"; sauce = "Plum Tomato Sauce";
toppings.add("Shredded Mozzarella Cheese"); }
void cut() { System.out.println("Cutting the pizza into square slices"); } } |
芝加哥披散工厂实现2 ChicagoStyleClamPizza
package com.DesignPatterns.ad.factory2;
public class ChicagoStyleClamPizza extends Pizza {
public ChicagoStyleClamPizza() {
name = "Chicago Style Clam Pizza";
dough = "Extra Thick Crust Dough";
sauce = "Plum Tomato Sauce";
toppings.add("Shredded Mozzarella Cheese");
toppings.add("Frozen Clams from Chesapeake Bay");
}
void cut() {
System.out.println("Cutting the pizza into square slices");
} | } |
工厂方法模式:定义一个创建对象的接口,但是由子类决定实例化类是哪一个。工厂方法的实例化推迟到子类来实现。
说白了就是把代码的创建都放到一块去做,而不是用当我们用到的时候在去创建,用的时候只需要new一个工厂就行了。
依赖倒置的原则:
通过上面的例子我们可以来分析一下依赖倒置原则:
package com.DesignPatterns.ad.factory2; public class DependentPizzaStore { public Pizza createPizza(String style, String type) { Pizza pizza = null; if (style.equals("NY")) { if (type.equals("cheese")) { pizza = new NYStyleCheesePizza(); } else if (type.equals("veggie")) { pizza = new NYStyleVeggiePizza(); } else if (type.equals("clam")) { pizza = new NYStyleClamPizza(); } else if (type.equals("pepperoni")) { pizza = new NYStylePepperoniPizza(); } } else if (style.equals("Chicago")) { if (type.equals("cheese")) { pizza = new ChicagoStyleCheesePizza(); } else if (type.equals("veggie")) { pizza = new ChicagoStyleVeggiePizza(); } else if (type.equals("clam")) { pizza = new ChicagoStyleClamPizza(); } else if (type.equals("pepperoni")) { pizza = new ChicagoStylePepperoniPizza(); } } else { System.out.println("Error: invalid type of pizza"); return null; } pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } } |
我们把上面的例子化成类图,如下
所谓的依赖倒置就是要依赖抽象不要依赖抽象类
就相当于下面的代码pizzaStroe和pizza的实现类都依赖pizza的道理是一样的。这样就实现了解耦的良好的效果。
依赖倒置就如下面的图,就实现依赖倒置了。
正向的思维就是现在,我们现在有一个披萨门店(pissStore)我们需要需要不同品类的披萨,所以就在pissStore中不断的添加披萨。
逆向的思维就是,我们先设置一个抽象的Pizza披萨,里面有抽象的方法prepare(),具体的实现类都实现这个Pizza抽象类。而在PizzaStore披萨店中则关联Pizza。这样就实现了高层组件和底层组件都依赖抽象的。
依赖倒置需要符合下面的几个原则:
pizza = new NYStyleCheesePizza()(变量不可以持有具体类的引用) new NYStyleCheesePizza(不要让类派生自具体类)
void prepare() {
System.out.println("Prepare " + name);
System.out.println("Tossing dough...");
System.out.println("Adding sauce...");
System.out.println("Adding toppings: ");
for (String topping : toppings) {
System.out.println(" " + topping);
}
}(不要覆盖基类中以实现的方法)
gg
标签:getname opp minutes void arraylist design http tin bst
原文地址:https://www.cnblogs.com/qingruihappy/p/9739643.html