Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is functional, however, it might get cumbersome if more types are added.<br> According to my personal preferences for the simple factory the implementation would be:</p> <pre><code>enum BackgroundFactoryType { Special, Image, Normal, } static class BackgroundFactory{ static Dictionary&lt;BackgroundFactoryType, Func&lt;Cursor, ContentManager, AbstractBackground&gt;&gt; constructors; static BackgroundFactory() { //initialize the constructor funcs constructors = new Dictionary&lt;BackgroundFactoryType, Func&lt;Cursor, ContentManager, AbstractBackground&gt;&gt;(); constructors.Add(BackgroundFactoryType.Special, (cursor, content) =&gt; new SpecialBackground(cursor, content)); constructors.Add(BackgroundFactoryType.Image, (_, content) =&gt; new ImageBackground(content)); constructors.Add(BackgroundFactoryType.Normal, (_, __) =&gt; new NormalBackground()); } static public AbstractBackground Create(BackgroundFactoryType type, Cursor cursor, ContentManager content) { if (!constructors.ContainsKey(type)) throw new ArgumentException("the type is bogus"); return constructors[type](cursor, content); } } </code></pre> <p>or you could simply do:</p> <pre><code>static class BackgroundFactory{ static public AbstractBackground Create(BackgroundFactoryType type, Cursor cursor, ContentManager content) { switch (type) { case BackgroundFactoryType.Special: return new SpecialBackground(cursor, content); case BackgroundFactoryType.Image: return new ImageBackground(content); case BackgroundFactoryType.Normal: return new NormalBackground(); default: throw new ArgumentException("the type is bogus"); } } } </code></pre> <p>One nice side-effect of this approach, is that it takes just a little work to make this thing configuration driven instead of hard coded.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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