IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Advice on factory methods
I've got a base class, and two child classes that extended it. When I wrote them, I would always know what type of child class I'd want. Now I've got a case where it depends on a lookup. I could do a switch/case where I include the appropriate child class and instantiate it, but I'm thinking a factory should hold that logic.

The question is, should I go back to the places where I've already instantiated the child classes and switch them to use the factory? They'll keep working as they are, and will save an extra db lookup that otherwise the factory will have to do.

Are there any good reasons not to instantiate child classes directly if they're also available via a factory?
--

Drew
New There are benefits to both.
In some languages, you have to jump through too many hoops to make a constructor do things you want to do that a factory can do much easily. For example, I wrote an ORM library in PHP that uses a factory to create objects. I did it this way so that if you request one by an id that already exists, it can hand the same one back to you. This was very difficult to do with a constructor in PHP v4.

Wade.

Q:Is it proper to eat cheeseburgers with your fingers?
A:No, the fingers should be eaten separately.
New Design elegance and maintainability
A design purist would argue you should use the factory so there is only one place that instantiates the objects. If this is part of a big complex system that will be maintained by a team of programmers, I would tend to agree. At that point design elegance and maintainability are more important then the speed penalty of one DB call. On a smaller scale project, I wouldn't bother.

Jay
New Sounds right
Every exception is something else I have to remember if I ever need to change it. Better to have one interface than three.
--

Drew
     Advice on factory methods - (drook) - (3)
         There are benefits to both. - (static)
         Design elegance and maintainability - (jay) - (1)
             Sounds right - (drook)

When's the last time you heard China?
53 ms