CSC/ECE 517 Summer 2008/wiki3 5 rp: Difference between revisions
No edit summary |
No edit summary |
||
(13 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=Pure Fabrication= | |||
Sometimes it is a good idea to introduce into a system an object that has no counterpart in the real world. This is called the pure fabrication pattern. Find examples of pure fabrication, and weave them into a narrative that can teach programmers when it is helpful and when it is not helpful to fabricate objects. | Sometimes it is a good idea to introduce into a system an object that has no counterpart in the real world. This is called the pure fabrication pattern. Find examples of pure fabrication, and weave them into a narrative that can teach programmers when it is helpful and when it is not helpful to fabricate objects. | ||
= | =Background= | ||
All design patterns, regardless of their specific purpose, are born from the same basic principles. These principles include [http://en.wikipedia.org/wiki/Cohesion_(computer_science) high cohesion], [http://en.wikipedia.org/wiki/Coupling_(computer_science) low coupling], and other features which result in flexible, maintainable, extensible code. Most often it is possible to separate the objects of an OO design into real world abstractions, or objects which reflect well known design patterns, such as a factory or an observer. Sometimes, however, the need for high cohesion, low coupling, and separation of dependence exists within software when there is no real world object representation or familiar design pattern to provide a solution. This is when pure fabrication is needed. Simply put, pure fabrication means creating a class object completely unrelated to the problem domain specifically for the purpose of achieving some degree of high cohesion, low coupling, potential for code re-use and high maintainability. | |||
Pure Fabrication is often attributed to Craig Larman as part of his nine [http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design) GRASP] design patterns. Each of these patterns is similar to all other design patterns in that they strive to achieve code refactoring, decoupling, and reusability. Arguably, they are more generic than the [http://en.wikipedia.org/wiki/Design_Patterns GoF] patterns, and pure fabrication is perhaps the most generic of them. In fact, pure fabrication is so generic that often existing well-known design patterns can be considered instances of Pure Fabrication. To put it another way, pure fabrication means to create a new design pattern. | |||
=Examples of Pure Fabrication= | |||
As mentioned above, several well known design patterns can be considered instances of Pure Fabrication. For example, Strategy and Command began as Pure Fabrications, but are common enough that a more specific name for each has been adopted. In a problem domain sense, they do not abstract a real world object. Each simply exists to promote decoupling between other objects in a system. | |||
This following example of Pure Fabrication is given in Larman's [http://www.amazon.com/Applying-UML-Patterns-Introduction-Object-Oriented/dp/0130925691/ref=sr_11_1?ie=UTF8&qid=1216994566&sr=11-1 book], Applying UML and Design Patterns - An Introduction to Object-Oriented Analysis and Design and Iterative Development. Consider a class Sale, which represented a purchase or transaction record. The system using the class requires storing the records in a relational database. The functionality required to do this could exist in the Sale class, however, this would mean Sale class would need to know specifics about the interface into the database. It would need to directly call database functions, which is not in the interest of the Sale class (this violates high cohesion). Also, if the system were to change its persistent storage mechanism in the future, the Sale class would have to be modified to adapt to the new storage interface (this violates low coupling). | |||
The solution is to create a new class whose sole responsibility is to save Sale records. This class could be called PersistantStorageBroker, and Sale would interact with it via a single save() method. The save() method is generic enough that it does not introduce unnecessary knowledge into the Sale class, and high cohesion is maintained. The PersistantStorageBroker class would know the details of the relational database interface, and so if future modifications were made to adapt to a new persistant storage mechanism, the Sale class could still use the save() method. In this, low coupling is achieved. The only class which would require modifications then is PersistantStorageBroker, but that is OK because that is the intent.<br> | |||
[[Image:Wiki3A.jpg]]<br> | |||
The above picture shows how the Sale class without the PersistantStorageBroker class would adapt to a new database type. It would need an Adapter class to bridge the two interfaces. | |||
[[Image:Wiki3B.jpg]]<br> | |||
The above picture shows how the PersistantStorageBroker class would facilitate adapting to a new database type.<br> | |||
It is easy to see that this Pure Fabrication is close to other patterns. It is almost an [http://en.wikipedia.org/wiki/Adapter_pattern Adapter] pattern, except an Adapter typically connects two existing interfaces. In both cases, a design pattern is used to accommodate change in the database interface. It is easy to recognize the first example as using the Adapter pattern: a new class is created to accept the old interface and connect it to the new interface. The other example is similar, but with some key differences. First, the Sale class does not directly interface with any database type, old or new. It only interfaces with the PersistantStorageBroker class. Second, the PersistantStorageBroker class exists from the very beginning; it is not added later when a new interface is required. In this, it anticipates change, unlike the Adaptor which comes about reactively. So, the PersistantStorageBroker class is not quite an Adapter, it is a Pure Fabrication. | |||
=The Case Against Pure Fabrication= | |||
It is important not to overuse Pure Fabrication, or other design patterns for that matter, if it results in many small classes with too much interaction between them. Too much of this can actually cause unexpected coupling problems despite the intent to decouple objects. Further, this can increase the yo-yo effect (both vertical and horizontal) while reading source code. If code is spread among too many classes it can affect maintainability in a different, yet equally as negative way as tightly coupled code. | |||
Similarly, sometimes it is OK to write code that has some degree of coupling, as there is always a trade off. In some cases, code does not necessarily need to be "pluggable" or compatible with all possible future uses and modifications of the code. It is important to realize this is a judgement call, and the design of the code and it's intent must be carefully analyzed. Code that is known ahead of time to have potential reuse obviously would benefit from using the priciples of Pure Fabrication and other design patterns, code such as framework code. However, if concessions are made in the design "just in case" future modification is needed, it may be good to re-evaluate the separation of responsibilities. Remember, re-factoring code in the future for maintenance reasons is not always bad, if it can be foreseen that the modifications are trivial compared to the cost of creating and implementing a very flexible, adaptable design. | |||
=External Links= | =External Links= | ||
[http://davidhayden.com/blog/dave/archive/2005/09/18/2476.aspx Pure Fabrication GRASP Pattern]<br> | |||
[http://codebetter.com/blogs/david.hayden/archive/2006/08/26/Over_2D00_Architecting-Via-Pure-Fabrication-and-Indirection-to-Reduce-Coupling.aspx Over-Architecting with Pure Fabrication]<br> | |||
[http://www.google.com/url?sa=t&ct=res&cd=4&url=http%3A%2F%2Falcor.concordia.ca%2F~smw%2Fcomp354%2FL22web-2x2.pdf&ei=SdSJSLv8NYWshgTq6u1T&usg=AFQjCNFvKxZzrAps2Z0rGnTGdCrpvI3D6w&sig2=NdOWfIQrIPIIipsaaTZDFg Pure Fabrication Notes]<br> | |||
[http://www.google.com/url?sa=t&ct=res&cd=9&url=http%3A%2F%2Fhebb.cis.uoguelph.ca%2F~dastacey%2FCIS3200%2FLectures%2FPDF%2FDesignPatterns3.pdf&ei=SdSJSLv8NYWshgTq6u1T&usg=AFQjCNEjBkMRZ_NhQpLp4zWjM9mlrBBhKw&sig2=S8iBydS82trn51IxSMbhbQ More GRASP Patterns]<br> | |||
[[CSC/ECE 517 Summer 2008/wiki3 Assignment|Back to the assignment page]] |
Latest revision as of 01:31, 31 July 2008
Pure Fabrication
Sometimes it is a good idea to introduce into a system an object that has no counterpart in the real world. This is called the pure fabrication pattern. Find examples of pure fabrication, and weave them into a narrative that can teach programmers when it is helpful and when it is not helpful to fabricate objects.
Background
All design patterns, regardless of their specific purpose, are born from the same basic principles. These principles include high cohesion, low coupling, and other features which result in flexible, maintainable, extensible code. Most often it is possible to separate the objects of an OO design into real world abstractions, or objects which reflect well known design patterns, such as a factory or an observer. Sometimes, however, the need for high cohesion, low coupling, and separation of dependence exists within software when there is no real world object representation or familiar design pattern to provide a solution. This is when pure fabrication is needed. Simply put, pure fabrication means creating a class object completely unrelated to the problem domain specifically for the purpose of achieving some degree of high cohesion, low coupling, potential for code re-use and high maintainability.
Pure Fabrication is often attributed to Craig Larman as part of his nine GRASP design patterns. Each of these patterns is similar to all other design patterns in that they strive to achieve code refactoring, decoupling, and reusability. Arguably, they are more generic than the GoF patterns, and pure fabrication is perhaps the most generic of them. In fact, pure fabrication is so generic that often existing well-known design patterns can be considered instances of Pure Fabrication. To put it another way, pure fabrication means to create a new design pattern.
Examples of Pure Fabrication
As mentioned above, several well known design patterns can be considered instances of Pure Fabrication. For example, Strategy and Command began as Pure Fabrications, but are common enough that a more specific name for each has been adopted. In a problem domain sense, they do not abstract a real world object. Each simply exists to promote decoupling between other objects in a system.
This following example of Pure Fabrication is given in Larman's book, Applying UML and Design Patterns - An Introduction to Object-Oriented Analysis and Design and Iterative Development. Consider a class Sale, which represented a purchase or transaction record. The system using the class requires storing the records in a relational database. The functionality required to do this could exist in the Sale class, however, this would mean Sale class would need to know specifics about the interface into the database. It would need to directly call database functions, which is not in the interest of the Sale class (this violates high cohesion). Also, if the system were to change its persistent storage mechanism in the future, the Sale class would have to be modified to adapt to the new storage interface (this violates low coupling).
The solution is to create a new class whose sole responsibility is to save Sale records. This class could be called PersistantStorageBroker, and Sale would interact with it via a single save() method. The save() method is generic enough that it does not introduce unnecessary knowledge into the Sale class, and high cohesion is maintained. The PersistantStorageBroker class would know the details of the relational database interface, and so if future modifications were made to adapt to a new persistant storage mechanism, the Sale class could still use the save() method. In this, low coupling is achieved. The only class which would require modifications then is PersistantStorageBroker, but that is OK because that is the intent.
The above picture shows how the Sale class without the PersistantStorageBroker class would adapt to a new database type. It would need an Adapter class to bridge the two interfaces.
The above picture shows how the PersistantStorageBroker class would facilitate adapting to a new database type.
It is easy to see that this Pure Fabrication is close to other patterns. It is almost an Adapter pattern, except an Adapter typically connects two existing interfaces. In both cases, a design pattern is used to accommodate change in the database interface. It is easy to recognize the first example as using the Adapter pattern: a new class is created to accept the old interface and connect it to the new interface. The other example is similar, but with some key differences. First, the Sale class does not directly interface with any database type, old or new. It only interfaces with the PersistantStorageBroker class. Second, the PersistantStorageBroker class exists from the very beginning; it is not added later when a new interface is required. In this, it anticipates change, unlike the Adaptor which comes about reactively. So, the PersistantStorageBroker class is not quite an Adapter, it is a Pure Fabrication.
The Case Against Pure Fabrication
It is important not to overuse Pure Fabrication, or other design patterns for that matter, if it results in many small classes with too much interaction between them. Too much of this can actually cause unexpected coupling problems despite the intent to decouple objects. Further, this can increase the yo-yo effect (both vertical and horizontal) while reading source code. If code is spread among too many classes it can affect maintainability in a different, yet equally as negative way as tightly coupled code.
Similarly, sometimes it is OK to write code that has some degree of coupling, as there is always a trade off. In some cases, code does not necessarily need to be "pluggable" or compatible with all possible future uses and modifications of the code. It is important to realize this is a judgement call, and the design of the code and it's intent must be carefully analyzed. Code that is known ahead of time to have potential reuse obviously would benefit from using the priciples of Pure Fabrication and other design patterns, code such as framework code. However, if concessions are made in the design "just in case" future modification is needed, it may be good to re-evaluate the separation of responsibilities. Remember, re-factoring code in the future for maintenance reasons is not always bad, if it can be foreseen that the modifications are trivial compared to the cost of creating and implementing a very flexible, adaptable design.
External Links
Pure Fabrication GRASP Pattern
Over-Architecting with Pure Fabrication
Pure Fabrication Notes
More GRASP Patterns
Back to the assignment page