Long time no see, OK OK I admit I have been quite busy from quite some weeks. Well I should not say busy I was kind of sloppy, as charmed by the usual onsite game of companies and having a little time enjoying beautiful Denmark. But now I am back on track and now I will continue this series. So far we have looked into all the creational patterns.
• Singleton
• Factory
• Abstract factory
• Builder
And one structural pattern
• Bridge
But before moving on to any other pattern I just wanted to think a little bit about creational patterns. Hmmmm, Well creational patterns means we want to create the objects. What all possible scenarios can be there when we want to create any object, let us try to find out
1. The client knows exactly what kind of object it needs.
2. The client knows the category of objects it wants and it want to create any object of that category
3. The client knows a set of categories from which it can select a category and then within that category it can create an object.
4. The client want to get a customized object so it need control on object creation/assembly process.
5. The client want to access an object that deals with a critical resource.
Fair enough, so far I could think of only these possibilities. And if you try to look carefully u will see that every scenario maps up to a specific pattern we have discussed. Try to figure out which one for which scenario. And till then I will try to jot down some points that should be kept in mind when we want to use the creational patterns.
Abstract factory:
Indicators in analysis: Different cases exist that require different implementations of common rules.
Indicators in design: Many polymorphic structures exist that are used in pre-defined combinations. These combinations are defined by there being particular cases to implement or different needs of client objects.
Indication pattern is not being used when it should be: A variable is used in several places to determine which object to instantiate.
Relationships involved: The Abstract Factory object is responsible for coordinating the family of objects that the client object needs. The client object has the responsibility for using the objects.
Builder:Indicators in analysis: Several different kinds of complex objects can be built with the same overall build process, but where there is variation in the individual construction steps.
Indicators in design: You want to hide the implementation of instantiating complex object, or you want to bring together all of the rules for instantiating complex objects.
Factory:
Indicators in analysis: There are different commonalities whose implementations are coordinated with each other.
Indicators in design: A class needs to instantiate a derivation of another class, but doesn’t know which one. Factory method allows a derived class to make this decision.
Field notes: The Factory method is often used with frameworks. It is also used when the different implementations of one class hierarchy requires a specific implementation of another class hierarchy.
Singleton:
Indicators in analysis: There exists only one entity ofsomething in the problem domain that is used by several different things.
Indicators in design: Several different client objects need to refer to the same thing and we want to make sure we don’t have more than one ofthem. You only want to have one of an object but there is no higher object controlling the instantiation of the object in questions.
Field notes: You can get much the same function as Singletons with static methods. Therefore, the Singleton should be used only when statics don’t work at all. This occurs hen you need to control hen the class is instantiated (that is, static members are allocated). Another case is if you want to use polymorphism on the Singleton.
So now after reading these notes we can easily figure out which creational pattern to use in which scenario. And as far as our little analysis on object creation scenario goes let us look at it again with patterns they map to
1. The client knows exactly what kind of object it needs. – Create an object directly.
2. The client knows the category of objects it wants and it want to create any object of that category – Factory pattern.
3. The client knows a set of categories from which it can select a category and then within that category it can create an object. – Abstract factory
4. The client want to get a customized object so it need control on object creation/assembly process. - Builder
5. The client want to access an object that deals with a critical resource. – Singleton.
So I guess now we have mastered the creational patterns, if not, we at-least have an idea about these patterns and maybe next time when we sit and try to think of organizing our code some of these idea will be there in the back of our mind and will effect the way we write our code. Hopefully in a good way
• Singleton
• Factory
• Abstract factory
• Builder
And one structural pattern
• Bridge
But before moving on to any other pattern I just wanted to think a little bit about creational patterns. Hmmmm, Well creational patterns means we want to create the objects. What all possible scenarios can be there when we want to create any object, let us try to find out
1. The client knows exactly what kind of object it needs.
2. The client knows the category of objects it wants and it want to create any object of that category
3. The client knows a set of categories from which it can select a category and then within that category it can create an object.
4. The client want to get a customized object so it need control on object creation/assembly process.
5. The client want to access an object that deals with a critical resource.
Fair enough, so far I could think of only these possibilities. And if you try to look carefully u will see that every scenario maps up to a specific pattern we have discussed. Try to figure out which one for which scenario. And till then I will try to jot down some points that should be kept in mind when we want to use the creational patterns.
Abstract factory:
Indicators in analysis: Different cases exist that require different implementations of common rules.
Indicators in design: Many polymorphic structures exist that are used in pre-defined combinations. These combinations are defined by there being particular cases to implement or different needs of client objects.
Indication pattern is not being used when it should be: A variable is used in several places to determine which object to instantiate.
Relationships involved: The Abstract Factory object is responsible for coordinating the family of objects that the client object needs. The client object has the responsibility for using the objects.
Builder:Indicators in analysis: Several different kinds of complex objects can be built with the same overall build process, but where there is variation in the individual construction steps.
Indicators in design: You want to hide the implementation of instantiating complex object, or you want to bring together all of the rules for instantiating complex objects.
Factory:
Indicators in analysis: There are different commonalities whose implementations are coordinated with each other.
Indicators in design: A class needs to instantiate a derivation of another class, but doesn’t know which one. Factory method allows a derived class to make this decision.
Field notes: The Factory method is often used with frameworks. It is also used when the different implementations of one class hierarchy requires a specific implementation of another class hierarchy.
Singleton:
Indicators in analysis: There exists only one entity ofsomething in the problem domain that is used by several different things.
Indicators in design: Several different client objects need to refer to the same thing and we want to make sure we don’t have more than one ofthem. You only want to have one of an object but there is no higher object controlling the instantiation of the object in questions.
Field notes: You can get much the same function as Singletons with static methods. Therefore, the Singleton should be used only when statics don’t work at all. This occurs hen you need to control hen the class is instantiated (that is, static members are allocated). Another case is if you want to use polymorphism on the Singleton.
So now after reading these notes we can easily figure out which creational pattern to use in which scenario. And as far as our little analysis on object creation scenario goes let us look at it again with patterns they map to
1. The client knows exactly what kind of object it needs. – Create an object directly.
2. The client knows the category of objects it wants and it want to create any object of that category – Factory pattern.
3. The client knows a set of categories from which it can select a category and then within that category it can create an object. – Abstract factory
4. The client want to get a customized object so it need control on object creation/assembly process. - Builder
5. The client want to access an object that deals with a critical resource. – Singleton.
So I guess now we have mastered the creational patterns, if not, we at-least have an idea about these patterns and maybe next time when we sit and try to think of organizing our code some of these idea will be there in the back of our mind and will effect the way we write our code. Hopefully in a good way
No comments:
Post a Comment