Why Design Patterns?
There are several reasons why is interesting to learn about Design patterns:
It gives us vocabulary that we can use to talk about design. For example: "I am thinking about using an strategy for this scenario".
It is important to understand when each pattern could be used.
Creational Patterns
Robert Martin talks in his book about how each application should have a part where the objects are created and link together, and then they are passed to the application which exercises them to fulfill the requirements through the object interfaces. In this way we could change the application behaviour by changing the object building process, and returning different object which respect the same interfaces.
So it is important to be able to identify where objects are created and how we can plugged in the new objects which will change the functionality.
Rather than spread the word "new" all around our code, we should be locating the object creation under a well set of defined classes which responsibility is building the net of objects that the application will use.
Abstract Factory
It make sense when we need to create a well defined number of objects, and all of them can be defined within an abstract interface..
Classic example is UI application, where a widget factory is used to create all the different types of widgets for an application. This allows to change the look of application by defining a new set of widgets and then creating them with a factory which will passed to the application.
Builder
Builder defines an algorithm for the process of building a final product, separating the algorithm from the how the product is created and represented. It allows to reuse the algorithm and to create different products by implementing the algorithm steps.
One the main advantages is that allows to hide the internal representation of the product being build.
RTF converter
The builder acts as listener of the director, who carries an operation and it calls method on the builder, where the implementation varies. Builder objects is called multiple times and we can finally call a method on it to retrieve the wished information. The director is the part that can be reused.
To enhance this view of the pattern we could rename the methods "convertChar", "convertFont", "convertParragraph" to "onCharRead", "onFontConvert", "onParragraphConvert". Those method would be implemented by the builder.
Maze Builder
The maze builder encapsulates how mazes are created, without exposing the internal structure the maze. We could subclass the Maze builder to create different type of Mazes.
Director
Create an additional driver list
Based on [GOF], there isn't much benefit in wrapping the products under a common abstract concept. The builder client configures the director with a builder, therefore he should expect the relative product. This is not the scope of this pattern. It is more focus in capturing the knowledge on which operations need to be implemented for creating a certain type of object.
In the example of the RTF document reader, a builder can be passed in, so everything about the reader can be reused.
Builder is related to template method in that the template method defines the skeleton for the algorithm, which is what the builder does. So we could say the builder does.
The main difference between a builder and an abstract factory is that the builder define steps on the building process while the abstract factory defines an interface to create a family of products, and all the products can be wrapped under an abstract interface.
In DDD mentions that factories, which are used by erepositories to make agreggates, used frequently builder objects to build aggregates which are then passed to the repositories to retrieve.
Builder is a more finer grain version than factory Method. Factory method return the product directly while in the builder identifies a common pattern (template method) on the creation of the product.
There are several reasons why is interesting to learn about Design patterns:
It gives us vocabulary that we can use to talk about design. For example: "I am thinking about using an strategy for this scenario".
It is important to understand when each pattern could be used.
Creational Patterns
Robert Martin talks in his book about how each application should have a part where the objects are created and link together, and then they are passed to the application which exercises them to fulfill the requirements through the object interfaces. In this way we could change the application behaviour by changing the object building process, and returning different object which respect the same interfaces.
So it is important to be able to identify where objects are created and how we can plugged in the new objects which will change the functionality.
Rather than spread the word "new" all around our code, we should be locating the object creation under a well set of defined classes which responsibility is building the net of objects that the application will use.
Abstract Factory
It make sense when we need to create a well defined number of objects, and all of them can be defined within an abstract interface..
Classic example is UI application, where a widget factory is used to create all the different types of widgets for an application. This allows to change the look of application by defining a new set of widgets and then creating them with a factory which will passed to the application.
Builder
Builder defines an algorithm for the process of building a final product, separating the algorithm from the how the product is created and represented. It allows to reuse the algorithm and to create different products by implementing the algorithm steps.
One the main advantages is that allows to hide the internal representation of the product being build.
RTF converter
The builder acts as listener of the director, who carries an operation and it calls method on the builder, where the implementation varies. Builder objects is called multiple times and we can finally call a method on it to retrieve the wished information. The director is the part that can be reused.
To enhance this view of the pattern we could rename the methods "convertChar", "convertFont", "convertParragraph" to "onCharRead", "onFontConvert", "onParragraphConvert". Those method would be implemented by the builder.
Maze Builder
The maze builder encapsulates how mazes are created, without exposing the internal structure the maze. We could subclass the Maze builder to create different type of Mazes.
Director
Create an additional driver list
Based on [GOF], there isn't much benefit in wrapping the products under a common abstract concept. The builder client configures the director with a builder, therefore he should expect the relative product. This is not the scope of this pattern. It is more focus in capturing the knowledge on which operations need to be implemented for creating a certain type of object.
In the example of the RTF document reader, a builder can be passed in, so everything about the reader can be reused.
Builder is related to template method in that the template method defines the skeleton for the algorithm, which is what the builder does. So we could say the builder does.
The main difference between a builder and an abstract factory is that the builder define steps on the building process while the abstract factory defines an interface to create a family of products, and all the products can be wrapped under an abstract interface.
In DDD mentions that factories, which are used by erepositories to make agreggates, used frequently builder objects to build aggregates which are then passed to the repositories to retrieve.
Builder is a more finer grain version than factory Method. Factory method return the product directly while in the builder identifies a common pattern (template method) on the creation of the product.
No hay comentarios:
Publicar un comentario