1

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).

My problem is that I want some kind of factory (or director/etc) that creates objects that may or may not have dependencies.

Let's suppose I have this code: (I'm using C#)

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 >
        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*/}
}

I don't really know how to achieve this: myobj.setParent(this);. It cannot be in the constructor (like ObjectB), because Parent is not the same for every situation.

This is what I would have for my factory:

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;
        }
    }
}

So far ObjectA and ObjectB are good. ObjectA is simple. ObjectB constructor gets set by Factory (Factory was previously given content variable).

That leaves ObjectC, which needs Parent to remove itself (it may not be the best example. Trying to simulate different needs). How do I achieve this?

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.

I hope this is not confusing.

Edit I forgot to add Parent in: class Map : Parent

Jonwd
  • 635
  • 10
  • 24

2 Answers2

3

I think you mixing behavior with the Object creation. Factory Method and Builder are both creational design patterns. So you can use either to create objects. Just keep in mind that these patterns will only solve your object creation problem.

Other behavioral aspect( like as you have mentioned that you want to achieve a particular behavior) is not part of either of these design patterns. So you have flexibility to do in your way but just keep in mind these design principles like OCP, SRP, DIP etc.

this SO problem talks about these patterns : factory method (1) vs factory(2) vs Builder (3) pattern

Community
  • 1
  • 1
rai.skumar
  • 10,309
  • 6
  • 39
  • 55
1

A simple, if not elegant, fix would be to pass 'this' to your factory, and have the factory call setParent() only when needed, since the factory is aware of the implementation details.

   myobj = MyObjectFactory.create(map.objectInfo, this);

   case Type.C:  
     var inst = new ObjectC();  
     inst.setParent(parent);
JakeSays
  • 329
  • 1
  • 6