通知 网站从因情语写改为晴雨,这个网站的模板也从calmlog_ex改为 whimurmur

创建型模式-工厂相关模式

1268人浏览 / 0人评论 / | 作者:因情语写  | 分类: 设计模式与算法  | 标签: 设计模式与算法  | 

作者:因情语写

链接:https://www.proprogrammar.com/article/438

声明:请尊重原作者的劳动,如需转载请注明出处


  这里我们来说一下简单工厂,工厂方法,抽象工厂

  可以看这篇文章:详解设计模式之工厂模式(简单工厂+工厂方法+抽象工厂)

  简单工厂

  先说说简单工厂,简单工厂模式是属于创建型模式,又叫做静态工厂方法(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();
    }
}

  运行结果


亲爱的读者:有时间可以点赞评论一下

点赞(0) 打赏

全部评论

还没有评论!
广告位-帮帮忙点下广告