News for hackers. Stuff that matters.
Re:Stop writing good code; Start writing good software. by paganel on Saturday November 19, 2011 @04:52PM (#3256373)
> If I say "Factory" you're probably not thinking Chapter 3 of GOF

Honest question, what does this pattern accomplish? I'm asking this because I just saw a bunch of Factory-like classes in some PHP code I was trying to port to Python, and for the life of me I couldn't understand why the original programmer had made it so complicated and convoluted, when he could have done it all in 30 lines of code.

And a second question for whoever might have the free time to answer it: does anybody actively use inheritance (or, why not, multiple inheritance) on a daily basis and in the same time do they feel like it helps them? (as in: does the size of their code base gets significantly smaller? does the code fits better in one's head? things like that).

Re:Stop writing good code; Start writing good software. by icebraining on Saturday November 19, 2011 @06:06PM (#3256594)
Factory classes make less sense in a language with first class functions, but Factory functions are handy sometimes. As you can see in this example of a Factory function in Python from the standard library[1], it can be useful to return different subclasses depending on the arguments, but it doesn't have to be heavyweight like in Java.

[1]: http://docs.python.org/library/collections.html#collections....

Re:Stop writing good code; Start writing good software. by davesims on Saturday November 19, 2011 @05:42PM (#3256526)
A Factory is used to create an object of a specific type, when the calling code only has a reference to a supertype or prefereably an abstract type of that object. This reduces coupling and therefore side-effects in your code.

For instance:

  Car car = CarFactory.newCar(someLocalContext);
Might return a specific type of car for the given context, but the calling code's coupling is only to the supertype Car, and therefore can operate in the same way on any kind of Car.

Since php is dynamically typed there's not as much reason to use this pattern, although in certain cases it might be the right choice.

The Factories in question might actually be more like Builders -- classes used to hide complex construction processes.

I use inheritance all the time in Ruby and Java these days. With Ruby you get Modules and the Mixin approach, which allows for what is essentially multiple inheritance. I try to keep the level of inheritance close to 1 (I can't remember the last time I went past that) but yes, it's an essential tool in the toolkit.

I'm apparently one of the last coders on earth that still uses UML, but I find it helps me clarify architectures and visualize my code. OOP was meant to be visual in nature -- object relationships are, imho, best understood visually rather than through linear code. My suggestion would be to get used to UML or some hacked derivative thereof and express your dependencies visually, and you may find inheritance starts to make more sense.

Re:Stop writing good code; Start writing good software. by mattmanser on Sunday November 20, 2011 @09:08AM (#3258115)
Perhaps it's just a difference in styles, I avoid inheritance like the plague. To me something has to be really, really special to inherit off another class that I've written.

And even then I'll come back to it the next day and see if I can get rid of it or it really does still make sense.

Re:Stop writing good code; Start writing good software. by vegas on Sunday November 20, 2011 @01:42PM (#3258683)
The factory pattern exists to create jobs and products. Whether anyone wants or needs either is yet to be determined.