Common Design Pattern

What is command design pattern? Explain this pattern based on below scenario
Map m=new HashMap(); HashMap m=new HashMap();
What is difference between Map m=new HashMap(); HashMap m=new HashMap();

Questions by mannam haribabu chowdary   answers by mannam haribabu chowdary

Showing Answers 1 - 5 of 5 Answers

The main difference of creation of hashmp instance

HashMap hm = new HashMap(); Here we are getting the HashMap methods only will get inherited.

Where as
Map mp= new HashMap(); // Here will get the Map interface methods inherited into class.

  Was this answer useful?  Yes


  • May 30th, 2014

Hi Prasad,

Sorry to say but this cannot be the correct answer.
HashMap class inherits the map interface, so it must be having all the methods from map interface.
Please read your answer after reading mine. You will get to know the concept.

  Was this answer useful?  Yes


  • Jan 6th, 2015

Using the Map mp = new HashMap() convention we can convert one collection into a different collection implementation. Thus if there are two classes which implementation for Map interface you can at runtime change the collection type. This is the preferred way for creating collections as per the Oracle recommendation.

  Was this answer useful?  Yes

Ganesh Bhat

  • Apr 2nd, 2015

Map m = new HashMap(); here the reference of interface Map holds the object of HashMap, this is called Liscov Substitution Principle. OO Principles encourages to write implementation to interfaces than to implementations. Thereby you can easily switch the implementations without affecting rest of your module.
This concept introduces Cold Spot and HotSpot in the design, HotSpot is place in your design, which allows you to change, cold is one frozen.

  Was this answer useful?  Yes

Thomas John

  • Sep 22nd, 2015

Command decouples the object that invokes the operation from the one that knows how to perform it. To achieve this separation, the designer creates an abstract base class that maps a receiver (an object) with an action (a pointer to a member function). The base class contains an execute() method that simply calls the action on the receiver.
An example can be seen when we go to a restruant and order food. The waiter takes the order and passes it to the kitchen. A cook in the kitchen will take the order and prepare the order. Here the customer is the object that invokes the operation, order is the command interface/abstract class and the cook is class that implements the command interface.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.


Related Answered Questions


Related Open Questions