Grammar behind design patterns

Grammar behind design patterns.

In this section I will discuss a business case which will help us understand the grammar behind core design patterns. This example will also help the readers understand the importance of fundamental thinking.

It has been found out that mostly the engineers and the designers get influenced by the business jargons thrown by the customer and because of the lack of knowledge about the business jargons and domain finds the problem to be daunting and fails to understand the same.

Now this is where the basic difference lies….. If a person thinks as a layman, he might find the problem daunting because of the scale and ambiguity …But if the person is an engineer or a designer who understands the basic fact that "Technically every thing in this world is a system which is nothing more than structure and behavior".. then the same daunting problem looks extremely simple.

This is the first best practice that we share with any prospective designer or an architect …. "For any good designer or an architect, every thing in this world is a technical system, which in turn is made up of structure and behavior and hence they should never get influenced by the business jargons thrown by the customer."

The business case that I am describing next will help us understand the importance of this best practice. During my trainings when I describe this case to my participants and ask them to think about the solutions …. they just struggle to understand the problem , given the scale and the complexity of the problem as they mostly focus on the business jargons thrown by me while describing the case. Had they looked at the same problem from a systematic point of view …. the problem looks very simple with clearly defined answers.

So let me quickly describe this case …

Lets say there is a Bank named ABC based out of the US and this Bank is offering traditional banking services to its customers in the United States. Traditional banking services means the usual services offered by a bank in a manual or a non automated manner. Also there are 1000 banking professionals working in the bank using which the bank is offering its services in a non automated way.

Over a period of time, the customers of the bank are fed up with these non automated services and they start demanding "Technology Based Banking" … ie they want to access most of the services of the bank through all the technical gizmos like the PDA, Handheld devices, Internet, Information Kiosks, ATMs etc .

Now lets say you are the decision maker of the Bank lets say the CEO of the bank … how will you handle a crisis like this ?

Most of the times when I pose this question my participants are stunned because of the magnitude of the problem … they start saying that we will first do a feasibility study, followed by a risk mitigation planning ete etc. This happens because they get trapped into all the banking jargons that I have thrown while discussing the case.

Now let us understand the same problem from a systemic point of view.

Do we agree that every Bank is a technical system as well with behaviors and enabling structure?

Yes let us understand technically a Bank is a system wherein this bank is currently offering a behavior called traditional banking and the 1000 banking professionals who are working in this bank is the enabling structure which is giving this behavior.

Figure- Figure

Now over a period of time , the customers start demanding a new behavior from the Bank(System) called "Technology Banking". So I hope the readers will realize that this is a simple problem of behavioral change wherein there is an already existing system with some structure and behavior and now there is a need for a new behavior from the same system.

So what are the ways in which this behavioral change can be handled?

For the readers who have gone through my sections on Association (in OOAD principles) they should be able to think about the solution for the same?

let me repeat the same example of "My association with my Car" …. where in I as a Human being have the structures and behaviors defined by the class called as Human Beings. One fine day I come across a need of a behavior called as "Movement at 100 kmph" which is not supported by my base class [ie my body currently doesn't have an enabling structure which can enable my body to run at 100 kmph]. So what are the ways in which I can satisfy the need of a behavior which is not supported by my base class.

As you will realize …. one of the ways is to use an external system (Car) and get the need satisfied [Association]

While there the second option is to extend the structure of the existing system with a new new enabling structure which can incorporate the new behavior. [This is possible by extending the structure of a human being by permanently attaching a jet engine or a high powered motor or a engine on him].

The rules for association can be studied in detail on the following link.

Coming back to our original case…. The situation faced by the bank is similar to the situation faced by the Human body as they both face a simple problem of behaviorial change.

Hence there are two solutions for this problem

  1. Associate with an external system to get this new behavior
  2. Extend the existing bank with a new enabling structure to facilitate this new behavior.

The first option is nothing but outsourcing the technology related work to any technology service provider which could be any of the Indian TSPs like Infosys, Wipro, Polaris, TCS etc etc

The second option is to extend the company …lets the management of the bank decides to extend the bank by 20% ie they recruit 200 technology experts using which the new technology banking as a behavior will be delivered.

So there are two simple solutions … either the bank can outsource or extend.

The following figure shows the two solutions in the form of a class diagram.

Figure- Figure

In the figure TSP is an interface while Infosys, Wipro and Iflex are implementations of the TSP interface. So technically speaking outsourcing is nothing but simple association [shown by a green oval] and polymorphism [highlighted by a red oval].

Now think about the parameters that can help a person choose one of the two options ..

Or in other words

What are the parameters that should be true for Outsourcing to the best option?

and

What are the parameters that should be true for Extension to the best option?

To this my participants talk about multiple parameters like time, availability of resources, cost, security [IP Protection] etc etc.

Let us quickly understand each of these parameters and its repercussions…

Time - Time is a very important parameter which influences this decision. If technology banking as a behavior is needed immediately … then outsourcing may be a much better option as recruiting [recruitment and training ] may take a much longer time while on the other hand there are trained and readily available professionals who can quickly implement this behavior .

Availability of trained resources - This again can be an influential parameter .

Security [IP Protection] - If bank doesn't want to share its IP with the technology service provider, then the bank might take the decision of extending the company.

Cost - Cost can play a very critical role … but cost should not be associated with cheap labour. Let us understand Outsourcing and "Cheap labor" are two different issues and should be considered separately. This confusion is the courtesy the Indian IT Industry which has made "Outsourcing" and "Cheap Labor" synonymous.

Let us understand all of the above mentioned factors can influence the choice of decision , but there is one parameter which will clearly help us understand which option is a better way of handling this problem.

Ask yourself … what is the basic problem that we are trying to address ?

As we know .. this is a simple problem of behavioral change wherein there is a need for a extra behavior.

The moment we realize that it is a simple problem of behaviorial change …. don't you think that the first question that should hit our mind is

"Whether the change is a permanent change or a temporary/intermittent change?"

I hope you will realize this question will clearly give you an idea about the appropriate solution.

"A permanent behavioral change is a behavior which will always be used"

while "A temporary or intermittent behavioral change is a behavior which will not be used at all points of time."

The rule says …"If the change is a permanent change, then extension is a better way while if the change is either temporary or intermittent , the outsourcing is a better way of managing change".

I feel the reasoning for the above rule is self explanatory. But still let us quickly understand the same with an example.

Lets say Technology Banking as a behavior is non permanent behavior and the Bank only needs this behavior only for six months in a year and the Bank takes the decision of extending the bank with 200 technology professionals to incorporate this new behavior.

Now think about it … for the first six months when the technology banking behavior was needed … these 200 technology professionals were justifying the existence in the organization , but what will the bank do with these 200 technology professionals when this behavior is not needed for the next six months. Will the bank chuck / lay these 200 TPs when this behavior is not needed and recruit them once again when this behavior is not needed ?

Let us understand laying off/ chucking people from an organization is a very difficult decision which brings a lot of disrepute the company … and hence is one of the very difficult decision for the companies to take.

Remember extending a Class or a Organization or a family is a static way of managing change because once the decision of extension is taken it is either impossible or extremely difficult to undo this decision.

For example … marriage is a static way of managing change … wherein when a person extends his family with a new member there by enabling new behaviors . Now although there is a concept called as "Divorce" to get out of this association … but it is one of the very difficult decisions that a person can take in this life …

Hence the decision of extension of structure to incorporate a new behavior should only be taken if the change is a permanent change else the added structure will become a liability on the system when the behavior is not needed.

This principle can also be understood as the principle of core and non core behaviors. One of the fundamental principles of any well designed system says … that "A system should not do anything more than its core behavior, else the enabling structure of the non core behaviors will become a liability on the system when these non core behaviors are not needed"

Core Behavior - Core Behavior is a behavior which is always used/needed and hence the enabling structure for these core behaviors will never become a liability on the system. While

Non Core Behavior - Non Core Behavior is a behavior which is not needed / used at all points of time and hence any enabling structure for these non core behaviors will become a liability on the system when these behaviors are not needed.

There are multiple design patterns which use the concept of core/ non core behaviors to derive a solution.

So very simply we can say …. if the change is permanent use extension else use outsourcing or association.

Extension of structure is technically called as Class Inheritance . The keyword class is prefixed as classes are static in nature and hence the user will realize that he is taking a static decision to manage change.

Outsourcing is a business word…. It is technically called as Object Composition. The key word Object is prefixed as Objects are dynamic in nature and hence the user will realize that he is taking a dynamic decision to manage change.

Although it is very simple to say that .. if the change is permanent use "Class inheritance" else use "Object Composition" but this may not be true in certain situations.

let us consider a situation

lets say the Bank ABC realizes that Technology Banking as a behavior is a permanent behavior and hence they extend the bank by 20% recruiting 200 TPs to implement this behavior. But immediately after recruiting these 200 TPs the bank realizes that these 200 TPs either don't have the right skills, or attitude or knowledge to deliver this behavior. Now what can the bank do ….. should they lay off these 200 TPs and recruit another 200 TPs with the right skills….. ask yourself .. how can they lay off … they have recruited them…

So now the bank has two options , either it can take these 200 TPs as liability and recruit 200 more TPs with the right skill or take these 200 TPs as liability and outsource the work.

So I can reframe my previous best practice to take into account the above mentioned scenario.

I can now say "Class Inheritance" should only be used

  1. The change is permanent
  2. We are very confident about the structure needed to implement this behevaior

If both of the conditions is not true … Class inheritance is not the right way of managing change making way for Object Composition.

Class Inheritance if not done the right way will build liability within the system while Object Composition will build [Two levels]flexibility within the system.

The first level of flexibility comes because of the association relationship where in the bank is given the flexibility to use the TSP whenever they want , paying only for the services delivered whenever needed.

While the second level of flexibility comes because of the polymorphism highlighted by red oval. [TSP is a standard interface which any technology service provider will provide, but their implementions can be different ]

Lets say initially the Bank ABC gets associated with Infosys as the service provider. Lets say after couple of months … the bank realizes that Infosys is not able to provide the right services, they can easily switch from Infosys to Wipro … if after sometime if they are not even comfortable with Wipro .. they can switch to any service provider implementing the TSP interface till the interface remains constant. For all the reader who were struggling to understand why is it important in polymorphism for the behavior or the signature to be constant? .. .The answer lies here … if the interface (and its signatures) were not constant then the bank wouldn't have got the flexibility of switching the service providers are run time.

Now ask yourself ….

Why is "Outsourcing" a very successful business paradigm wherein every business is trying to outsource its non core activities or behaviors?

Let us understand … Outsourcing [Object Composition] is a Flexible way of designing a system because of the two levels of flexibility that object composition provides.

If we closely look into organizations today … the house keeping and security activities are outsourced to external service providers while previously the companies used to recruit house keeping and the security staff. The answer to this is companies today wants high level of flexibility with limited liability. So they only recruit those employees which are meant for its core behaviors [which are needed always] while for all the non core behaviors [which is not always needed] they outsource the task to a external service provider. This gives them two levels of flexibility , one to use the service whenever they want and even if one of the service providers is not able to give them the right service , they have the flexibility of changing the service providers at run time.

So let us understand Outsourcing is not just cost cutting by getting the work done by cheap labor … it is preferred today because of the flexibility provided by this concept.

So let me quickly summarize … Changes to a system can be managed either by Class Inheritance or Object Composition wherein Class Inheritance should only be used if the change is permanent and we are confident about the structure used to implement the changes. Object composition should be used if the two mentioned conditions is not true.

If the rules for Class Inheritance and Object composition is clear to the readers , then we are virtually covered 80% of core design patterns.

The following section shows how Class Inheritance and Object Composition becomes the building blocks of Core Design Patterns

Classification of Core Design Patterns

Core Design Patterns are best practices for designing highly flexible systems.

As we know flexibility as a parameter defines the adaptability of the system to various types of changes and there are three types of changes that can be possible within systems viz Structural Change, Behaviorial Change and Creational Change.

Hence design patterns can also be of three types viz

  1. Structural Design Patterns
  2. Behaviorial Design Patterns
  3. Creational Design Patterns

Structural Design Patterns are best practices for designing highly flexible systems wrt structural changes.

Behaviorial Design Patterns are best practices for designing highly flexible systems wrt behavioral changes.

Creational Design Patterns are best practices for designing highly flexible systems wrt creational changes. Creational changes are specific to factory based systems.

Irrespective of the type of change that needs to be managed we know there are two ways of managing changes within systems ie Class Inheritance and Object Composition.

Hence every design pattern has a scope which could be either be "class scope" or "object scope" or both.

If the pattern uses "Class Inheritance" as a technique to manage change the pattern is considered to be of class scope while if the pattern uses "Object Composition" as a technique to manage change the pattern is of object scope.

It should not be surprising to find out that out of the 23 core design patterns , 20 design patterns are of object scope rest 3 patterns are of class scope. As object composition gives two levels of flexibility hence most of the patterns uses object composition to manage changes.

As flexibility is one of the very important parameters needed within software systems … most of the programming languages, tools, frameworks, IDEs etc are designed using these 23 design patterns and hence are some times called as the Core Design Patterns.

As Object Composition is nothing but simple association and polymorphism and s most of the design patterns is using Object Composition to incorporate flexibility …. Design Patterns at a very basic level is nothing but commonsensical use of association and polymorphism.

We know Polymorphism also can be of two types viz Controlled and UnControlled Polymorphism wherein Interfaces are used to implement UnControlled polymorphism while Abstract Classes with Non Abstract methods marked in a form that it cannot be over ridden are used to implement Controlled Polymorphism.

So technically speaking design patterns is all about association and interfaces / abstract classes.

I presume any one who is reading this topic will know how to write an interface / abstract class in any programming language.

Hence we should not bother about implementing a design pattern as deep down it will be either implemented as an Interface / Abstract Class.

 
Hemant Jha
Founder - VPlanSolutions
Researcher, Trainer

www.VPlanSolutions.co.in