1. 程式人生 > >【設計模式】使用unity實現代理模式(Proxy mode)

【設計模式】使用unity實現代理模式(Proxy mode)

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

先貼上書中的程式碼:

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

namespace 代理模式
{
    public abstract class Subject
    {
        public abstract void Request();
    }

    public class RealSubject : Subject 
    {
        public override void Request()
        {
            Console.WriteLine("真實的請求!!!");
        }
    }

    class Proxy : Subject 
    {
        private RealSubject realSubject;

        public override void Request()
        {
            if(realSubject == null)
            {
                realSubject = new RealSubject();
            }

            realSubject.Request();
        }
    }
}

客戶端程式碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 代理模式
{
    class Program
    {
        static void Main(string[] args)
        {
            Proxy proxy = new Proxy();
            proxy.Request();

            Console.ReadKey();
        }
    }
}
然後下面是自己根據書中的思路實現的代理模式的程式碼,能夠代理Text, Button, Image等元件的功能:

UIView.cs

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;

public class UIView  {
    private Text mText;
   // private Button mButton;
    private Text mBtnText;
    private Image mImage;

    private Action Callback;

    public UIView() { }


    public void Init(Transform transform, Action callback, Color imageColor, string txt = "", string btnTxt = "")
    {
        InitUI(transform);
        SetText(txt);
        SetBtnText(btnTxt);
        SetImage(imageColor);
        Callback = callback;
    }

    private void InitUI(Transform transform)
    {
        mText = transform.Find("Text").GetComponent<Text>();
        Button mButton = transform.Find("Button").GetComponent<Button>();
        mBtnText = transform.Find("Button/Text").GetComponent<Text>();
        mImage = transform.Find("Image").GetComponent<Image>();

        mButton.onClick.AddListener(onClick);
    }

    private void onClick()
    {
        if(Callback != null)
        {
            Callback();
        }
    }

    private void SetText(string txt)
    {
        mText.text = txt;
    }

    private void SetBtnText(string txt)
    {
        mBtnText.text = txt;
    }

    private void SetImage(Color color)
    {
        mImage.color = color;
    }
}

ProxyUIView.cs
using UnityEngine;
using System.Collections;
using System;
public class ProxyUIView
{
    public static void Proxy(Transform transform, Color imageColor, Action callback, string txt = "", string btnTxt = "")
    {
        UIView v = new UIView();
        v.Init(transform, callback, imageColor, txt, btnTxt);
    }
}


Test.cs:

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

	void OnGUI()
    {
        if(GUI.Button(new Rect(100, 100, 120,50), "代理者"))
        {
            ProxyUIView.Proxy(this.transform, Color.green, Callback, "代理者", "代理者按鈕");
        }
    }
     
    private void Callback()
    {
        print("代理成功");
    }
}
只要把test.cs掛到canvas上即可。
執行效果,點選button就會log出內容