簡單工廠和抽象工廠(二次整理)

簡單工廠:

上次整理過簡單工廠,爲了加深印象,抽時間又再次整理了一下.iphone

簡單工廠的建立通常爲如下幾個步驟:ide

1).建立父類(虛方法或抽象方法)spa

       Abstract/virturecode

2).建立子類(繼承父類,通常都重寫父類方法以達到所需效果)對象

     overrideblog

    2.1).添加一個枚舉類型(能夠不須要,便於傳入參數,在工廠中被判斷)繼承

3).建立工廠(傳入一個所需物體的參數(能夠爲枚舉),傳出一個物體對象,就像一個工廠同樣)ip

4).實現it

   廢話很少說,代碼走起!io

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MySimpleFactory : MonoBehaviour {

    
    void Start () 
    {
        FatherMobliePhone APhone = MobliePhoneFactory01.CreatMiblePhone(PhoneType01.iphone);
        APhone.ToDo();

    }
    
    
    void Update () {
        
    }
}
//建立父類
public abstract class FatherMobliePhone 
{
    public abstract void ToDo();
}
//子類繼承父類
public class Iphone : FatherMobliePhone
{
    public override void ToDo()
    {
        Debug.Log("My Name Is Iphone .");
    }
}
public class HuaWei : FatherMobliePhone
{
    public override void ToDo()
    {
        Debug.Log("My Name Is HuaWei .");
    }
}
public class ViVo : FatherMobliePhone
{
    public override void ToDo()
    {
        Debug.Log("My Name Is ViVo .");
    }
}
//建立枚舉類型
public enum PhoneType01 
{
  iphone,
    ViVo,
    HuaWei
}
//建立工廠
public class MobliePhoneFactory01
{
    //傳入一個手機類型,在此返回的是一個手機對象!!!!
    public static FatherMobliePhone CreatMiblePhone(PhoneType01 MyType)
    { 
        FatherMobliePhone mMobliePhone =null;
        switch (MyType)
        {
            case PhoneType01.iphone:
                mMobliePhone=new Iphone();
                break;
            case PhoneType01.ViVo:
                mMobliePhone = new ViVo();
                break;
            case PhoneType01.HuaWei:
                mMobliePhone = new HuaWei();
                break;
            default:
                break;
        }

        return mMobliePhone;
    }

}
View Code

 

抽象工廠:

看過了簡單工廠就來感覺一下抽象工廠.抽象工廠也就是工廠方法(特色:一個物體對應一個工廠,工廠具體化,企業化!)

先來看一下大致思路:

代碼展現:

 

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AbstractFactoryTest : MonoBehaviour//實現 
{
    void Start ()
    {
        AbstractFactory xiaomiFactory= new XiaomiFactory();
        MotherBoard motherBoard = xiaomiFactory.CreateMotherBoard();
        motherBoard.Print();
        Screen screen = xiaomiFactory.CreateScreen();
        screen.Print();
    }    
    void Update () 
    {
        
    }
}

public abstract class AbstractFactory// 抽象工廠父類
{
    public abstract Screen CreateScreen();
    public abstract MotherBoard CreateMotherBoard();
}

public abstract class Screen// 抽象屏幕父類
{
    public abstract void Print();
}
public abstract class MotherBoard// 抽象主板父類
{
    public abstract void Print();
}

//繼承屏幕的子類
public class AppleScreen : Screen
{
    public override void Print()
    {
        Debug.Log("蘋果手機屏幕");
    }
}
public class XiaoMiScreen : Screen
{
    public override void Print()
    {
        Debug.Log("小米屏幕");
    }
}

//繼承主板的子類
public class AppleMotherBoard : MotherBoard
{
    public override void Print()
    {
        Debug.Log("蘋果主板");
    }
}
public class XiaoMiMotherBoard : MotherBoard
{
    public override void Print()
    {
        Debug.Log("小米主板");
    }
}


public class AppleFactory : AbstractFactory//繼承主工廠的子類---->蘋果專用
{
    public override MotherBoard CreateMotherBoard()
    {
        return new AppleMotherBoard();
    }

    public override Screen CreateScreen()
    {
        return new AppleScreen();
    }
}

public class XiaomiFactory : AbstractFactory//繼承主工廠的子類---->小米專用
{
    public override MotherBoard CreateMotherBoard()
    {
        return new XiaoMiMotherBoard();
    }

    public override Screen CreateScreen()
    {
        return new XiaoMiScreen();
    }
}
View Code
相關文章
相關標籤/搜索