Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/dancher743/unity-mvp

Implementation of MVP (Model-View-Presenter) architectural pattern via Unity engine.
https://github.com/dancher743/unity-mvp

architectural-pattern csharp-code framework model-view-presenter mvp unity unity-framework unity-mvp

Last synced: about 1 month ago
JSON representation

Implementation of MVP (Model-View-Presenter) architectural pattern via Unity engine.

Awesome Lists containing this project

README

        

# unity-mvp
Implementation of MVP (Model-View-Presenter) architectural pattern via Unity engine.

Sample
---
Before to start, it's recommended to get [sample project](https://github.com/dancher743/unity-mvp/releases/tag/sample-project) with the latest version of the package.

Getting Started
---
### What is MVP?
**MVP** or **Model-View-Presenter** is an architectural pattern, which consists of three components: _Model_, _View_ and _Presenter_.

* _Model_ is a data.
* _View_ is an interface that displays data and routes user commands to Presenter.
* _Presenter_ wires up Model and View together and thereby creates a functioning entity.

[![MVP-diagram.png](https://i.postimg.cc/jSCcjt5W/MVP-diagram.png)](https://postimg.cc/w18LfKDH)

For more information about MVP, check an original source - ["MVP: Model-View-Presenter. The Taligent Programming Model for C++ and Java." Mike Potel](http://www.wildcrest.com/Potel/Portfolio/mvp.pdf).

### Creating a Model
Implement `IModel` interface to create a _Model_ -

```
public class CubeModel : IModel
{
// ...
}
```

### Creating a View
Implement `IView` interface to create a _View_ -

```
public class CubeView : MonoBehaviour, IView
{
// ...
}
```

You can also use `MonoView` class as an "stub" instead of `MonoBehaviour` -

`public class CubeView : MonoView, IView`

### Creating a Presenter
Create a `CubePresenter` class and derive it from `Presenter`. Specify types: `TView` and `TModel`. In our case `TModel` is `CubeModel` and `TView` is `CubeView` -

`CubePresenter : Presenter`

```
public class CubePresenter : Presenter
{
public CubePresenter(CubeView cubeView, CubeModel cubeModel) : base(cubeView, cubeModel)
{
// ...
}
}
```

At this point we're done with the main components of MVP - `CubeModel`, `CubeView` and `CubePresenter`!

Instancing
---
To create an instance of a `Presenter` use `Create()` method in `PresenterFactory` -

```
[SerializeField]
private CubeView cubeView;

// ...

private CubePresenter cubePresenter;

// ...

void Start()
{
cubePresenter = presenterFactory.Create(cubeView, new CubeModel());
}
```
`PresenterFactory` is built-in implementation of `IPresenterFactory` interface -

```
public interface IPresenterFactory
{
public TPresenter Create(params object[] data) where TPresenter : IPresenter;
}
```

But you can implement your own factory.

Messaging
---
### Message Dispatcher
Each `Presenter` should interact with another `Presenter`. One possible way to do it is to use messages. `MessageDispatcher` is a class which provides needed functionality for messaging.

But to receive a _Message_ we need a _Subscriber_.

### Receive a Message
Implement `IMessageSubscriber` interface to make some class available for message receiving -

```
public interface IMessageSubscriber
{
void ReceiveMessage(TMessage message);
}
```

In the example we have `UIPresenter` -

```
public class UIPresenter : Presenter, IMessageSubscriber
{
// ...

void IMessageSubscriber.ReceiveMessage(TMessage message)
{
switch (message)
{
case CubeColorMessage cubeColorMessage:
model.ColorText = cubeColorMessage.Color.ToString();
break;
}
}
}
```

Switch-case is used here as a way to handle a message from `CubePresenter`.

### Send a Message
To send a _Message_ to some `Presenter` use `DispatchMessageTo(TMessage message)` method in `MessageDispatcher` -

`MessageDispatcher.DispatchMessageTo(new CubeColorData(color))`

In the example where `CubePresenter` class is -

```
public class CubePresenter : Presenter
{
// ...

private void OnModelColorChanged(Color color)
{
view.Color = color;
messageDispatcher.DispatchMessageTo(new CubeColorMessage { Color = color });
}
}
```

Clearing
---
To clear a `Presenter` (or some class) you can use built-in `IClearable` interface -

```
public interface IClearable
{
public void Clear();
}
```

Base `Presenter` class implements `IClearable` interface -

`Presenter : IPresenter, IClearable`

In the example, inside of `EntryPoint.OnDestroy()` method `Clear` is used to free up resources -

```
public class EntryPoint : MonoBehaviour
{
// ...

private CubePresenter cubePresenter;
private UIPresenter UIPresenter;

// ...

private void OnDestroy()
{
cubePresenter.Clear();
UIPresenter.Clear();
}
{
```