- 设计原则
1、多用组合,少用继承
2,针对接口编程,不针对现实编程
3、为交互对象之间的松耦设计而努力
4、类应该对扩展开放,对修改关闭
5、依赖抽象,不要依赖具体类。
- 抽象工厂模式
抽象工厂模式 —— 提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。
/**
* 抽象工厂接口
* 抽象工厂定义了一个接口,所有的具体工厂都必须实现此接口。
* 这个接口包含一组方法来生产产品
* @author WangChunhe
*
*/
public interface PizzaIngredientFactory {
public Dough createDough();
public Sauce creataSauce();
public Cheese createCheese();
public Veggies[] createVeggies();
public Pepperoni createPepperoni();
public Clams createClam();
}
/**
* 具体工厂
* @author WangChunhe
*
*/
public class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory {
@Override
public Dough createDough() {
// TODO Auto-generated method stub
return new ThickCrustDough();
}
@Override
public Sauce creataSauce() {
// TODO Auto-generated method stub
return new PlumTomatoSauce();
}
@Override
public Cheese createCheese() {
// TODO Auto-generated method stub
return new MozzarellaCheese();
}
@Override
public Veggies[] createVeggies() {
// TODO Auto-generated method stub
Veggies veggies[] = { new BlackOlives(), new Spinach(), new Eggplant()};
return veggies;
}
@Override
public Pepperoni createPepperoni() {
// TODO Auto-generated method stub
return new SlicedPepperoni();
}
@Override
public Clams createClam() {
// TODO Auto-generated method stub
return new FrozenClams();
}
}
/**
* 具体工厂
* 该具体工厂实现不同的产品家族。要创建一个产品,
* 客户主需要使用其中的一个工厂而完全不需要实例化任何产品对象
* @author WangChunhe
*
*/
public class NYPizzaIngredientFactory implements PizzaIngredientFactory{
@Override
public Dough createDough() {
// TODO Auto-generated method stub
return new ThinCrustDugh();
}
@Override
public Sauce creataSauce() {
// TODO Auto-generated method stub
return new MarinaraSauce();
}
@Override
public Cheese createCheese() {
// TODO Auto-generated method stub
return new ReggianCheese();
}
@Override
public Veggies[] createVeggies() {
// TODO Auto-generated method stub
Veggies veggies[] = { new Garlic() , new Onion() , new Mushroom() , new RedPepper()};
return veggies;
}
@Override
public Pepperoni createPepperoni() {
// TODO Auto-generated method stub
return new SlicedPepperoni();
}
@Override
public Clams createClam() {
// TODO Auto-generated method stub
return new FreshClams();
}
}
/**
* 具体工厂
* 该具体工厂实现不同的产品家族。要创建一个产品,
* 客户主需要使用其中的一个工厂而完全不需要实例化任何产品对象
* @author WangChunhe
*
*/
public class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory {
@Override
public Dough createDough() {
// TODO Auto-generated method stub
return new ThickCrustDough();
}
@Override
public Sauce creataSauce() {
// TODO Auto-generated method stub
return new PlumTomatoSauce();
}
@Override
public Cheese createCheese() {
// TODO Auto-generated method stub
return new MozzarellaCheese();
}
@Override
public Veggies[] createVeggies() {
// TODO Auto-generated method stub
Veggies veggies[] = { new BlackOlives(), new Spinach(), new Eggplant()};
return veggies;
}
@Override
public Pepperoni createPepperoni() {
// TODO Auto-generated method stub
return new SlicedPepperoni();
}
@Override
public Clams createClam() {
// TODO Auto-generated method stub
return new FrozenClams();
}
}
--------------------------------------------------------
/**
*接下来是产品家族,
*每个具体工厂都能产生仪征组的产品
*
*/
--------------------------------------------------------
/**
* 产品接口 Dough
* @author WangChunhe
*
*/
public interface Dough {
public String toString();
}
/**
* 具体产品
* @author WangChunhe
*
*/
public class ThickCrustDough implements Dough {
@Override
public String toString() {
// TODO Auto-generated method stub
return "ThickCrust style extra thick crust dough " ;
}
}
/**
* 具体产品
* @author WangChunhe
*
*/
public class ThinCrustDugh implements Dough {
@Override
public String toString() {
// TODO Auto-generated method stub
return "Thin Crust Dough" ;
}
}
/**
* 产品接口
* @author WangChunhe
*
*/
public interface Sauce {
public String toString();
}
/**
* 具体产品
* @author WangChunhe
*
*/
public class PlumTomatoSauce implements Sauce {
@Override
public String toString() {
// TODO Auto-generated method stub
return "Tomato sauce with plum tomatoes" ;
}
}
/**
* 具体产品
* @author WangChunhe
*
*/
public class MarinaraSauce implements Sauce {
@Override
public String toString() {
// TODO Auto-generated method stub
return "Marinara Sauce" ;
}
}
/**
* 产品接口
* @author WangChunhe
*
*/
public interface Cheese {
public String toString();
}
/**
* 具体产品
* @author WangChunhe
*
*/
public class MozzarellaCheese implements Cheese {
@Override
public String toString() {
// TODO Auto-generated method stub
return "Shredded Mozzarella" ;
}
}
/**
* 具体产品
* @author WangChunhe
*
*/
public class ReggianCheese implements Cheese {
@Override
public String toString() {
// TODO Auto-generated method stub
return "Reggiano Cheese" ;
}
}
/**
* 产品接口
* @author WangChunhe
*
*/
public interface Clams {
public String toString();
}
/**
*
* 具体产品
* @author WangChunhe
*
*/
public class FreshClams implements Clams {
@Override
public String toString() {
// TODO Auto-generated method stub
return "Fresh clams from Long Island Sound" ;
}
}
/**
*
* 具体产品
* @author WangChunhe
*
*/
public class FrozenClams implements Clams {
@Override
public String toString() {
// TODO Auto-generated method stub
return "Frozen Clams from Chesapeake Bay" ;
}
}
/**
* 产品接口
* @author WangChunhe
*
*/
public interface Veggies {
public String toString();
}
/**
* 具体产品
* @author WangChunhe
*
*/
public class Onion implements Veggies{
@Override
public String toString() {
// TODO Auto-generated method stub
return "Onion" ;
}
}
/**
* 具体产品
* @author WangChunhe
*
*/
public class Garlic implements Veggies{
@Override
public String toString() {
// TODO Auto-generated method stub
return "Garlic" ;
}
}
/**
* 具体产品
* @author WangChunhe
*
*/
public class Mushroom implements Veggies {
@Override
public String toString() {
// TODO Auto-generated method stub
return "Mushroom" ;
}
}
/**
* 具体产品
* @author WangChunhe
*
*/
public class ReggianCheese implements Cheese {
@Override
public String toString() {
// TODO Auto-generated method stub
return "Reggiano Cheese" ;
}
}
/**
* 具体产品
* @author WangChunhe
*
*/
public class Spinach implements Veggies {
@Override
public String toString() {
// TODO Auto-generated method stub
return "Spinach" ;
}
}
/**
* 具体产品
* @author WangChunhe
*
*/
public class Eggplant implements Veggies {
@Override
public String toString() {
// TODO Auto-generated method stub
return "Eggpint" ;
}
}
/**
* 具体产品
* @author WangChunhe
*
*/
public class BlackOlives implements Veggies {
@Override
public String toString() {
// TODO Auto-generated method stub
return "Black Olives" ;
}
}
/**
* 抽象工厂的客户端
* @author WangChunhe
*
*/
public abstract class PizzaStore {
public 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;
}
}
/**
* 客户端
* @author WangChunhe
*
*/
public class ChicagoPizzaStore extends PizzaStore {
@Override
public Pizza createPizza(String item) {
// TODO Auto-generated method stub
Pizza pizza = null;
PizzaIngredientFactory ingredientFactory =
new ChicagoPizzaIngredientFactory();
if (item.equals("cheese" )) {
pizza = new CheesePizza(ingredientFactory);
pizza.setName( "Chicago Style Cheese Pizza" );
} else if (item.equals("veggie")) {
pizza = new VeggiePizza(ingredientFactory);
pizza.setName( "Chicago Style Veggie Pizza" );
} else if (item.equals("clam")) {
pizza = new ClamPizza(ingredientFactory);
pizza.setName( "Chicago Style Clam Pizza");
} else if (item.equals("pepperoni")) {
pizza = new PepperoniPizza(ingredientFactory);
pizza.setName( "Chicago Style Pepperoni Pizza" );
}
return pizza;
}
}
/**
* 客户端
* @author WangChunhe
*
*/
public class NYPizzaStore extends PizzaStore {
@Override
public Pizza createPizza(String item) {
// TODO Auto-generated method stub
Pizza pizza = null;
PizzaIngredientFactory ingredientFactory =
new NYPizzaIngredientFactory();
if (item.equals("cheese" )) {
pizza = new CheesePizza(ingredientFactory);
pizza.setName( "New York Style Cheese Pizza" );
} else if (item.equals("veggie")) {
pizza = new VeggiePizza(ingredientFactory);
pizza.setName( "New York Style Veggie Pizza" );
} else if (item.equals("clam")) {
pizza = new ClamPizza(ingredientFactory);
pizza.setName( "New York Style Clam Pizza" );
} else if (item.equals("pepperoni")) {
pizza = new PepperoniPizza(ingredientFactory);
pizza.setName( "New York Style Pepperoni Pizza" );
}
return pizza;
}
}
/**
* Pizza类
* @author WangChunhe
*
*/
public abstract class Pizza {
//每个比萨都持有一组在准备时会用的原料
String name;
Dough dough;
Sauce sauce;
Veggies veggies[];
Cheese cheese;
Pepperoni pepperoni;
Clams clam;
/**
* 该方法声明为抽象。在这个方法中,需要搜集比萨所需的原料,而这些东西来自原料工厂
*/
abstract void prepare();
void cut() {
System. out.println("Cutting the pizza into diagonal" );
}
void bake() {
System. out.println("Bake for 25 minutes at 350" );
}
void box() {
System. out.println("Place pizza in official PizzaStore box");
}
void setName( String name) {
this.name = name;
}
String getName() {
return name ;
}
public String toString() {
StringBuffer result = new StringBuffer();
result.append( "---- " + name + " ----\n");
if (dough != null) {
result.append( dough);
result.append( "\n");
}
if (sauce != null) {
result.append( sauce);
result.append( "\n");
}
if (cheese != null) {
result.append( cheese);
result.append( "\n");
}
if (veggies != null) {
for (int i = 0; i < veggies.length; i++) {
result.append( veggies[i]);
if (i < veggies .length -1) {
result.append( ", ");
}
}
result.append( "\n");
}
if (clam != null) {
result.append( clam);
result.append( "\n");
}
if (pepperoni != null) {
result.append( pepperoni);
result.append( "\n");
}
return result.toString();
}
}
public class ClamPizza extends Pizza {
PizzaIngredientFactory ingredientFactory;
public ClamPizza (PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory = ingredientFactory;
}
@Override
void prepare() {
// TODO Auto-generated method stub
dough = ingredientFactory .createDough();
sauce = ingredientFactory .creataSauce();
cheese = ingredientFactory .createCheese();
}
}
public class VeggiePizza extends Pizza {
PizzaIngredientFactory ingredientFactory;
public VeggiePizza ( PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory = ingredientFactory;
}
@Override
void prepare() {
// TODO Auto-generated method stub
System. out.println("Prepar " + name);
dough = ingredientFactory .createDough();
sauce = ingredientFactory .creataSauce();
cheese = ingredientFactory .createCheese();
veggies = ingredientFactory .createVeggies();
}
}
public class PepperoniPizza extends Pizza {
PizzaIngredientFactory ingredientFactory;
public PepperoniPizza (PizzaIngredientFactory ingredientFactory) {
this.ingredientFactory = ingredientFactory;
}
@Override
void prepare() {
// TODO Auto-generated method stub
System. out.println("Preparing " + name);
dough = ingredientFactory .createDough();
sauce = ingredientFactory .creataSauce();
cheese = ingredientFactory .createCheese();
pepperoni = ingredientFactory .createPepperoni();
}
}
/**
* 测试类
* @author WangChunhe
*
*/
public class PizzaTestDrive {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PizzaStore nyStore = new NYPizzaStore();
PizzaStore chicagoStore = new ChicagoPizzaStore();
Pizza pizza = nyStore.orderPizza( "cheese");
System. out.println("Ethan ordered a " + pizza + "\n");
pizza = chicagoStore.orderPizza( "cheese");
System. out.println("Joel ordered a " + pizza + "\n");
pizza = nyStore.orderPizza( "clam");
System. out.println("Ethan ordered a " + pizza + "\n");
pizza = chicagoStore.orderPizza( "clam");
System. out.println("Joel ordered a " + pizza + "\n");
pizza = nyStore.orderPizza( "pepperoni");
System. out.println("Ethan ordered a " + pizza + "\n");
pizza = chicagoStore.orderPizza( "pepperoni");
System. out.println("Joel ordered a " + pizza + "\n");
pizza = nyStore.orderPizza( "veggie");
System. out.println("Ethan ordered a " + pizza + "\n");
pizza = chicagoStore.orderPizza( "veggie");
System. out.println("Joel ordered a " + pizza + "\n");
}
}
测试结果
![](https://img-blog.csdn.net/20151120162440202?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)