当系统准备为用户提供一系列相关的对象,又不想让用户代码和创建这些对象的类形成耦合时,就可以使用抽象工厂方法模式来设计系统。
抽象工厂模式的关键是在一个抽象类或接口中定义若干个抽象方法,这些抽象方法分别返回某个类的实例,该抽象类或接口让其子类或实现该接口的类重写这些抽象方法,为用户提供一系列相关的对象。
模式的结构中包括四种角色:
public abstract class UpperClothes{
public abstract int getChestSize();
public abstract int getHeight();
public abstract String getName();
}
Trousers.java
public abstract class Trousers{
public abstract int getWaistSize();
public abstract int getHeight();
public abstract String getName();
}
InnerClothes.java
public abstract class InnerClothes {
public abstract int getSize();
public abstract int getHeight();
public abstract String getName();
}
具体产品
WesternUpperClothes.java
public class WesternUpperClothes extends UpperClothes{
private int chestSize;
private int height;
private String name;
WesternUpperClothes(String name,int chestSize,int height){
this.name=name;
this.chestSize=chestSize;
this.height=height;
}
public int getChestSize(){
return chestSize;
}
public int getHeight(){
return height;
}
public String getName(){
return name;
}
}
CowboyUpperClothes .java
public class CowboyUpperClothes extends UpperClothes{
private int chestSize;
private int height;
private String name;
CowboyUpperClothes(String name,int chestSize,int height){
this.name=name;
this.chestSize=chestSize;
this.height=height;
}
public int getChestSize(){
return chestSize;
}
public int getHeight(){
return height;
}
public String getName(){
return name;
}
}
WesternTrousers .java
public class WesternTrousers extends Trousers{
private int waistSize;
private int height;
private String name;
WesternTrousers(String name,int waistSize,int height){
this.name=name;
this.waistSize=waistSize;
this.height=height;
}
public int getWaistSize(){
return waistSize;
}
public int getHeight(){
return height;
}
public String getName(){
return name;
}
}
CowboyTrousers.java
public class CowboyTrousers extends Trousers{
private int waistSize;
private int height;
private String name;
CowboyTrousers(String name,int waistSize,int height){
this.name=name;
this.waistSize=waistSize;
this.height=height;
}
public int getWaistSize(){
return waistSize;
}
public int getHeight(){
return height;
}
public String getName(){
return name;
}
}
ShirtInnerClothes.java
public class ShirtInnerClothes extends InnerClothes {
private int Size;
private int height;
private String name;
ShirtInnerClothes(String name ,int size ,int height){
this.name=name;
this.Size=size;
this.height=height;
}
public int getSize(){
return Size;
}
public int getHeight(){
return height;
}
public String getName(){
return name;
}
}
SweaterInnerClothes.java
public class SweaterInnerClothes extends InnerClothes {
private int Size;
private int height;
private String name;
SweaterInnerClothes (String name ,int size ,int height){
this.name=name;
this.Size=size;
this.height=height;
}
public int getSize(){
return Size;
}
public int getHeight(){
return height;
}
public String getName(){
return name;
}
}
抽象工厂
ClothesFactory.java
public abstract class ClothesFactory{
public abstract UpperClothes createUpperClothes(int chestSize,int height);
public abstract Trousers createTrousers(int waistSize,int height);
public abstract InnerClothes createInnerClothes(int Size,int height);
}
具体工厂
BeijingClothesFactory.java
public class BeijingClothesFactory extends ClothesFactory {
public UpperClothes createUpperClothes(int chestSize,int height){
return new WesternUpperClothes("北京牌西服上衣",chestSize,height);
}
public Trousers createTrousers(int waistSize,int height){
return new WesternTrousers("北京牌西服裤子",waistSize,height);
}
public InnerClothes createInnerClothes(int Size,int height){
return new SweaterInnerClothes("北京牌衬衫",Size,height);
}
}
ShanghaiClothesFactory.java
public class ShanghaiClothesFactory extends ClothesFactory {
public UpperClothes createUpperClothes(int chestSize,int height){
return new WesternUpperClothes("上海牌牛仔上衣",chestSize,height);
}
public Trousers createTrousers(int waistSize,int height){
return new WesternTrousers("上海牌牛仔裤",waistSize,height);
}
public InnerClothes createInnerClothes(int Size,int height){
return new ShirtInnerClothes("上海牌衬衫",Size,height);
}
}
应用
Shop.java
public class Shop{
UpperClothes cloth;
Trousers trouser;
InnerCloth inner;
public void giveSuit(ClothesFactory factory,int chestSize,int waistSize,int Size,int height){
cloth=factory.createUpperClothes(chestSize,height);
trouser=factory.createTrousers(waistSize,height);
inner=factory.createInnerClothes(Size, 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());
System.out.println(inner.getName()+":");
System.out.print("尺寸:"+inner.getSize());
System.out.println("身高:"+inner.getHeight());
}
}
Application.java
public class Application{
public static void main(String args[]){
Shop shop=new Shop();
ClothesFactory factory=new BeijingClothesFactory();
shop.giveSuit(factory,110,82,45,170);
factory=new ShanghaiClothesFactory();
shop.giveSuit(factory,120,88,45,180);
}
}
UML类图:
应用程序在使用抽象工厂模式时,只和抽象产品、抽象工厂以及具体工厂打交道,用户只需了解抽象产品有哪些方法即可,不需要知道有哪些具体产品。
这里是不吃香菜也可做香菜头子。