Tell, Don't ask!

Tell, Don't ask!

Few days back I was having a discussion with one of my friends and we were discussing how to reduce coupling between objects to improve the design.

In that we have discussed this principle and how does it help to improve the design. So I thought to share with you all as well hence in this article I will mention what it is and how it helps to reduce coupling.

Consider a simple example of Fish. Consider you will feed the fish only if they are hungry.

Following is an example of how we design in general:

class Fish {

?????????private boolean isHungry;

?????????public boolean isHungry() {
??????????????return isHungry;
         }

         public void eat() { 
     ???????????// some work
          
          }      
}


class Feed {

???private Fish fish;

???public void feed() {
??????if (fish.isHungry()) {
        fish.eat();
??????}
???}
}        

What is the problem you can see here? Dependency on the internal state of other objects. Here we are first fetching the state of an object i.e. fish and then doing some action on it. So these 2 classes are tightly coupled. You can’t make any changes in Fish class without changing in Feed class.

So now, What if I can say, eat it if you hungry? What I meant is instead of asking it, tell it that eat it if you’re hungry? So that’s Tell don‘t ask.

So, the following are the changes:

class Fish {

??? private boolean isHungry;

	public void eatIfHungry() {
???????????if (isHungry) {
?????????????????//some work

        	}
	}

?}




class Feed {

???private Fish fish;

???public void feed() {

???????????fish.eatIfHungry();

???}

}        

Do you see what I am pointing to? Instead of tightly depending upon the state, you are just telling it to do the work. So by doing this, you are hiding the internal structure of fish from the caller. Also, we can independently make changes in Fish class without making any impact on the caller.

So as we see this well help us to make design less couple. And as a developer, we know cohesion is good and coupling is bad.

References:

https://martinfowler.com/bliki/TellDontAsk.html



要查看或添加评论,请登录

Tushar Goel的更多文章

  • DB Isolation Levels Part -2 (Repeatable Read and Serializable)

    DB Isolation Levels Part -2 (Repeatable Read and Serializable)

    In the last article, we learned about Read un-committed and Read committed isolation levels and the problems linked…

  • DB Isolation Levels in Detail - Part -1

    DB Isolation Levels in Detail - Part -1

    If two transactions don't touch the same data, they can safely run in parallel because they are not dependent on each…

    1 条评论
  • How Kafka is so efficient?

    How Kafka is so efficient?

    What is Apache Kafka? Apache Kafka is a distributed streaming platform that allows you to:: Publish and subscribe to…

  • Dependency Inversion

    Dependency Inversion

    Dependency ingestion is ‘D’ in the SOLID design principle. It is a guideline that helps design loosely coupled classes.

  • Law of Demeter (Principle of least knowledge)

    Law of Demeter (Principle of least knowledge)

    In the last article we have discussed Tell, Don’t ask guideline. In this article, we will discuss another guideline…

  • Concurrency Pattern: Active object pattern

    Concurrency Pattern: Active object pattern

    This pattern is a type of concurrency design pattern and widely used in applications. Also, it is used to create…

  • JavaScript: DOM Manipulation

    JavaScript: DOM Manipulation

    In this article, I will explain how to do DOM manipulation using pure JavaScript. We have many other libraries…

  • The JavaScript EventLoop

    The JavaScript EventLoop

    This post will explain one of the most important concepts of JavaScript i.e.

  • How to use an index in MongoDB?

    How to use an index in MongoDB?

    In this post, I will explain how to use an index in MongoDB. In an earlier article, I explained how indexes work.

    3 条评论