Thursday, 16 February 2017

Liskov’s Substitution Principle - A SOLID principle


Derived types must be completely substitutable for their base types.

Explanation:
To utilize the principles like inheritance, we create some class hierarchies (=extends some classes creating some derived classes).

LSP states that if a program module is using a Base class, then the reference to the Base class can be replaced with a Derived class without affecting the functionality of the program module i.e. we must make sure that the new derived classes just extend without replacing the functionality of old classes.

Otherwise the new classes can produce undesired effects when they are used in existing program modules.

package core.solid;
/**
 * Violation of Liskov's Substitution Principle
 */
classRectangle {
     protected int width;
     protected int height;

     public voidsetWidth(int width){
           this.width = width;
     }

     public voidsetHeight(int height){
           this.height = height;
     }

     public intgetWidth(){
           return width;
     }

     public intgetHeight(){
           return height;
     }

     public intgetArea(){
           return width * height;
     }   
}

classSquare extends Rectangle {

     public voidsetWidth(int width){
           this.width = width;
           this.height = width;
     }

     public voidsetHeight(int height){
           this.width = height;
           this.height = height;
     }
}

public classLSPTest {

     private staticRectangle getNewRectangle() {
           /**
            * It can be an object returned by some factory.
            */
           return new Square();
     }

     public static voidmain (String args[]) {
          
           Rectangle rObj = LSPTest.getNewRectangle();

           /**
            * User knows that rObj it's a rectangle.
            * It assumes that he's able to set the width
            * and height as for the base class
            *
            * But the height and width are setter has
            * been changed as per the SQUARE property.
            **
            */
           rObj.setWidth(5);
           rObj.setHeight(10);

          
           /**
            * Unexpected result as the area of SQUARE calculated but
            * he was expecting the area of rectangle.
            **/
           System.out.println(rObj.getArea());
     }
}

Rectangle and Square two classes are used in the Application. We extend the application and add the Square class. The square class is returned using a factory pattern, based on some conditions and we don't know the exact what type of object will be returned. But we know it's a Rectangle.

We get the rectangle object, set the width to 5 and height to 10 and get the area. For a rectangle with width 5 and height 10 the area should be 50. Instead the result will be 100(=because rectangle class is Substituted by the Square class).

Wednesday, 15 February 2017

Difference between Abstraction and Encapsulation in Java


Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. Encapsulation is implemented using private, package-private and protected access modifier.

Encapsulation is also called data hiding.


Abstraction is the process of abstraction in Java is used to hide certain details and only show the essential features of the object. In other words, it deals with the outside view of an object (interface).

Abstraction can be achieved using interface and abstract class.

Hide certain details and only show the essential features of the object

Wednesday, 1 February 2017

Object oriented design for a Restaurant

Let we start to design Object Oriented architecture of restaurant. Before start the design, we need to understand how a restaurant works.

Components required:
Customer
            Name
            Contact

selectDish(): Dish
Selects the dish from the menu.

callWaiter(): call upon a waiter.

placeOrder(d:Dish): Places the order and enjoy his meal once the dish is served on his table.

requestBill(): Ask for the bill to the waiter.

makePayment(): Pays for the services.

Waiter
            Name
            EmpId
            TableToWait
respondToCustomer(): Response to the customer’s calls on the tables he is waiting.

takeOrder(): Take the customer's order.

placeOrderToQueue(o:Order): Place the order in PendingOrderQueue.

handleOrderPreparedNotification(): Observer design pattern for notification
Wait for the order ready notifications.

serveDishToCustomer(d:Dish): Once notification is received, collects the dish and serves the dish to the corresponding customer.

takeBillRequest(): Receive the bill request from the customer.

acceptPayment(): Ask the Cashier to prepare the bill.

payMoneyToCashier(): Gives the bill to the customer and accepts the payment.

Chef
            EmpId

prepareDish(d:Dish): Get the next order from the PendingOrderQueue.

placeOrderInFinishedQueue(): Prepares the dish and push the order to finished order queue.

notifyOrderComplete(): Observer design pattern for notification
Send a notification that the order is ready.

Cashier
            Name
            EmpId

prepareBill(o:Order): Accepts the prepare bill request from the waiter for the given order details. Prepares the bills and hands it over to the waiter.

acceptPayment(): Accept the cash from the waiter towards the order.

Order (DTO)
            id
            Dish

Data structure used:
PendingOrderQueue
addOrder(o:Order): Waiter will place the order in the PendingOrderQueue.
getNextOrder(): Chef gets the next order from the PendingOrderQueue.

FinishedOrderQueue
getFinishedOrder(o:Order): Chef will prepare the dish and push the order to FinishedOrderQueue. 






Sunday, 29 January 2017

poll(), peek() and remove() Method in the Queue

E java.util.PriorityQueue.poll()
Retrieves and removes the head of this queue, or returns null if this queue is empty.

Specified by: poll() in Queue
Returns: the head of this queue, or null if this queue is empty


E java.util.PriorityQueue.peek()
Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

Specified by: peek() in Queue
Returns: the head of this queue, or null if this queue is empty

E java.util.AbstractQueue.remove()
Retrieves and removes the head of this queue. This method differs from poll only in that it throws an exception if this queue is empty.

Specified by: remove() in Queue
Returns: the head of this queue
Throws: NoSuchElementException - if this queue is empty




Related Posts Plugin for WordPress, Blogger...