Unity implements design patterns - mediator pattern

Unity implements design patterns - mediator pattern

Use a mediator object to encapsulate a series of object interactions. The mediator allows each object to reference each other without explicit reference, so that it is loosely coupled and can independently change the interaction between them.
Insert image description here
Here we use a real-life example to introduce the intermediary model. For example, when we are chatting in a chat hall, each person can send messages to the corresponding person based on their name. Then does person A have to hold the name information of the other people? If so, If a new person suddenly joins, do you need to inform everyone that there is an extra person? Obviously, this design method is very cumbersome. We can transform it into a star structure centered on the intermediary, which greatly decouples it.
Insert image description here

1.AbstractChatroom(The ‘Mediator’ abstract class)

Abstract base class for chat lobby

    abstract class AbstractChatroom
    {
    
    
        public abstract void Register(Participant participant);
        public abstract void Send(string from, string to, string message);
    }

2.Chatroom(The ‘ConcreteMediator’ class)

    class Chatroom : AbstractChatroom
    {
    
    
        private Dictionary<string, Participant> _participants =
          new Dictionary<string, Participant>();

        public override void Register(Participant participant)
        {
    
    
            if (!_participants.ContainsValue(participant))
            {
    
    
                _participants[participant.Name] = participant;
            }

            participant.Chatroom = this;
        }

        public override void Send( string from, string to, string message)
        {
    
    
            Participant participant = _participants[to];

            if (participant != null)
            {
    
    
                participant.Receive(from, message);
            }
        }
    }

3.Participant(The ‘AbstractColleague’ class)

Chat participant base class

    class Participant
    {
    
    
        private Chatroom _chatroom;
        private string _name;

        // Constructor
        public Participant(string name)
        {
    
    
            this._name = name;
        }

        // Gets participant name
        public string Name
        {
    
    
            get {
    
     return _name; }
        }

        // Gets chatroom
        public Chatroom Chatroom
        {
    
    
            set {
    
     _chatroom = value; }
            get {
    
     return _chatroom; }
        }

        // Sends message to given participant
        public void Send(string to, string message)
        {
    
    
            _chatroom.Send(_name, to, message);
        }

        // Receives message from given participant
        public virtual void Receive(string from, string message)
        {
    
    
            Debug.Log(from + " to " + Name + ": '" + message + "'");
        }
    }

4.Beatle( A ‘ConcreteColleague’ class)

Chat participant subclass

    class Beatle : Participant
    {
    
    
        // Constructor
        public Beatle(string name)
          : base(name)
        {
    
    
        }

        public override void Receive(string from, string message)
        {
    
    
            Debug.Log("To a Beatle: ");
            base.Receive(from, message);
        }
    }

5.NonBeatle(A ‘ConcreteColleague’ class)

Chat participant subclass

    class NonBeatle : Participant
    {
    
    
        // Constructor
        public NonBeatle(string name)
          : base(name)
        {
    
    
        }

        public override void Receive(string from, string message)
        {
    
    
            Debug.Log("To a non-Beatle: ");
            base.Receive(from, message);
        }
    }

6. Test

    public class MediatorExample1 : MonoBehaviour
    {
    
    
        void Start()
        {
    
    
            // Create chatroom
            Chatroom chatroom = new Chatroom();

            // Create participants and register them
            Participant George = new Beatle("George");
            Participant Paul = new Beatle("Paul");
            Participant Ringo = new Beatle("Ringo");
            Participant John = new Beatle("John");
            Participant Yoko = new NonBeatle("Yoko");

            chatroom.Register(George);
            chatroom.Register(Paul);
            chatroom.Register(Ringo);
            chatroom.Register(John);
            chatroom.Register(Yoko);

            // Chatting participants
            Yoko.Send("John", "Hi John!");
            Paul.Send("Ringo", "All you need is love");
            Ringo.Send("George", "My sweet Lord");
            Paul.Send("John", "Can't buy me love");
            John.Send("Yoko", "My sweet love");

        }
    }

Insert image description here

Guess you like

Origin blog.csdn.net/zzzsss123333/article/details/133409908