Introduction to: Patterns of Design (II)

In this next chapter another type of pattern of design will be introduced, the creational one. There are some differences between this kind of patterns and the behavioral types (a type of last one was described in part one), the most important thing is, while behavioral centers itself on the algorithms and how the object works, the creational pays attention in how the object is created.

We will decide to apply this kind of patterns when we need to focus in the creation of the object, and this creation is a highly complicated process. The concrete pattern shown in this introduction is the Builder Pattern.

The Builder Pattern is recommended when the algorithm for the creation of the object it’s very clear and it composed by very well-defined steps. The complexity of the creation lies on the different algorithms that might be used in the steps. Maybe a little class diagram should help:

Builder Pattern Class DiagramAs we can see in the class diagram, the Director is the only class that knows about the whole creation process. The Director it’s only coupled with the abstract Builder, so it has no dependency on the concrete classes. A concrete Builder class instance must be passed to the Director.

The Director knows which methods of the Builder class must be called, and in which order, what doesn’t know it’s what each method does, of course. Each concrete Builder class model a specific algorithm for the Product creation. In order to ensure that, the concrete classes must override the methods defined in the abstract class, except those ones that are generic enough.

Let’s apply this pattern in a real example, well maybe it isn’t very real, but it will be useful. Imagine that we need to model the Automated Letter Maker. Given a set of concepts (sender, subject, name…) the application must create different kinds of letters (friendly, formal, business letter…). All the letters has the same parts: header, greeting, body, complimentary clause, signature and postscript. Lets put it all together in another class diagram.

Class Diagram for Automatized Letter MakerThe Writer class knows the algorithm for writing a letter, first is the header, then de greeting… and ends with a possible postscript. But this class doesn’t know how to write it in the proper style (formal, business…). Each concrete builder knows how to write each part in the appropriate type. Finally the Letter is the output of any kind of builder.

Why this pattern is suitable for this example?

  • The output it’s always the same: a Letter.
  • The steps of the creation of a Letter are very clear.
  • There are different ways to implement each step.
  • The addition of new types of builders has the less possible impact in the model.

*All diagrams are made with StarUML.

6 Responses

  1. Nen, molt ben explicat i molt ben parit. Tens pensat algun article sobre Hibernate o Spring ?

    • És possible que intenti fer alguna sèrie, del tipus apunts del curs d’Hibernate i Spring.

      • Ja està pujat al dzone! Per cert, fixa´t en la descripció que he posat.

        Només un petit comentari sobre el blog: hauries de buscar un nom propi i després la descripció . Per exemple, Maldita Sea! : a blog about j2ee development.

  2. A veure si tornes al atac, perque això s’enfonsa
    deseguida. Pensa que ser de la èlite no és gratuït, t’ho has de currar molt!! ;) Vinga noi, ànims!!

    • Jo intentant escriure amb anglés i ell en català jeje. Estic amb el David necessites un nom i Spring i Hibernate seria molt interessant. d’acord que estàs començant de 0 però a poc a poc.

      Per cert, Ja sé que sembla molt cutre però estaria bé que adjuntis el codi font així la penya s’ho podria baixar, evidentment dels patrons de disseny no però si tens algo fet mai se sap.

      • Gràcies per intentar-ho Joan Martí!
        He de pensar un nom, si tens alguna idea :)

        Vull escriure sobre Spring i Hibernate també, tingues paciència.

        Sobre pujar codi, he de buscar alguna manera de fer-ho, al tenir l’hospedatge gratuit de wordpress no puc pujar-ho al blog. Buscaré un lloc on publicar-lo.


        Thanks for trying to write in English!
        I need a name for the blog, If you want to propose one…

        I want to write about Spring and Hibernate too, just be patient!

        About attaching the sample code, I have to think about how I could do it, because free hosting of wordpress doesn’t allow me to attach source files. I will search a web that let’s me share pieces of source code.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: