1

A factory class isn't the end product I am looking for but it is basically my problem boiled down. I am looking for a class we'll call foo that can be passed in another class bar as a parameter to its constructor such that when later calling fooInstance.create() it returns a new instance of bar. I can easily figure out how to make a fooInstance.create(bar) create an instance of bar. But that isn't what I need. I need each instance of the factory class to create a specific type of object. so:

    fooCar = new foo(Car);
    fooTruck = new foo(Truck);
    myCar = fooCar.create();
    myTruck = fooTruck.create();

This would be something easily handled by Generics in c#. I either get errors or I end up screwing with the prototype of foo which then changes the type created by all instances of foo.

2
  • What extra things is your foo class supposed to do. Javascript won't give you the static type safety you are looking for so perhaps you could instead opt for something simpler, such as assigning fooCar = Car and ignoring the create method completely: myCar = new fooCar. Commented Jan 18, 2013 at 4:28
  • Check this out addyosmani.com/resources/essentialjsdesignpatterns/book/… Commented Jan 18, 2013 at 4:29

2 Answers 2

1

Just return a regular object from your factory, with just one create method:

function foo (Constructor) {
    return {
        create: function () {
            return new Constructor();
        }
    };
}

Check out the fiddle: http://jsfiddle.net/fR5Gz/1/

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks!! That was so deceptively short and simple that I thought it was a plain factory at first.
@user1989150 - It is a plain factory. Isn't that what you wanted?
0

Thanks to everyone for your help. @missingno I thought the details of what I was trying to do would cloud the issue but now I think it was the other way around. I'll know better for next time. I wasn't looking for type safety but without it maybe there is no reason to do it this way. Maybe I'm stuck in classical thinking.

I actually figured it out myself this morning because I didn't get any email alerts on this thread like I was supposed to so I thought there weren't any replies yet. This is my solution (relevant code only):

    ORM.Repository = (function (){
    function Repository(ob){
        this.construct = ob;
    };
    //bunch of other generic repository stuff deleted
    Repository.prototype.make = function () { return new this.construct();}
    return Repository;
})();

Usage:

    var carRepository = new ORM.Repository(car);
    var personRepository = new ORM.Repository(person);
    var myCar = carRepository.make();
    var user = personRepository.make();

1 Comment

Have you looked at Resourceful? It's part of the Flatiron project and provides a mechanism for generating ORM models. github.com/flatiron/resourceful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.