首页 > 其他 > 详细

设计模式之工厂模式

时间:2016-07-22 02:18:54      阅读:568      评论:0      收藏:0      [点我收藏+]

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!