Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If your functionality is not limited by sharing the StageApp settings between actors, but also will be some other logic. For example when you need to know parent StageApp from Actor and vice versa. I preffer to implement it in this way:</p> <pre><code>namespace AppLib { /// &lt;summary&gt; /// Entry point for library. Stage manages all the actors in the logic. /// &lt;/summary&gt; class StageApp { /// &lt;summary&gt; /// Setting that is looked up by different actors /// &lt;/summary&gt; public int SharedSetting { get; set; } /// &lt;summary&gt; /// Stage managing actors with app logic /// &lt;/summary&gt; public IEnumerable&lt;Actor&gt; Actors { get { return m_actors.Where(x =&gt; x.Execute() &gt; 40).ToArray(); } } private List&lt;Actor&gt; m_actors = new List&lt;Actor&gt;(); public int TotalActorsCount { get { return m_actors.Count; } } public void AddActor(Actor actor) { if (actor == null) throw new ArgumentNullException("actor"); if (m_actors.Contains(actor)) return; // or throw an exception m_actors.Add(actor); if (actor.Stage != this) { actor.Stage = this; } } // we are hiding this method, to avoid because we can change Stage only to another non null value // so calling this method directly is not allowed internal void RemoveActor(Actor actor) { if (actor == null) throw new ArgumentNullException("actor"); if (!m_actors.Contains(actor)) return; // or throuw exception m_actors.Remove(actor); } } /// &lt;summary&gt; /// An object on the stage. Refers to stage (shared)settings and execute depending on the settings. /// Hence actor should have reference to stage /// &lt;/summary&gt; class Actor { private StageApp m_StageApp; private int m_Property; public StageApp Stage { get { return m_StageApp; } set { if (value == null) { throw new ArgumentNullException("value"); } if (m_StageApp != value) { if (m_StageApp != null) // not a call from ctor { m_StageApp.RemoveActor(this); } m_StageApp = value; m_StageApp.AddActor(this); } } } /// &lt;summary&gt; /// An actor that needs to refer to stage to know what behavior to execute /// &lt;/summary&gt; /// &lt;param name="stage"&gt;&lt;/param&gt; public Actor(StageApp stage) { Stage = stage; m_Property = new Random().Next(); } /// &lt;summary&gt; /// Execute according to stage settings /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; public int Execute() { return m_StageApp.SharedSetting * m_Property; } } } namespace AppExe { using AppLib; class Program { static void Main(string[] args) { StageApp app = new StageApp(); app.SharedSetting = 5; StageApp anotherApp = new StageApp(); anotherApp.SharedSetting = 6; // actor is added to the stage automatically after instantiation Actor a1 = new Actor(app); Actor a2 = new Actor(app); Actor a3 = new Actor(anotherApp); Console.WriteLine("Actors in anotherApp before moving actor:"); Console.WriteLine(anotherApp.TotalActorsCount); // or by calling method from StageApp class anotherApp.AddActor(a1); Console.WriteLine("Actors in anotherApp after calling method (should be 2):"); Console.WriteLine(anotherApp.TotalActorsCount); // or by setting Stage through property a2.Stage = anotherApp; Console.WriteLine("Actors in anotherApp after setting property of Actor instance (should be 3):"); Console.WriteLine(anotherApp.TotalActorsCount); Console.WriteLine("Actors count in app (should be empty):"); Console.WriteLine(app.TotalActorsCount); } } } </code></pre> <p>It allows to you to manipulate with object relationships transparently, but requires a little bit mor code to implement.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload