Java 实现工厂设计模式的三种方式

1.简单工厂(Simple Factory)

<span style="font-size:14px;">//简单工厂设计模式
interface Vehicle        //写一个接口,但是我更喜欢叫它标准,所有符合这个标准的类均归为同一类
{
    public void howToWork();
}
class Car implements Vehicle      //汽车类实现交通工具接口(或者说是汽车类符合交通工具标准)
{
    public void howToWork()
    {
        System.out.println("开车去!");
    }
}
class Bike implements Vehicle 
{
    public void howToWork()
    {
        System.out.println("骑车去!");
    }
}
class Airplane implements Vehicle
{
    public void howToWork()
    {
        System.out.println("坐飞机去!");
    }
}
class Factory             //工厂类
{ 
    public static Vehicle create(String vehicle)  //实现一个静态方法用于创建实例
    {
        Vehicle sample = null;
        switch(vehicle)
        {
            case "car":
                sample = new Car();
                break;</span><span style="font-size:18px;">
            case "bike":
                sample = new Bike();
                break;
            case "airplane" :
                sample = new Airplane();
                break;
        }
        return sample;
    }
}
public class SimpleFactory
{
    public static void main(String args[])
    {
        Vehicle vehicle = Factory.create(args[0]);
        vehicle.howToWork();
    }
}</span>
由上面的代码可以看出,简单工厂的核心就是一个SimpleFactory类,他拥有必要的逻辑判断能力和所有产品的创建权利,我们只需要向把定单给他,就能得到我们想要的产品。这使用起来似乎非常方便。
但,实际上,这个SimpleFactory有很多的局限。首先,我们每次想要增加一种新产品的时候,都必须修改SimpleFactory的原代码。其次,当我们拥有很多很多产品的时候,而且产品之间又存在复杂的层次关系的时候,这个类必须拥有复杂的逻辑判断能力,其代码量也将不断地激增,这对以后的维护简直就是恐怖两个字...
还有就是,整个系统都严重依赖SimpleFactory类,只要SimpleFactory类一出问题,系统就进入不能工作的状态,这也是最为致命的一点....
以上的不足将在工厂模式的另外两种状态中得到解决。

2.工厂方法(Factory Method)

<span style="font-size:14px;">interface Vehicle        //写一个接口,但是我更喜欢叫它标准,所有符合这个标准的类均归为同一类
{
    public void howToWork();
}
interface Factory    //工厂方法为工厂类定义了接口,用多态来削弱了工厂类的职能
{
    public Vehicle create();
}

class Car implements Vehicle      //汽车类实现交通工具接口(或者说是汽车类符合交通工具标准)
{
    public void howToWork()
    {
        System.out.println("开车去!");
    }
}
class Bike implements Vehicle 
{
    public void howToWork()
    {
        System.out.println("骑车去!");
    }
}
class Airplane implements Vehicle
{
    public void howToWork()
    {
        System.out.println("坐飞机去!");
    }
}

class UseCar implements Factory
{
    public Vehicle create()
    {
        return new Car();
    }
}
class UseBike implements Factory
{
    public Vehicle create()
    {
        return new Bike();
    }
}
class UseAirplane implements Factory
{
    public Vehicle create()
    {
        return new Airplane();
    }
}
public class FactoryMethod
{
    public static void main(String args[])
    {
        Vehicle vehicle = null;
        if(args[0].equals("car"))
        {
            vehicle = new UseCar().create();
            vehicle.howToWork();
        }
        if(args[0].equals("bike"))
        {
            vehicle = new UseBike().create();
            vehicle.howToWork();
        }
        if(args[0].equals("airplane"))
        {
            vehicle = new UseAirplane().create();
            vehicle.howToWork();
        }
    }
}</span>
3.抽象工厂(Factory Method)

<span style="font-size:14px;">interface Vehicle        //写一个接口,但是我更喜欢叫它标准,所有符合这个标准的类均归为同一类
{
    public String howToWork();
}
class Car implements Vehicle      //汽车类实现交通工具接口(或者说是汽车类符合交通工具标准)
{
    public String howToWork()
    {
        String str = "开车去";
        return str;
    }
}
class Bike implements Vehicle 
{
    public String howToWork()
    {
        String str = "骑车去";
        return str;
    }
}
class Airplane implements Vehicle
{
    public String howToWork()
    {
       String str = "开飞机去";
       return str;
    }
}
//==============================================
interface Breakfast         //早餐接口
{
    public String EatWhat();
}
class Bread implements Breakfast
{
    public String EatWhat()
    {
        String str = "面包";
        return str;
    }
}
class Egg implements Breakfast
{
    public String EatWhat()
    {
        String str = "鸡蛋";
        return str;
    }
}
class Noodles implements Breakfast
{
    public String EatWhat()
    {
        String str = "面条";
        return str;
    }
}
//============================================================
interface Factory     //创建工厂接口
{
    public Vehicle createVehicle();     //定义交通工具
    public Breakfast createBreakfast(); //定义早餐
}
class Poor implements Factory           //实现穷人上班和早餐
{
    public Vehicle createVehicle()
    {
        return new Bike();
    }
    public Breakfast createBreakfast()
    {
        return new Bread();
    }
}
class Rich implements Factory           //实现富人上班和早餐
{
    public Vehicle createVehicle()
    {
        return new Car();
    }
    public Breakfast createBreakfast()
    {
        return new Egg();
    }
}
//=======================================
public class AbstractFactory
{
    public static void main(String args[])
    {
       if(args[0].equals("poor"))
       {
           Poor poor = new Poor();
           System.out.println("穷人早上吃完" + poor.createBreakfast().EatWhat() + poor.createVehicle().howToWork()+"上班");
       }
       else if(args[0].equals("rich"))
       {
           Rich rich = new Rich();
           System.out.println("富人早上吃完" + rich.createBreakfast().EatWhat() + rich.createVehicle().howToWork()+"上班");
       }
    }
}</span><span style="font-size:18px;color: rgb(153, 0, 0);">
</span>



你可能感兴趣的:(设计模式)