http://www.books.com.tw/products/0010750585
chap05.
抽象工廠模式 Abstract Factory
目的:用一個工廠介面來產生一系列相關的物件,但實際建立哪些物件由實作工廠的子類別來實現。
package com.ssc24.chap05.demo01;
public abstract class Adventurer {
public Weapon weapon;
public Clothes clothes;
public abstract void display();
public Weapon getWeapon() {
return weapon;
}
public void setWeapon(Weapon weapon) {
this.weapon = weapon;
}
public Clothes getClothes() {
return clothes;
}
public void setClothes(Clothes clothes) {
this.clothes = clothes;
}
}
package com.ssc24.chap05.demo01;
public class Archer extends Adventurer {
@Override
public void display() {
System.out.println("我是弓箭手,裝備:");
weapon.display();
clothes.disPlay();
}
}
package com.ssc24.chap05.demo01;
public class ArcherEquipFactory implements EquipFactory {
@Override
public Weapon productWeapon() {
Bow product = new Bow();
product.setAtk(10);
product.setRange(10);
return product;
}
@Override
public Clothes productArmor() {
Leather product = new Leather();
product.setDef(5);
return product;
}
}
package com.ssc24.chap05.demo01;
public class ArcherTrainingCamp implements TrainingCamp {
@Override
public Adventurer trainAdventurer() {
System.out.println("訓練一個弓箭手");
Archer archer = new Archer();
EquipFactory factory = new ArcherEquipFactory();
archer.setWeapon(factory.productWeapon());
archer.setClothes(factory.productArmor());
return archer;
}
}
package com.ssc24.chap05.demo01;
public class Armor extends Clothes {
}
package com.ssc24.chap05.demo01;
public class Bow extends Weapon {
}
package com.ssc24.chap05.demo01;
public abstract class Clothes {
protected int def;
public void disPlay() {
System.out.println(this.getClass().getSimpleName() + "def=" + this.def);
}
public int getDef() {
return def;
}
public void setDef(int def) {
this.def = def;
}
}
package com.ssc24.chap05.demo01;
public interface EquipFactory {
Weapon productWeapon();
Clothes productArmor();
}
package com.ssc24.chap05.demo01;
public class Leather extends Clothes {
}
package com.ssc24.chap05.demo01;
public class LongSword extends Weapon {
}
package com.ssc24.chap05.demo01;
public interface TrainingCamp {
public Adventurer trainAdventurer();
}
package com.ssc24.chap05.demo01;
public class Warrior extends Adventurer {
@Override
public void display() {
System.out.println("我是一個鬥士,裝備:");
weapon.display();
clothes.disPlay();
}
}
package com.ssc24.chap05.demo01;
public class WarriorEquipFactory implements EquipFactory {
@Override
public Weapon productWeapon() {
LongSword product = new LongSword();
product.setAtk(10);
product.setRange(1);
return product;
}
@Override
public Clothes productArmor() {
Armor product = new Armor();
product.setDef(10);
return product;
}
}
package com.ssc24.chap05.demo01;
public class WarriorTrainingCamp implements TrainingCamp {
@Override
public Adventurer trainAdventurer() {
System.out.println("訓練一個鬥士");
Warrior warrior = new Warrior();
EquipFactory factory = new WarriorEquipFactory();
warrior.setWeapon(factory.productWeapon());
warrior.setClothes(factory.productArmor());
return warrior;
}
}
package com.ssc24.chap05.demo01;
public abstract class Weapon {
protected int atk;
protected int range;
public void display() {
System.out.println(this.getClass().getSimpleName() +
" atk=" + this.atk + " , range=" + this.range);
}
public int getAtk() {
return atk;
}
public void setAtk(int atk) {
this.atk = atk;
}
public int getRange() {
return range;
}
public void setRange(int range) {
this.range = range;
}
}
package com.ssc24.chap05.demo01;
import static org.junit.Assert.*;
import org.junit.Test;
public class EquipFactoryTest {
@Test
public void test() {
System.out.println("===抽象工廠模式測試===");
// EquipFactory equipFactory = new ArcherEquipFactory();
// Clothes archerLeather = equipFactory.productArmor();
// Weapon archerBow = equipFactory.productWeapon();
TrainingCamp archerCamp = new ArcherTrainingCamp();
Adventurer archer = archerCamp.trainAdventurer();
archer.display();
TrainingCamp warriorCamp = new WarriorTrainingCamp();
Adventurer warrior = warriorCamp.trainAdventurer();
warrior.display();
}
} /** ===抽象工廠模式測試=== 訓練一個弓箭手 我是弓箭手,裝備: Bow atk=10 , range=10 Leatherdef=5 訓練一個鬥士 我是一個鬥士,裝備: LongSword atk=10 , range=1 Armordef=10 **/