Giter Club home page Giter Club logo

unity-design-pattern's Introduction

Unity-Design-Pattern

Unity Design Pattern

๐Ÿ‘ฎ Command

Real world example

A generic example would be you ordering food at a restaurant. You (i.e. Client) ask the waiter (i.e. Invoker) to bring some food (i.e. Command) and waiter simply forwards the request to Chef (i.e. Receiver) who has the knowledge of what and how to cook. Another example would be you (i.e. Client) switching on (i.e. Command) the television (i.e. Receiver) using a remote control (Invoker).

In plain words

Allows you to encapsulate actions in objects. The key idea behind this pattern is to provide the means to decouple client from receiver.

Wikipedia says

In object-oriented programming, the command pattern is a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time. This information includes the method name, the object that owns the method and values for the method parameters.

Programmatic Example

First of all we have the receiver that has the implementation of every action that could be performed

// Receiver
using UnityEngine;

// Receiver
public class Bulb : MonoBehaviour
{
    public void TurnOn()
    {
        print("Bulb has been lit");
    }

    public void TurnOff()
    {
        print("Darkness!");
    }
}

then we have an interface that each of the commands are going to implement and then we have a set of commands

image

public Bulb bulb;

public interface ICommand
{
    void Execute();
    void Undo();
    void Redo();
}

//Command

class TurnOn : ICommand
{
    private Bulb _bulb;

    public TurnOn(Bulb bulb)
    {
        _bulb = bulb;
    }

    public void Execute()
    {
        _bulb.TurnOn();
    }

    public void Undo()
    {
        _bulb.TurnOff();
    }

    public void Redo()
    {
        Execute();
    }
}

//Command

class TurnOff : ICommand
{
    private Bulb _bulb;

    public TurnOff(Bulb bulb)
    {
        _bulb = bulb;
    }

    public void Execute()
    {
        _bulb.TurnOff();
    }

    public void Undo()
    {
        _bulb.TurnOn();
    }

    public void Redo()
    {
        Execute();
    }
}

// Invoker
public class RemoteControl
{
    public void Submit(ICommand command)
    {
        command.Execute();
    }
}

Then we have an Invoker with whom the client will interact to process any commands

// Invoker
class RemoteControl
{
    public void Submit(ICommand command)
    {
        command.Execute();
    }
}

Finally let's see how we can use it in our client

 var turnOn = new TurnOn(bulb);
 var turnOff = new TurnOff(bulb);

 var remote = new RemoteControl();
        
 remote.Submit(turnOn); // Bulb has been lit!
 remote.Submit(turnOff); // Darkness!

Command pattern can also be used to implement a transaction based system. Where you keep maintaining the history of commands as soon as you execute them. If the final command is successfully executed, all good otherwise just iterate through the history and keep executing the undo on all the executed commands.

Real World Examples

Simon Memory Game

memory game

Photoshop History

photoshop

Another way to Implement Command Pattern is using a List of Actions.

Action is a Delegate. It is defined like this:

public delegate void Action();

You could create your own delegate types similarly to how you would create abstract methods; you write the signature but no implementation. You then create instances of these delegates by taking the reference of a method.

Delegate in C# is a reference type, which holds a reference to the function and invokes the function when called with an Invoke method. If one is coming from C++/C background, delegate is like a pointer to a function.

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

public class Example : MonoBehaviour
{
   public List<Action> actions = new List<Action>();

   private void Start()
   {
      actions.Add(Method1);
      actions.Add(Method2);
      actions.Add(Method3);

      StartCoroutine(Replay());
   }


   public void Method1()
   {
     print(nameof(Method1)); 
   }  
   
   public void Method2()
   {
     print(nameof(Method2));
   }   
   
   public void Method3()
   {
     print(nameof(Method3));
   }

   public IEnumerator Replay()
   {
      foreach (var action in actions.ToList())
      {
         action.Invoke();
         yield return new WaitForSeconds(0.5f);
      }
   }
}

Let Your Players Undo Their In-Game Mistakes With the Command Pattern

Best approach to undo and redo is using Stack

private Stack<Command> undos = new Stack<Command>();
private Stack<Command> redos = new Stack<Command>();

image

cmdUnity08

unity-design-pattern's People

Contributors

smkplus avatar

Stargazers

Matt avatar Hengel avatar Man Sun avatar mika avatar Jesse Talavera avatar Endar avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

isaveu

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.