[color=red]【抽象工厂模式】提供一个创建一系列或相互依赖对象的接口,而无须指定它们具体的类。[/color]
package com.demo.abstractFactory;
public class BeijingClothesFactory extends ClothesFactory {
@Override
public UpperClothes createUpperClothes(int chestSize, int height) {
// TODO Auto-generated method stub
return new WesternUpperClothes("北京牌西服上衣",chestSize,height);
}
@Override
public Trousers createTrousers(int waistSize, int height) {
// TODO Auto-generated method stub
return new WesternTrousers("北京牌西服裤子",waistSize,height);
}
}
package com.demo.abstractFactory;
public class Shop {
UpperClothes cloth;
Trousers trouser;
public void giveSuit(ClothesFactory factory,int chestSize,int waistSize,int height){
cloth=factory.createUpperClothes(chestSize, height);
trouser=factory.createTrousers(waistSize, height);
showMess();
}
private void showMess(){
System.out.println("<套装信息>");
System.out.println(cloth.getName()+":");
System.out.print("胸围:"+cloth.getChestSize());
System.out.println(" 身高:"+cloth.getHeight());
System.out.println(trouser.getName()+":");
System.out.print("腰围:"+trouser.getWaistSize());
System.out.println(" 身高:"+trouser.getHeight());
}
}
package com.demo.abstractFactory;
public class Application {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Shop shop=new Shop();
ClothesFactory factory=new BeijingClothesFactory();
shop.giveSuit(factory, 110, 82, 170);
factory=new ShanghaiClothesFactory();
shop.giveSuit(factory, 120, 88, 180);
}
}
<套装信息>
北京牌西服上衣:
胸围:110 身高:170
北京牌西服裤子:
腰围:82 身高:170
<套装信息>
上海牌西服上衣:
胸围:120 身高:180
上海牌西服裤子:
腰围:88 身高:180