Sunday, April 26, 2009

Creational Patterns

Hi everyone!, today I'm going to talk about creational patterns, again, these are only my ideas regarding to this topic. I'm basically going to talk about the most used creational patterns: Abstract Factory and Factory Method and I'm going to describe the most important differences.

Ok, let's start with Abstract Factory and Factory Method (I've already talked a bit on the previous post, so let's reuse some diagrams J .

Let's say we want to create a simple car object, but we have two kinds of Cars (Compact and Premium), if we want our code to be independent of the Car construction so we could implement a Factory Method. The main idea of the factory method is that we create an abstract interface and let it's implementations to create the specific object. Basically, Factory Method is to creating objects as Template Method is to implementing an algorithm. Let's see the car example:




So CompactCarFactory creates a CompactCar and PremiumCarFactory creates a Premium Car but for the Factory client doesn't know about which type of car is being created. In my opinion, this is the most important quality of the Factory Method, the independence of the car type in the client class gives us a great decoupling between objects which is something that developer should be considering constantly while coding. 

Let's go a bit deeper in implementation, other intent of the Factory Method is that we want to avoid the construction of the objects in other parts of the code, maybe because we consider the new operator harmful for our problem, in this case,  object must have a protected constructor and the factory classes should be in the same package of the object. This is quite good, I think it is OK, however, I do not always find the need of avoiding the object construction with the new operator in other part of the code. I consider that other programmers are grown up people and would use the factory instead of the new operator. So having the factory in a separate package don't smell so bad for me, (we'll probably need to create a car in a fitnesse for example where is no need to be so elegant and so the factory could be unnecessary).

On the other hand, the implementation of createCar() method should not have any logic inside. If you see that the method is growing you should consider a Builder pattern instead of a factory Method (I'm talking in the design phase). As we shouldn't have any logic Unit test should not be very difficult to do, right? (and probably unnecessary J).

One final comment about this pattern, a method inside your class that creates an object is NOT a factory method J, you might think it is silly, but I've found several peaces of code where a class method is documented as a "Factory Method" without being a Factory Method.

So, let's move on with Abstract Factory, let's suppose we have the following:




Is it a Factory Method or an Abstract Factory?, I've found this example in some books a Factory Method, however if we read the intents of the Abstract Factory method in the same books, we find something like "families of objects are involved", well, for me it is kind of tricky right?, I have a redefinition of a factory Method but in each overriding we are create different type of objects which extends of an Abstract Object. In my opinion, it is a Factory Method because we cannot create with the CarFactory multiple families of objects. Abstract Factories can be used as an alternative of facade to hide platform-specific classes, so an Abstract Factory might look like this:



Most of the things commented for the implementation of the factory Method can be applied to Abstract Factory, so I don't want to be redundant on this and point out some useful topics:

  • Abstract Factory is more general that a Factory method. Sometimes creational patterns compete with each other, however they sometimes complement each other. An Abstract Factory might store a set of factory methods or Prototypes, for example.
  • Generally, we often start with a Factory Method and then evolve toward Abstract Factory.
  • While a Builder is used to create complex objects, Abstract Factory emphasizes a family of product objects (either simple or complex).

One final comment, despite of the fact that Factory Methods and Abstract Factories are quite similar they intents are different, consider an Abstract Factory as class that makes us able to create families of product objects and a Factory Method as more specific object factory.



1 comment:

  1. One technical comment:

    The "Entradas antiguas" link at the blog default page shows a single post only, making it useless for its declared purpose... (Yes, I know that nearly everyone reads blogs these days with Google Reader, but some of us (dinosaurs?) still prefer the old fashioned way... :-)

    ReplyDelete