工厂模式

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);

 

    }

}

 

总结:

工厂方法模式:针对的是一个产品等级结构。

抽象工厂模式:针对多个产品等级结构。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值