1. 程式人生 > >【設計模式】使用unity實現外觀模式(Facade mode)

【設計模式】使用unity實現外觀模式(Facade mode)

閱讀《大話設計模式》後,自己根據unity的特性寫的一個簡單demo,如有不妥之處,歡迎評論糾正....

首先貼上書中關於外觀模式的程式碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 外觀模式
{
    class Facade
    {
        SubsystemOne one;
        SubsystemTwo two;
        SubsystemThree three;
        SubsystemFour four;

        public Facade()
        {
            one = new SubsystemOne();
            two = new SubsystemTwo();
            three = new SubsystemThree();
            four = new SubsystemFour();
        }

        public void MethodA()
        {
            Console.WriteLine("方法組A()-------");
            one.MethodOne();
            two.MethodTwo();
            three.MethodThree();
        }

        public void MethodB()
        {
            Console.WriteLine("方法組B()------");
            three.MethodThree();
            two.MethodTwo();
        }
    }

    public class SubsystemOne
    {
        public void MethodOne()
        {
            Console.WriteLine("子系統方法1");
        }
    }
    public class SubsystemTwo
    {
        public void MethodTwo()
        {
            Console.WriteLine("子系統方法2");
        }
    }
    public class SubsystemThree
    {
        public void MethodThree()
        {
            Console.WriteLine("子系統方法3");
        }
    }
    public class SubsystemFour
    {
        public void MethodFour()
        {
            Console.WriteLine("子系統方法4");
        }
    }

}
客戶端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 外觀模式
{
    class Program
    {
        static void Main(string[] args)
        {
            Facade f = new Facade();

            f.MethodA();
            f.MethodB();

            Console.ReadKey();
        }
    }
}

然後下面是我寫的使用unity實現外觀模式(facade mode)的unity程式碼
using UnityEngine;
using System.Collections;

public class Facade
{
    UIMethodA a;
    UIMethodB b;

    public Facade()
    {
        a = new UIMethodA();
        b = new UIMethodB();
    }

    public void MethodA(Transform transform)
    {
        a.MethodA(transform);
    }

    public void MethodB(Transform transform)
    {
        b.MethodB(transform);
    }
}

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UI
{
    protected Text mText;
    protected Text mBtnText;
    protected Image mImage;
    protected void InitUI(Transform transform)
    {
        Button button = transform.Find("Button").GetComponent<Button>();
        button.onClick.AddListener(OnClick);

        mText = transform.Find("Text").GetComponent<Text>();
        mBtnText = transform.Find("Button/Text").GetComponent<Text>();
        mImage = transform.Find("Image").GetComponent<Image>();
    }

    protected virtual void OnClick()
    {

    }
}

public class UIMethodA : UI
{
    protected override void OnClick()
    {
        Debug.Log("方法組A");
    }

    public  void MethodA(Transform transform)
    {
        //這裡設計的不是很合理,但是不知道要怎麼改,以後想到了在做更改
        InitUI(transform);
        mText.text = "方法A";
        mBtnText.text = "方法A按鈕";
        mImage.color = Color.green;
    }
}

public class UIMethodB : UI
{
    protected override void OnClick()
    {
        Debug.Log("方法組B");
    }

    public void MethodB(Transform transform)
    {
        //這裡設計的不是很合理,但是不知道要怎麼改,以後想到了在做更改
        InitUI(transform);
        mText.text = "方法B";
        mBtnText.text = "方法B按鈕";
        mImage.color = Color.yellow;
    }
}
Test.cs
using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour
{
    void OnGUI()
    {
        if(GUI.Button(new Rect(100, 100, 120, 50),"方法組A"))
        {
            Facade f = new Facade();
            f.MethodA(this.transform);
        }
        if (GUI.Button(new Rect(100, 200, 120, 50), "方法組B"))
        {
            Facade f = new Facade();
            f.MethodB(this.transform);
        }

    }

}


效果圖:



然後下面是我寫的使用unity實現模板模式(template mode)的unity程式碼 然後下面是我寫的使用unity實現模板模式(template mode)的unity程式碼