Last active
June 11, 2022 04:44
-
-
Save bestknighter/8e70499ea4a3194dcd3a3d66b80e1f3a to your computer and use it in GitHub Desktop.
Structure (with example) for a Model <-> Viewer inspired code architecture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public class Program | |
{ | |
public static void Main() | |
{ | |
var fc = new MyIntViewer_FancyConsole(); | |
var sc = new MyIntViewer_SimpleConsole(); | |
Console.WriteLine("Hello World"); | |
var x = new MyInt(); | |
Console.WriteLine("Initial value: "+x.Value); | |
x.Value = 2; | |
x.Bind( new[]{fc} ); | |
x.Value = 10; | |
x.Bind( new[]{sc} ); | |
x.Value = -26; | |
x.Value = int.MaxValue; | |
x.Unbind( new[]{fc} ); | |
x.Value = int.MinValue; | |
x.Value = 0; | |
x.Value = 2022; | |
x.Unbind( new[]{sc} ); | |
x.Value += 100; | |
x.Value -= 2022; | |
x.Bind( new MyIntViewer[]{fc, sc} ); | |
x.Value += 100; | |
x.Value = 42; | |
x.UnbindAll(); | |
x.Value = 1; | |
x.Value = 0; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Model example | |
* Here I created a simple one for demonstration purposes. | |
* But this can get as complex as you desire. For example, | |
* I used this structure for making different inventory | |
* viewers for a single inventory in a Unity game I worked on. | |
* Items had all sorts of information that most viewers didn't | |
* care about. So I simply updated them when needed and passing | |
* only what was truly needed. | |
*/ | |
using System.Collections.Generic; | |
public class MyInt : Viewable<MyInt, MyIntViewer> | |
{ | |
private int _value = 0; | |
public int Value | |
{ | |
get { return _value; } | |
set | |
{ | |
foreach( var v in m_viewers ) v.ValueWillChange(value); | |
_value = value; | |
} | |
} | |
readonly private List<MyIntViewer> m_viewers = new List<MyIntViewer>(); | |
// If it makes no sense for a model to have more than one viewer, it can be handled in the function. | |
// You can ignore or throw or do whatever you deem fit. | |
public void Bind(MyIntViewer[] viewers) | |
{ | |
foreach(var v in viewers) | |
{ | |
if( !m_viewers.Contains(v) ) | |
{ | |
v.Bind(this); | |
m_viewers.Add(v); | |
} | |
} | |
} | |
public void Unbind(MyIntViewer[] viewers) | |
{ | |
foreach(var v in viewers) | |
{ | |
var idx = m_viewers.FindIndex(mv => mv == v); | |
if( idx != -1 ) | |
{ | |
m_viewers[idx].Unbind(); | |
m_viewers.RemoveAt(idx); | |
} | |
} | |
} | |
public void UnbindAll() | |
{ | |
foreach(var v in m_viewers) v.Unbind(); | |
m_viewers.Clear(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Viewable<TViewable, TViewer> | |
where TViewable: class, Viewable<TViewable, TViewer> | |
where TViewer:Viewer<TViewer, TViewable> | |
{ | |
void Bind(TViewer[] viewers); | |
void Unbind(TViewer[] viewers); | |
void UnbindAll(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Viewer<TViewer, TViewable> | |
where TViewable: class, Viewable<TViewable, TViewer> | |
where TViewer: Viewer<TViewer, TViewable> | |
{ | |
protected TViewable model = null; | |
public bool Bind(TViewable model) | |
{ | |
if( this.model != null ) Unbind(); | |
this.model = model; | |
return OnBind(); | |
} | |
public void Unbind() | |
{ | |
OnUnbind(); | |
model = null; | |
} | |
protected abstract bool OnBind(); | |
protected abstract void OnUnbind(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** You can declare a concrete class if there's no need for different viewers. | |
* Or you can declare an abstract class like below if you want different viewers. | |
* You can have several types of Viewers this way. Console, file logger, GUI, remote computer, etc. | |
*/ | |
using System; | |
public abstract class MyIntViewer : Viewer<MyIntViewer, MyInt> | |
{ | |
protected override bool OnBind() | |
{ | |
Console.WriteLine("I just got binded! :D"); | |
return true; | |
} | |
protected override void OnUnbind() | |
{ | |
Console.WriteLine("I was unbinded... :("); | |
} | |
abstract public void ValueWillChange(int newValue); | |
} | |
public class MyIntViewer_SimpleConsole : MyIntViewer | |
{ | |
override public void ValueWillChange(int newValue) | |
{ | |
Console.WriteLine(DateTime.Now.ToUniversalTime().ToString()+"\t-\tOld: "+model?.Value+"\tNew: "+newValue); | |
} | |
} | |
public class MyIntViewer_FancyConsole : MyIntViewer | |
{ | |
override public void ValueWillChange(int newValue) | |
{ | |
Console.WriteLine("Sir, I detected a change on the value I was watching. It used to be "+model.Value | |
+" and now its new value is "+newValue); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: