Association and its best practises


Let's start our discussion by one of the most important and the most powerful but of the most dangerous relationship called as the "Association relationship".

This is the most fundamental relationship of the three.

Let's first try and understand what an Association relationship is.

Association relationship is nothing but a simple "Uses" relationship wherein one system uses the behaviour offered by another system."

Please understand that no one associates just for the sake of associating. If it is Association relationship, it is very obvious that one system is interested in using the behaviour offered by another system.

Now that we know what an Association relationship is, let's understand as to why we say that it is one of the most important and one of the most powerful relationships.

Let's go back to the system fundamentals where we discussed that every system in this world will offer some behaviour. And if a system is offering at least one behaviour, then there will be one more system in this world which will use that behaviour.

What do you understand by this? Is it not Association relationship?

When we discussed that systems cannot exist in isolation, is it not Association relationship that we are talking about?

What is the technical cause behind the implementation of a behaviour?

The components which are modelling the structural details of the system interact with each other in different ways to give different behaviours.

Is it not Association relationship that we are talking about?

Let's understand that without Association relationship, the concept of systems will fall flat. That's the reason we say that it is one of the most fundamental and of the most important relationships.

Now let's find out as to why two entities associate with each other or why do two systems associate with each other.

Let me give you an example.

"I" as an object of class "Human" is associated to an object of "Type Car".

Or

"I" as an object of class "Human" is associated to an object of "Type Laptop".

Or

"I" as an object of class "Human" is associated to an object of "Type Mobile".

Why am I associated with my car, Laptop or mobile phone?

Let's try to find out technically as to why I am associated with my car.

The moment I say that I am an object of class "Human", it is very clear that I have the structure and behaviour defined by the Class Human Being.

One day I come across a need of a behaviour called as "Movement at 100 kmph". And this as a behaviour is not supported by my base class.

When I say that my base class doesn't support this behaviour, it means that currently my system doesn't have the structure which can enable this behaviour.

So given the fact that I come across the need of a behaviour which is not supported by my base class, what are the options that are available to me to satisfy this need?

Whenever I pose this question, the first thing that comes to my mind or anybody's mind is "Associate".

Associate with a Car and get your work done. This is a human tendency. The moment we come across a need which is not supported by our base class, human tendency is to jump to an "Association relation" and get the work done.

But let me tell you that Association is not the only way of doing things. And Association may not always be the right thing to do.

I agree that "Association" is one way of getting things done. But is there any other way, technique or method by which I can get my need satisfied?

Apart from Association, is there any other way?

We know that component interaction is the technical cause behind the implementation of behaviour.

Given the fact that I am in need of a behaviour which is not supported by my base class and that currently I don't have the structure that can enable this behaviour, one of the ways in which I can get my need satisfied is I can think of extending my "current structure" with a "new structure" which can give me this behaviour.

Do you think it is possible?

Since we know that component interaction or structural component interaction is the technical cause behind the implementation of behaviour, I can simply take a decision of extending my structure with a new structure which can give me the desired behaviour.

The ways in which I can do it;

  • I can extend my structure by adding Wheels to myself permanently, assuming that this new structure can give me the desired behaviour. But as you know, just by adding wheels you cannot get the behaviour of movement at 100kmph.


  • I can extend my structure by adding Wheels to myself permanently, assuming that this new structure can give me the desired behaviour. But as you know, just by adding wheels you cannot get the behaviour of movement at 100kmph.


Let's understand that extension of structure is also a way of satisfying a need for a behaviour which is not supported by the base class. But sometimes, extension as a technique to incorporate the new behaviour may be too impractical or it may be too expensive.

For example, attaching wheels to me is an option to get the desired behaviour of 100 kmph but it may be too impractical an option.

Similarly attaching a jet engine over me is also an option. But it is too expensive.

If extension as a technique is either too impractical or too expensive for you to use, you can still get that behaviour if someone or some other system is publicly willing to give it to you.

When I say that there is an external system which is publicly willing to give you that behaviour, it means that you cannot force anyone into an Association relationship.

And the last thing is that you should be willing to pay for that association relationship.

When I say that you should be willing to pay for the association relationship, it means that implicitly or explicitly you always pay for Association relationship. Or in other words, "Association never comes for free" or there is no such thing called as "free lunch".

Association as a way of satisfying the need of behaviour should only be used when the following is true.

  • You are in need of a behaviour which is not supported by your base class.

  • Extension as a technique to incorporate a new structure to satisfy the new behaviour; is either impractical or too expensive.

  • There is an external entity which is publicly willing to give you the desired behaviour.

  • Last but not the least you should be willing to pay for the association relationship.


Only when the above are true, association is the right way to satisfy the need of Behaviour.

Although Association relationship is very important, it is very dangerous at the same time.

By now we have understood as to what are the technical rules that define the usage of association relationship.

Now let's get on to the dangerous part of Association relationship.

For me to explain this to you in a better way, let me give you an example.

Let's say Ramesh is a friend of mine. For better understanding let me give you the technical properties of Ramesh. Ramesh is an expert of writing cryptographic code. He is the best in it.One fine day I go to him and ask to write a particular code for me and also tell me as to how he is doing it.

Given a situation like this, what do you think should be Ramesh's most obvious reaction?


Whenever I ask this question, I get answers like,

  • "How much will you pay me if I tell you how I do it"?

  • "Why do you want to know how I do it"?


Let's analyse these answers.

"How much will you pay me if I tell you how I do it"?

If I say that I am going to give you 5 lakhs, will you tell me?

"Why do you want to know how I do it"?

If I tell you why I want to know, will you tell me"?

According to me, if I was in Ramesh's place, I would have said that you should only be concerned with your work getting done and not as to how I am doing it.

Now let's take a hypothetical case. Although Ramesh is very prudent, accidently he lands up telling me as to how he was doing that particular thing.

Can you think as to what all can happen to Ramesh, the moment he tells me as to how he was doing it?

We know that Association relationships never come for free. So you would realize that Ramesh was not doing it for "Free" for me or in other words, I was "Paying" for the service. If it is association relationship, implicitly or explicitly I have to pay for it.

The moment Ramesh tells me as to how he was doing it; I am wise enough to not go to him the second time. I would do the work myself. And if I don't go to him for the second time, he would lose that part of the value which he would have got from me had he not told me how he did that. This is still OK.

If I was a wrong person or if I was a wrong entity, the moment Ramesh tells me as to how he was doing the particular task, what I could have done was, I would have exposed the same service myself at a much cheaper rate. In this case, everyone who was associated with Ramesh would have started coming to me and I could have endangered Ramesh's existence in this world.

Why did I go to Ramesh in the first place? Or why would anyone go to Ramesh in the first place?

I went to Ramesh because he had something which I did not have. Or he had something which others did not have.

The moment Ramesh tells me as to how he was doing it; he no longer has the USP. The moment I start offering the same service at a much cheaper rate, everyone will start coming to me and I am virtually endangering Ramesh's existence in this world.

This is the dangerous part of Association relationship. Association relationship if not done the right way, the target object is at the risk of losing its existence in the world or in the system.

Now before we think about strategies as to how Ramesh can safeguard himself or what are the best practices associated to it, there is one bigger question that needs to be answered.

"Why did I go to Ramesh in the first place"?

I hope you will agree that "Ramesh" and "I" are two different objects of class Human Being. The moment I say that "Ramesh" and "I" are two different objects of class human being, I hope you will agree that we will have similar structure and similar behaviours. And that we have similar capabilities.

Do you agree that two objects of the same class will have the same structure, same behaviour and similar capabilities? This is a technical rule.

If Ramesh can write cryptographic code, even I should be able to do it.

Technical fact is that we are two objects of class human being and hence whatever Ramesh can do, even I should be able to do it. Whatever I can do, Ramesh should also be able to do it. Whatever we can do, even you should be able to do it.

So why did I go to Ramesh in the first place?

There are only 2 reasons for it.

  • I am extremely foolish

  • Although we are two objects of the same class, with the same structure and same behaviour, there is something different between me and Ramesh.


The first reason is definitely not right. I am definitely not foolish. So if I had gone to Ramesh, it was certainly because there was something that I didn't have and Ramesh has.

Now my question is; can you think as to what could be the difference between two objects of the same class? Or what is the difference between me and Ramesh although we are two objects of the same class human being?

Ok. Let me make it simpler for you.

What is the difference between me and Sachin Tendulkar? Sachin Tendulkar and you?

Both are human beings. Both have the same structure and same behaviour. So if Sachin Tendulkar can play cover drive, even I should be able to do it. If he can play cover drive, even you should be able to do it. All of us are human beings with similar structure and similar behaviour.

Then what is the difference between us?

Many times people fail to understand or rather have difficulties accepting that two human beings can have similar capabilities.

So what is the difference between two objects of the same class although they have same structure and similar behaviour?

Two objects of the same class will definitely have the same structure but they might have different "values" which are being stored within the structure, which is nothing but the state.

Or

I can say that two objects of the same class will have the same structure but they can have different states within the same. And it is quite possible that these states influence the behaviour.


But please understand that I went to Ramesh not for his state. I went to Ramesh for a behaviour which even I had.

Can you think what could be the reason then?

I will give you a clue. Whatever I have said for structure and state, use the same analogy and you will get the answer.

Do you agree that A behaviour can be implemented in multiple ways? Do you agree that A behaviour can have multiple implementations? Think about the definition of Polymorphism.

The definition of Polymorphism says, "Keeping the behaviour constant, switching the implementation at run time is called Polymorphism".

All of us are Polymorphic. Do you agree?

We all have the capability of having multiple implementations for the same behaviour and we all have the capability of switching the same at run time.

Now can you think what is the difference between 2 objects of the same class having the same structure and same behaviour?

Like structure and states, you can say, 2 objects of the same class will definitely have the same behaviour, but they can have different implementations of the behaviour.

If you are strictly thinking from a programming point of view, then this is something which is not possible in your programming languages directly.

In your programming languages it is not possible for two different objects of the same class to have different implementations of a single behaviour. This is the limitation of software systems.

But let me tell you that designers of your programming languages have created work around to basically simulate something like this. It's not that it is not taken into account in your programming language. But directly it's not possible.

In real world it is very much possible that 2 different objects of the same class will definitely have the same behaviour but can have different implementations of the behaviour.

I will give you an example for better understanding. Human Beings. Human beings are highly polymorphic. We have multiple implementations of the same behaviour. And we are capable of changing the same at run time.

Now that you have understood that 2 objects of the same class will definitely have the same behaviour but different implementations of the same, what is the difference between Ramesh and me?

Although both of us are capable of writing cryptographic codes, why did I go to him?

I went to Ramesh because he probably knows the best way of implementing that behaviour. I went to him because he probably knows the most secure way of implementing that behaviour. I went to him because he probably knows the most efficient and cost effective way of doing the same.

When I asked Ramesh as to how he was doing things, do you know what was I trying to do?

I was asking him for the implementation of a behaviour which I did not have.

And the moment Ramesh tells me the implementation of a behaviour which I did not have; I basically get into a position wherein I can endanger his existence in a big way.

So what is the difference between Sachin Tendulkar and me?

Both of us can play cover drive but Sachin knows the best way of implementing that cover drive.

What is the difference between me and Richard Branson? What is the difference between me and Dhirubhai Ambani?

Both of us are human beings. Both of us have the same structure and same behaviour. Both of us have similar capabilities. But what is different is the implementation of behaviours like risk taking, innovation etc. He has a different implementation. I have a different implementation.

Always remember the uniqueness of any object or a system is always stored in its internal state. And at any point of time, if the internal state or the implementation of behaviour is exposed to the outside world, the target system is at the risk of losing its existence in that corresponding system. And this is called as "Encapsulation violation".

This is a very important concept in designing of systems called as "Encapsulation".

What do you understand by a highly encapsulated system?

A highly encapsulated system is a system which only exposes the public behaviour while hiding the rest. While hiding the rest means hiding the internal structure and while hiding the implementation of a behaviour.

Why is encapsulation needed within systems?

Encapsulation is needed so as to make Association succeed. If there was no concept of encapsulation, then association as a relation wouldn't have succeeded.

The prerequisite for association relationship to succeed is that the objects participating or the systems participating in an association relationship should be highly encapsulated.

If your association with systems in this world is working, it is only because of encapsulation. You are doing it every minute, every day. It's just that you are not aware of it.

Tell me something. Who do you think is rich and famous or rich today? Who is doing very well today? Who is successful?

I have realised a common trend which says that people who are successful are highly associated. A lot of people want to associate with them. People always want to associate with someone who is rich, famous and successful.

Why is it that everyone wants to associate with people who are rich, famous and successful?

The reason is that these people have something which others don't have. They have an implementation of a behaviour which others don't have. Because they have something which others don't have, they basically land up using them. They use this association. They end up paying for that association. So the more association a person has the more money he accumulates and he becomes rich.

This is just a technical analogy that I have given you. It is right in most of the cases but may not always be true.


 
Hemant Jha
Founder - VPlanSolutions
Researcher, Trainer

www.VPlanSolutions.co.in