Note that there are some explanatory texts on larger screens.

plurals
  1. POSolving a library design - centralised configuration reference
    primarykey
    data
    text
    <p>Consider the sample code below consisting of a Class Library <em>design</em> and an executable Program using the library.</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;(); } /// &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; /// &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) { m_StageApp = 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; // Question: How to add actor to stage? foreach (var actor in app.Actors) Console.WriteLine(actor.Execute()); } } } </code></pre> <p><strong>Question</strong></p> <p><code>Stage</code> and <code>Actor</code> have circular dependency and seems bad to me. For example, how should we add actors to stage?</p> <p>If I let user to create new <code>Actor()</code> themselves, then they must keep on supplying the <code>Stage</code>.</p> <p>If I give <code>Actor()</code> an internal constructor and make <code>Stage</code> a factory, then I lose some of the flexibility for users to do making inherited <code>Actor</code>s.</p> <p>If I make <code>Stage</code> a singleton, then I can only have one set of <code>SharedSetting</code>. In case the user wants more than one <code>Stage</code> in his <code>AppExe</code>, then it cannot be done.</p> <p>Is there anyway to redesign the architecture so as to avoid the problems above?</p>
    singulars
    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. This table or related slice is empty.
    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