1、简单工厂
// 抽象产品-人类
abstract class Human {
? ? public abstract void Eat();
? ? public abstract void Sleep();
? ? public abstract void Beat();
}
?
// 具体产品-Man
class Man extends Human{
? ? public void Eat() {
? ? ? ? System.out.println("Man can eat."); ? ? ? ?
? ? }
?
? ? public void Sleep() {
? ? ? ? System.out.println("Man can sleep.");
? ? }
?
? ? public void Beat() {
? ? ? ? System.out.println("Man can beat doudou."); ? ? ? ?
? ? }
?
}
?
// 具体产品-Female
class Female extends Human{
?
? ? public void Eat() {
? ? ? ? System.out.println("Female can eat."); ??
? ? }
?
? ? public void Sleep() {
? ? ? ? System.out.println("Female can sleep.");
? ? }
?
? ? public void Beat() {
? ? ? ? System.out.println("Female can beat doudou."); ? ? ? ?
? ? }
?
}
?
// 简单工厂
public class HumanFactory {
? ? public static Human createHuman(String gender){
? ? ? ? Human human = null;
? ? ? ? if( gender.equals("man") ){
? ? ? ? ? ? human = new Man();
? ? ? ? }else if( gender.equals("female")){
? ? ? ? ? ? human = new Female();
? ? ? ? }
?
? ? ? ? return human;
? ? }
}
?
// 女娲造人
public class Goddess { ?
? ? public static void main(String[] args) throws IOException { ?
? ? ? ? // Make Man ?
? ? ? ? Human human = HumanFactory.createHuman("man"); ?
? ? ? ? human.Eat();
? ? ? ? human.Sleep();
? ? ? ? human.Beat();
? ? }?
}
?
2、工厂方法
// 抽象产品
abstract class Human {
? ? public abstract void Eat();
? ? public abstract void Sleep();
? ? public abstract void Beat();
}
?
// 具体产品-Man
class Man extends Human {
? ? public void Eat() {
? ? ? ? System.out.println("Man can eat."); ? ? ? ?
? ? }
?
? ? public void Sleep() {
? ? ? ? System.out.println("Man can sleep.");
? ? }
?
? ? public void Beat() {
? ? ? ? System.out.println("Man can beat doudou."); ? ? ? ?
? ? }
?
}
?
// 具体产品-Female
class Female extends Human{
?
? ? public void Eat() {
? ? ? ? System.out.println("Female can eat."); ??
? ? }
?
? ? public void Sleep() {
? ? ? ? System.out.println("Female can sleep.");
? ? }
?
? ? public void Beat() {
? ? ? ? System.out.println("Female can beat doudou."); ? ? ? ?
? ? }
?
}
?
// 简单工厂变为了抽象工厂
abstract class HumanFactory {
? ? public abstract Human createHuman(String gender) throws IOException;
}
?
// 具体工厂(每个具体工厂负责一个具体产品) ?
class ManFactory extends HumanFactory{ ?
? ? public Human createHuman(String gender) throws IOException { ?
? ? ? ? return new Man(); ?
? ? } ?
} ?
class FemaleFactory extends HumanFactory{ ?
? ? public Human createHuman(String gender) throws IOException { ?
? ? ? ? return new Female(); ?
? ? } ?
} ?
?
// 女娲造人
public class Goddess { ?
?
? ? public static void main(String[] args) throws IOException { ?
? ? ? ? // Make Man ?
? ? ? ? HumanFactory hf = new ManFactory();
? ? ? ? Human h = hf.createHuman("man");
? ? ? ? h.Eat();
? ? ? ? h.Sleep();
? ? ? ? h.Beat();
? ? }?
}
?
3、抽象工厂
// 抽象工厂
public interface KitchenFactory {
? ? public Food getFood();
? ? public TableWare getTableWare();
}
?
// 抽象食物
public interface Food {
? ? public String getFoodName();
}
?
// 抽象餐具
public interface TableWare {
? ? public String getToolName();
}
?
// 以具体工厂 AKitchen 为例
public class AKitchen implements KitchenFactory {
?
? ? public Food getFood() {
? ? ? ?return new Apple();
? ? }
?
? ? public TableWare getTableWare() {
? ? ? ?return new Knife();
? ? }
}
?
// 具体食物 Apple 的定义如下
public class Apple implements Food{
? ? public String getFoodName() {
? ? ? ?return "apple";
? ? }
}
?
// 具体餐具 Knife 的定义如下
public class Knife implements TableWare {?
? ? public String getToolName() {
? ? ? ?return "knife";
? ? }
}
?
// 吃货要开吃了
public class Foodaholic {
?
? ? public void eat(KitchenFactory k) {
? ? ? ?System.out.println("A foodaholic is eating "+ k.getFood().getFoodName()
? ? ? ? ? ? ? + " with " + k.getTableWare().getToolName() );
? ? }
?
? ? public static void main(String[] args) {
? ? ? ?Foodaholic fh = new Foodaholic();
? ? ? ?KitchenFactory kf = new AKitchen();
? ? ? ?fh.eat(kf);
?
? ? }
}
?
总结:
工厂方法模式:针对的是一个产品等级结构。
抽象工厂模式:针对多个产品等级结构。
原文:http://wuhoujian322.iteye.com/blog/2311996