As I mentioned in my previous article, Pure Fabrication GRASP Pattern, I want to complete my discussion of the nine GRASP Patterns that help aid developers in assigning responsibilities to objects in your web applications and winform applications. These nine GRASP Patterns are found in Craig Larman's excellent book, Applying UML and Patterns, An Introduction to Object-Oriented Analysis and Design and Iterative Development. I have talked about a number of these GRASP Patterns already, which you can find at the end of the article.
GRASP Patterns
GRASP stands for General Responsibility Assignment Software Patterns. As mentioned above, there are nine GRASP Patterns that I learned from Applying UML and Patterns by Craig Larman. The concepts mentioned by the software patterns are not unique. They are the same software patterns and principles that have been discussed in almost all books dealing with design patterns and object-oriented principles.
I think Craig Larman does an excellent job covering the software patterns as well as iterative development. If you are interested in the usefulness of these patterns, I recommend the book.
Polymorphism
Polymorphism can be talked about in numerous ways. I will be talking about it from only 1 perspective - using polymorphism to extend your applications and make them more pluggable. That is the point of the GRASP Pattern and usually the point of all books on object-oriented programming.
Polymorphism in this context entails creating polymorphic methods in base classes / interfaces that are overridden / implemented in concrete or derived classes.
Command Pattern
The simplest form of polymorphism may be the Command Pattern, which almost seems like an absurd pattern but can be incredibly powerful:
We can then implement the ICommand interface by a couple of classes to see the usefulness of polymorphism at work:
Now this example simply outputs “Hello” and “Goodbye” to the console, but you could certainly extend the idea to more useful ideas in your application. I believe Community Server uses the Command Pattern to queue work on periodic intervals that is a bit too time consuming to run all the time. I believe they use it for forum and blog statistics.
Dependency-Inversion Principle
The Polymorphism GRASP Pattern is essentially the Dependency-Inversion Principle object-oriented principle associated with class design. The Dependency-Inversion Principle states that:
- High-level modules should not depend on low-level modules. Both should depend on abstractions.
- Abstractions should not depend on details. Details should depend on abstractions.
This is exactly what we are doing with the Command Pattern. The main function above doesn't care about the WriteHello and WriteGoodbye classes as well as the details of their implementation of ICommand. You can easily add additional classes, such as WriteGoodMorning, that implement ICommand and the application will work fine. This provides us a very extensible and pluggable application.
Strategy Design Pattern
The whole purpose of the Strategy Design Pattern is to make your application more extensible and pluggable. I have already written two articles about the Strategy Design Pattern and how it supports both the Dependency-Inversion Principle and Open-Closed Principle:
Abstract Classes and Factory Method in Enterprise Library Data Access Application Block
Polymorphism is alive and well in the Enterprise Library Data Access Application Block (DAAB). Enterprise Library defines an abstract class, called Database, for which concrete classes ( SqlDatabase and OracleDatabase ) will derive. The concrete classes share a number of methods in Database as well as override a number of abstract methods to provide implementation details.
A Factory Method is used in your client application to shelter your application from the concrete classes. The concrete class is chosen via the Data Access Application Block configuation file.
One of the statements worth understanding a bit better is
In the DAAB, Database is an abstract class. As any abstract class, you cannot create an instance of it. You can only create concrete classes that derive from Database. The DAAB currently has 2 such concrete classes: SqlDatabase and OracleDatabase.
So given this piece of knowledge, we can infer that the DatabaseFactory.CreateDatabase() static method will probably return an object of SqlDatabase or OracleDatabase which will be assigned to db. The actual concrete class will probably be created on-the-fly based on some logic (in this case the settings in the DAAB configuration file). This type of method is called a Factory Method as it defers the decision of which class to instantiate.
Conclusion
Polymorphism is an incredibly powerful tool in your applications to make them more extensible and pluggable. Many of the incredibly useful design patterns, such as Command Pattern, Strategy Design Pattern, and Template Method Pattern are used for extending your application. Object-Oriented Principles, such as the Dependency-Inversion Principle and Open-Closed Principle, “rely” on polymorphism and those design patterns to describe their intent and value in your application architecture.
GRASP Patterns
I have talked about several other GRASP Patterns introduced in the book, Applying UML and Patterns, An Introduction to Object-Oriented Analysis and Design and Iterative Development. I will be talking about those yet to be covered in the following weeks.
- Information Expert ( Part I and Part II ) - A general principal of object design and responsibility assignment?
- Creator - Who creates?
- Controller - What first object beyond the UI layer receives and coordinates a system operation?
- Low Coupling - How to reduce the impact of change?
- High Cohesion - How to keep objects focused, understandable, and manageable?
- Polymorphism - Who is responsible when behavior varies by type?
- Pure Fabrication - Who is responsible when you are desperate, and do not want to violate high cohesion and low coupling?
- Indirection - How to assign responsibilities to avoid direct coupling?
- Protected Variations - How to assign responsibilities so that the variations or instability in the elements do not have an undesirable impact on other elements?
Agile Software Development, Principles Patterns and Practices
The GRASP Patterns are similar to Object-Oriented Principles that are associated with class design as discussed by Robert Martin. See my chapter by chapter review of Agile Software Development, Principles Patterns and Practices by Robert Martin.
David Hayden ( Website / Blog )
Drinking: Gyokuro Green Tea