1. 程式人生 > >建立型模式-工廠相關模式 詳解設計模式之工廠模式(簡單工廠+工廠方法+抽象工廠)

建立型模式-工廠相關模式 詳解設計模式之工廠模式(簡單工廠+工廠方法+抽象工廠)

  這裡我們來說一下簡單工廠,工廠方法,抽象工廠

  可以看這篇文章:詳解設計模式之工廠模式(簡單工廠+工廠方法+抽象工廠)

  簡單工廠

  先說說簡單工廠,簡單工廠模式是屬於建立型模式,又叫做靜態工廠方法(Static Factory Method)模式,但不屬於23種GOF設計模式之一

  看程式碼自己感受下:

  車輛型別

package creational.pattern.factory.simplefactory;

public enum CarType {
    SportCarType(0),
    JeepCarType(1),
    HatchbackCarType(
2); private int value; private CarType(int value) { this.value = value; } public int getValue() { return value; } }

  車介面與實現

package creational.pattern.factory.simplefactory;

/// <summary>
/// 抽象產品類: 汽車
/// </summary>
public interface ICar
{
    
void GetCar(); }
package creational.pattern.factory.simplefactory;

public class HatchbackCar implements ICar {

    public void GetCar()
    {
        System.out.println("場務把兩箱車交給範·迪塞爾");
    }

}
package creational.pattern.factory.simplefactory;

public class JeepCar implements ICar {

    
public void GetCar() { System.out.println("場務把越野車交給範·迪塞爾"); } }
package creational.pattern.factory.simplefactory;

public class SportCar implements ICar{
    public void GetCar()
    {
        System.out.println("場務把跑車交給範·迪塞爾");
    }
}

  簡單工廠

package creational.pattern.factory.simplefactory;

public class Factory {
    public ICar GetCar(CarType carType)
        throws Exception
    {
        // switch 中用列舉
        switch (carType)
        {
            case SportCarType:
                return new SportCar();
            case JeepCarType:
                return new JeepCar();
            case HatchbackCarType:
                return new HatchbackCar();
            default:
                throw new Exception("愛上一匹野馬,可我的家裡沒有草原. 你走吧!");
        }
    }
}

  測試程式碼

package creational.pattern.factory.simplefactory;

public class Client {
    public static void main(String[] args) {
        ICar car;
        try
        {
            Factory factory = new Factory();

            System.out.println("範·迪塞爾下一場戲開跑車。");
            car = factory.GetCar(CarType.SportCarType);
            car.GetCar();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

}

  簡單的呼叫工廠的方法,傳參獲取想要的實現類

  執行結果

  工廠方法

  工廠方法對不同的實現類呼叫不用的工廠類

  汽車及其實現類

package creational.pattern.factory.factorymethod;

public interface ICar {
    void GetCar();
}
package creational.pattern.factory.factorymethod;

public class HatchbackCar implements ICar {

    @Override
    public void GetCar() {
        System.out.println("場務把兩箱車交給範·迪塞爾");
    }

}
package creational.pattern.factory.factorymethod;

public class JeepCar implements ICar {

    @Override
    public void GetCar() {
        System.out.println("場務把越野車交給範·迪塞爾");
    }

}
package creational.pattern.factory.factorymethod;

public class SportCar implements ICar {

    @Override
    public void GetCar()
    {
        System.out.println("場務把跑車交給範·迪塞爾");
    }

}

  工廠及其實現類

package creational.pattern.factory.factorymethod;

public interface IFactory {
    ICar CreateCar();
}
package creational.pattern.factory.factorymethod;

public class HatchbackFactory implements IFactory {

    public ICar CreateCar()
    {
        return new HatchbackCar();
    }

}
package creational.pattern.factory.factorymethod;

public class JeepFactory implements IFactory {

    public ICar CreateCar()
    {
        return new JeepCar();
    }
}
package creational.pattern.factory.factorymethod;

public class SportFactory implements IFactory {

    public ICar CreateCar()
    {
        return new SportCar();
    }

}

  測試程式碼

package creational.pattern.factory.factorymethod;

public class Client {
    public static void main(String[] args) {
        IFactory factory = new JeepFactory();
        ICar car = factory.CreateCar();
        System.out.println("範·迪塞爾下一場戲開吉普");
        car.GetCar();
    }
}

  執行結果

  抽象方法

  抽象方法可以建立多個相關物件例項,進行使用

  車抽象類及實現

package creational.pattern.factory.abstractmethod;

/// <summary>
/// 抽象產品: 車抽象類
/// </summary>
public abstract class AbstractCar
{
    /// <summary>
    /// 車的型別屬性
    /// </summary>
    private String type;

    /// <summary>
    /// 車的顏色屬性
    /// </summary>
    private String color;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}
package creational.pattern.factory.abstractmethod;

public class JeepCar extends AbstractCar {
    private String type = "Jeep";
    private String color = "Green";
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
}
package creational.pattern.factory.abstractmethod;

public class SportCar extends AbstractCar {
    private String type = "Sport";
    private String color = "Red";
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
}

  包抽象類及實現類

package creational.pattern.factory.abstractmethod;

public abstract class AbstractBackpack {
    /// <summary>
    /// 包的型別屬性
    /// </summary>
    private String type;

    /// <summary>
    /// 包的顏色屬性
    /// </summary>
    private String color;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}
package creational.pattern.factory.abstractmethod;

public class JeepBackpack extends AbstractBackpack {
    private String type = "Jeep";
    private String color = "Green";
    
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
}
package creational.pattern.factory.abstractmethod;

public class SportBackpack extends AbstractBackpack {
    private String type = "Sport";
    private String color = "Red";
    
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
}

  運動裝備(車,揹包)抽象類及其實現類

package creational.pattern.factory.abstractmethod;

public abstract class AbstractEquipment {
    /// <summary>
    /// 抽象方法: 建立一輛車
    /// </summary>
    /// <returns></returns>
    public abstract AbstractCar CreateCar();

    /// <summary>
    /// 抽象方法: 建立揹包
    /// </summary>
    /// <returns></returns>
    public abstract AbstractBackpack CreateBackpack();
}
package creational.pattern.factory.abstractmethod;

public class JeepEquipment extends AbstractEquipment {

    @Override
    public AbstractCar CreateCar() {
        return new JeepCar();
    }

    @Override
    public AbstractBackpack CreateBackpack() {
        return new JeepBackpack();
    }

}
package creational.pattern.factory.abstractmethod;

public class SportEquipment extends AbstractEquipment {

    @Override
    public AbstractCar CreateCar() {
        return new SportCar();
    }

    @Override
    public AbstractBackpack CreateBackpack() {
        return new SportBackpack();
    }

}

  使用運動裝備的類

package creational.pattern.factory.abstractmethod;

public class CreateEquipment {
    private AbstractCar fanCar;
    private AbstractBackpack fanBackpack;
    public CreateEquipment(AbstractEquipment equipment)
    {
        fanCar = equipment.CreateCar();
        fanBackpack = equipment.CreateBackpack();
    }

    public void ReadyEquipment()
    {
        System.out.println(String.format("老範揹著%s色%s包開著%s色%s車",
            fanBackpack.getColor(),
            fanBackpack.getType(),
            fanCar.getColor(),
            fanCar.getType()
            ));
    }
}

  測試類

package creational.pattern.factory.abstractmethod;

public class Client {
    public static void main(String[] args) {
        AbstractEquipment abstractEquipment = new JeepEquipment();
        CreateEquipment createEquipment = new CreateEquipment(abstractEquipment);
        createEquipment.ReadyEquipment();
    }
}

  執行結果