Archive | September, 2014

Single Responsible Vs Interface Segregation Principle

16 Sep

https://www.linkedin.com/groupItem?view=&item=5917243273432281088&type=member&gid=64069&trk=eml-group_discussion_new_comment-discussion-title-link&midToken=AQH_rEWlnZ_gcg&fromEmail=fromEmail&ut=2Kqza2d1DaOSo1

Advertisements

Interface Segregation Principle

15 Sep

Better explained –

Liskov’s Substitution Principle(LSP)

12 Sep

Problem explained at here : http://www.oodesign.com/liskov-s-substitution-principle.html

Solution
———-

1. CREATE RECTANGLE class

package lsp;

/**
* @author kumarash
*
*/
public class Rectangle {

protected int hight;
protected int width;

/**
*
*/
public Rectangle() {

// TODO Auto-generated constructor stub
}

public final int getArea() {

return hight * width;
}

/**
* @return the hight
*/
public final int getHight() {

return hight;
}

/**
* @param hight
* the hight to set
*/
public final void setHight(int hight) {

this.hight = hight;
}

/**
* @return the width
*/
public final int getWidth() {

return width;
}

/**
* @param width
* the width to set
*/
public final void setWidth(int width) {

this.width = width;
}
}

2. CREATE Square class as

package lsp;

/**
* @author kumarash
*
*/
public class Square extends Rectangle {

/**
*
*/
public Square() {

// TODO Auto-generated constructor stub
}

public void setHightWidth(int hightWidth) {

this.hight = hightWidth;
this.width = hightWidth;
}

}

3. Now test it , must be follow lsp !!!

package lsp;

/**
* @author kumarash
*
*/
public class lspTest {

/**
*
*/
public lspTest() {

// TODO Auto-generated constructor stub
}

/**
* @param args
*/
public static void main(String[] args) {
final Rectangle rectangle=new lspTest().rectangleFactory();
rectangle.setHight(3);
rectangle.setWidth(6);
System.out.println(rectangle.getArea());
}

public Rectangle rectangleFactory() {

return new Square();
}

}

thank you