03 工厂模式

using UnityEngine;

/// <summary>
/// 功能说明:简单工厂模式
/// </summary>
public class DPFactory:MonoBehaviour
{
    private void Start()
    {
        FactoryIPhone8 factoryIPhone8 = new FactoryIPhone8();
        factoryIPhone8.CreateIPhone();
        FactoryIPhoneX factoryIPhoneX = new FactoryIPhoneX();
        factoryIPhoneX.CreateIPhone();
    }
}


//工厂模式分为简单工厂模式,工厂方法模式,抽象工厂模式

//简单工厂模式
public class IPhone
{
    public IPhone()
    {

    }
}
public class IPhone8 : IPhone
{
    public IPhone8() {

        Debug.Log("创建IPhone8");
    }
}
public class IPhoneX : IPhone
{
    public IPhoneX() {

        Debug.Log("创建IPhoneX");
    }
}

public interface IFactory
{
    IPhone CreateIPhone();
}

public class FactoryIPhone8 : IFactory
{
    public IPhone CreateIPhone()
    {
        return new IPhone8();
    }
}
public class FactoryIPhoneX : IFactory
{
    public IPhone CreateIPhone()
    {
        return new IPhoneX();
    }
}
using UnityEngine;

/// <summary>
/// 功能说明:抽象工厂模式
/// </summary>
public class DPFactory:MonoBehaviour
{
    private void Start()
    {
        FactoryIPhone8 factoryIPhone8 = new FactoryIPhone8();
        factoryIPhone8.CreateIPhone();
        factoryIPhone8.CreateIPhoneCharger();
        FactoryIPhoneX factoryIPhoneX = new FactoryIPhoneX();
        factoryIPhoneX.CreateIPhone();
        factoryIPhoneX.CreateIPhoneCharger();
    }
}


//工厂模式分为简单工厂模式,工厂方法模式,抽象工厂模式

//抽象工厂模式

//手机
public interface IPhone
{

}
public class IPhone8 : IPhone
{
    public IPhone8() {

        Debug.Log("创建IPhone8");
    }
}
public class IPhoneX : IPhone
{
    public IPhoneX() {

        Debug.Log("创建IPhoneX");
    }
}

//充电器
public interface IPhoneCharger
{

}

public class IPhone8Charger:IPhoneCharger
{
   public  IPhone8Charger()
    {
        Debug.Log("创建IPhone8Charger充电器");
    }
}
public class IPhoneXCharger:IPhoneCharger
{
    public IPhoneXCharger()
    {
        Debug.Log("创建IPhoneXCharger充电器");
    }
}

public interface IFactory
{
    IPhone CreateIPhone();
    IPhoneCharger CreateIPhoneCharger();
}

public class FactoryIPhone8 : IFactory
{

    public IPhone CreateIPhone()
    {
        return new IPhone8();
    }

    public IPhoneCharger CreateIPhoneCharger()
    {
      return  new IPhone8Charger();
    }
}
public class FactoryIPhoneX : IFactory
{

    public IPhone CreateIPhone()
    {
        return new IPhoneX();
    }

    public IPhoneCharger CreateIPhoneCharger()
    {
       return  new IPhoneXCharger();
    }
}

 

posted on 2024-02-25 22:57  莫凡-荒天帝  阅读(1)  评论(0编辑  收藏  举报

导航