Software patterns - GRASP (Part VII – Polymorphism)

Vishvvas
Posted by in Best Practices category on for Advance level | Points: 350 | Views : 11697 red flag
Rating: 4 out of 5  
 1 vote(s)

In this part of article series for "Software patterns- GRASP", we would explore the process of assigning responsibilties so has to handle the alternatives based on types through implemention of the pattern polymorphism.

Objective

To understand the GRASP pattern “Polymorphism”.

Preface


We have explored what are patterns and GRASP (in part I), Information Expert in part II and Creator in part II and “Controller” in part IV, “Low Coupling” in part V and “High Cohesion” in part VI. In this part VII, we would focus on next GRASP pattern named “Polymorphism”. Many of the experts regard the patterns we have visited in this series as patterns and remaining artifacts like “Polymorphism” etc as extensions to GRASP. Of course the word “Polymorphism” is omnipresent in the object oriented world and also it is one of the features of object oriented/ based languages. The dictionary meaning of the word “polymorphism” is  “(genetics) the genetic variation within a population that natural selection can operate on “.The biology meaning is “the existence of two or more forms of individuals within the same animal species (independent of sex differences)”. This looks closer to the meaning perceived by programming fraternity. In this context, it could be “calling the services in the different objects with the same name when such services are similar or related”. Such different objects have generally a common interface or common base class.

One of the challenges faced by object oriented programmers is “How to handle alternatives based on type? How to create pluggable software components?” This is the problem statement for this pattern.

The pluggable software means “one component can be replaced with other component without affecting the client (code)”.

Generally when instance behaviour changes slightly, the program is designed with conditional statements (like if or switch in C#). When a new type arrives, it needs modifications of such conditional statements occurring at several places. This is harder scenario to locate the conditional statements, making modifications and making sure it works. Also it can make the program a lot fragile and difficult to extend when a new variant.

Problem: How to handle alternatives based on type? How to create pluggable software components?

Solution: Assign responsibility for the behaviour (using polymorphic operations) to the types for which the behaviour varies.

 

It’s is recommended not to test for the type of an object and use conditional logic to perform varying alternatives based on type.

 

Approach:

Step I: Identify classes/ types which would have the behaviour changing slightly

Step II: Look for the methods which use conditional statements

Step III: Assign a responsibility to a type for which the behaviour varies

Description


Let’s discuss this pattern in the context of an example of POS (Point Of Sale) systems carried in series of articles. When a Sale is made, the payment has to be made. In the real world, the payment can be made in cash, by Card or by check. Generally in this scenario a single class with attribute for type of payment is created (as shown on the left side of the diagram) or it may have different implementations according to programmer’s convenience. This is achieved through conditional statement like

**********************************

If (“PaymentMode”== “Cash”).....

//Perform payment related actions

**********************************

This would need to be done at many places like a customer is given choice of payment mode, he/she chooses the payment mode and authorizes the payment. In such scenario when there is addition of payment mode, the changes have to be made at any places. This is what is termed as alternatives based on type.

When this pattern is employed, the design of the solution is as shown in the Fig. No.1. Payment is a base class for different modes of payment and as described above this pattern results into having either a common interface or a base class. The behaviour for different modes is varying i.e. for cash mode, a customer would pay cash directly, for mode liked “Credit” he would swipe the card and authorize the payment or for Check payment, he would issue a check.

Fig No. 1

Class

Relationship with other classes

Responsibility and method

Remarks

Payment

Base class for other classes

Authorize the payment. This could be a template method having some/no implementation.

 

 

CashPayment

CreditPayment

CheckPayment

Specialized class inherited from Payment class.

Authorize the payment for particular mode.

 

This introduces plug ability and also if a new type is introduces it doesn’t incurs much changes.

 

As seen above, if a new type is added which has different payment mode, this can be added seamlessly in this design culminating absolutely no/minimal changes.

Benefits

·         Extension : Un-envisaged or unknown extensions required for new variants are easy to add

·         Maintainability :- Maintenance and enhancements are simplified


Summary & Conclusion

In this part, we discussed the “Polymorphism” and also the principle behind this. We discussed the steps for locating the avenues for implementing the polymorphism so as to handle the alternatives based on types simultaneously achieving plug ability. We saw practical example from POS (Point Of Sales) and explanation.

To reiterate, following such patterns for design would result into highly maintainable, robust and reusable systems.

Happy Designing!!!

References:

Applying UML and Patterns
Craig Larman


Page copy protected against web site content infringement by Copyscape

About the Author

Vishvvas
Full Name: Vishwas Sutar
Member Level: HonoraryPlatinum
Member Status: Member,MVP
Member Since: 5/30/2011 2:13:10 AM
Country: India

http://www.dotnetfunda.com
Extensive and rich experience across gamut of technologies and programming languages like PB,VB,C++,VB.NET, C#, Classic ASP,ASP.NET, ASP.NET MVC.

Login to vote for this post.

Comments or Responses

Posted by: SheoNarayan on: 8/25/2011 | Points: 25
Hi Vishvvas,

Thanks for writing the great series of articles. Appreciate your commitment towards writing the series.

Keep it up!

Regards

Login to post response

Comment using Facebook(Author doesn't get notification)