Note that there are some explanatory texts on larger screens.

plurals
  1. POdesign build or factory pattern
    text
    copied!<p>I understand both patterns (I think), but given my specific need I can't seem to either find a solution or find an example (all examples are really simple).</p> <p>My problem is that I want some kind of factory (or director/etc) that creates objects that may or may not have dependencies.</p> <p>Let's suppose I have this code: (I'm using C#)</p> <pre><code>interface MyObject{ public void load(); public void update(); public void draw(); } class ObjectA : MyObject{ public void load(){/*load*/} public void update(){/*update*/} public void draw(){/*draw*/} } class ObjectB : MyObject{ Texture texture; public ObjectB(Content content){ texture = content.load("texture"); } public void load(){/*load*/} public void update(){/*update*/} public void draw(){/*draw*/} } class ObjectC : MyObject{ Parent parent; public void setParent(Parent parent){ this.parent = parent; } public void load(){/*load*/} public void update(){ if( this.status == Status.Done ){ parent.remove(this); } } public void draw(){/*draw*/} } class Map : Parent{ MyObject myobj; public void load(MapInfo map){ //This is what I want to achieve myobj = MyObjectFactory.create(map.objectInfo); //This is my problem. I don't really know how to solve this. //I can't do this &gt; myobj.setParent(this); //error //Unless I create a setParent method in interface, I don't know how to achieve this. } public void remove(MyObject obj){/*remove*/} } </code></pre> <p><strong>I don't really know how to achieve this: <code>myobj.setParent(this);</code>. It cannot be in the constructor (like ObjectB), because Parent is not the same for every situation.</strong></p> <p>This is what I would have for my factory:</p> <pre><code>class MyObjectFactory{ Content content; public MyObjectFactory(Content content){ this.content = content; } public MyObject create(objectInfo){ //suppose objectInfo is xml //read xml Type type = objectInfo.type; //I'm totally fine with this. (Noob implementation) switch(type){ case Type.A: return new ObjectA(); break; case Type.B: //I'm also fine with this, unless there is a better way. return new ObjectB(this.content); break; case Type.C: return new ObjectC(); break; } } } </code></pre> <p>So far ObjectA and ObjectB are good. ObjectA is simple. ObjectB constructor gets set by Factory (Factory was previously given <code>content</code> variable).</p> <p>That leaves ObjectC, which needs Parent to remove itself (it may not be the best example. Trying to simulate different needs). <strong>How do I achieve this?</strong></p> <p>I only came up with one solution: Add a method to MyObject interface, ObjectA and ObjectB would have just a setParent method that does nothing.</p> <p>I hope this is not confusing.</p> <p><strong>Edit</strong> I forgot to add Parent in: <code>class Map : Parent</code></p>
 

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