JAVA设计模式之工厂模式
<!--[if !supportLists]-->(2)??? <!--[endif]-->当一个产品族中的多个对象被设计成一起工作时,它能够保证客户端始终只使用同一个产品族中的对象。
缺点:
(1)添加新的产品对像时,难以扩展抽象工厂以便生产新种类的产品。
?
4.4抽象工厂模式的适用环境(1)一个系统不应当依赖于产品类实例如何被创建、组合和表达的细节。这对于所有形态的工厂模式都是重要的;
(2)一个系统的产品有多于一个的产品族,而系统只消费其中某一族的产品;
(3)同属于同一个产品族的产品是在一起使用的,这一约束必须要在系统的设计中体现出来;
(4)系统提供一个产品类的库,所有的产品以同样的接口出现,从而使客户端不依赖于实现。
?
4.5抽象工厂模式的举例例:
//抽象产品
UpperClothes.java
publicabstract class UpperClothes {
?????? public abstract int getChestSize();
?????? public abstract int getHeight();
?????? public abstract String getName();
}
Trousers.java
publicabstract class Trousers {
?????? public abstract int getWaistSize();
?????? public abstract int getHeight();
?????? public abstract String getName();
}
//具体产品
WesternUpperClothes.java
publicclass WesternUpperClothes extends UpperClothes {
?????? private int chestSize;
?????? private int height;
?????? private String name;
?????? WesternUpperClothes(String name,intchestSize,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
publicclass CowboyUpperClothes extends UpperClothes {
?????? private int chestSize;
?????? private int height;
?????? private String name;
?????? CowboyUpperClothes(String name,intchestSize,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
publicclass WesternTrousers extends Trousers {
?????? private int waistSize;
?????? private int height;
?????? private String name;
?????? WesternTrousers(String name,intwaistSize,int height){
????????????? this.name=name;
????????????? this.waistSize=waistSize;
????????????? this.height=height;
?????? }
?????? public int getHeight() {
????????????? return height;
?????? }
?????? public String getName() {
????????????? return name;
?????? }
?????? public int getWaistSize() {
????????????? return waistSize;
?????? }
}
CowboyTrousers.java
publicclass CowboyTrousers extends Trousers {
?????? private int waistSize;
?????? private int height;
?????? private String name;
?????? CowboyTrousers(String name,intwaistSize,int height){
????????????? this.name=name;
????????????? this.waistSize=waistSize;
????????????? this.height=height;
?????? }
?????? public int getHeight() {
????????????? return height;
?????? }
?????? public String getName() {
????????????? return name;
?????? }
?????? public int getWaistSize() {
????????????? return waistSize;
?????? }
}
//抽象工厂
ClothesFactory.java
publicabstract class ClothesFactory {
?????? public abstract UpperClothescreateUpperClothes(int chestSize,int height);
?????? public abstract Trousers createTrousers(intwaistSize,int height);
}
//具体工厂
BeijingClothesFactory.java
publicclass BeijingClothesFactory extends ClothesFactory {
?????? public Trousers createTrousers(int waistSize,int height) {
????????????? return new WesternTrousers("北京牌裤子",waistSize,height);
?????? }
?????? public UpperClothescreateUpperClothes(int chestSize, int height) {
????????????? return newWesternUpperClothes("北京牌上衣",chestSize,height);
?????? }
}
ShanghaiClothesFactory.java
publicclass ShanghaiClothesFactory extends ClothesFactory {
?????? public Trousers createTrousers(int waistSize,int height) {
????????????? return new WesternTrousers("上海牌裤子",waistSize,height);
?????? }
?????? public UpperClothescreateUpperClothes(int chestSize, int height) {
????????????? return newWesternUpperClothes("上海牌上衣",chestSize,height);
?????? }
}
?
?
?