Last active
April 11, 2016 20:13
-
-
Save malalanayake/caa21198dcd56784a226e01992613c87 to your computer and use it in GitHub Desktop.
Open-Close Principal - Bad sample code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample.design.open.close.bad; | |
/** | |
* | |
* Distibution under GNU GENERAL PUBLIC LICENSE Version 2, June 1991 | |
* | |
* @author dmalalan | |
* @created Apr 11, 2016 1:32:39 PM | |
* | |
* @blog https://malalanayake.wordpress.com/ | |
*/ | |
public class Circle implements Shape { | |
public int getType() { | |
return 2; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample.design.open.close.bad; | |
/** | |
* | |
* Distibution under GNU GENERAL PUBLIC LICENSE Version 2, June 1991 | |
* | |
* @author dmalalan | |
* @created Apr 11, 2016 1:32:49 PM | |
* | |
* @blog https://malalanayake.wordpress.com/ | |
*/ | |
public class GraphicEditor { | |
public void drawShape(Shape s) { | |
if (s.getType() == 1) | |
drawRectangle((Rectangle) s); | |
else if (s.getType() == 2) | |
drawCircle((Circle) s); | |
} | |
public void drawCircle(Circle s) { | |
System.out.println("[PRINT:Circle]"); | |
} | |
public void drawRectangle(Rectangle r) { | |
System.out.println("[PRINT:Rectangle]"); | |
} | |
public static void main(String[] args) { | |
GraphicEditor ge = new GraphicEditor(); | |
// Print circle | |
Circle c = new Circle(); | |
ge.drawShape(c); | |
// Print Rectangle | |
Rectangle rec = new Rectangle(); | |
ge.drawShape(rec); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample.design.open.close.bad; | |
/** | |
* | |
* Distibution under GNU GENERAL PUBLIC LICENSE Version 2, June 1991 | |
* | |
* @author dmalalan | |
* @created Apr 11, 2016 1:31:29 PM | |
* | |
* @blog https://malalanayake.wordpress.com/ | |
*/ | |
public class Rectangle implements Shape { | |
public int getType() { | |
return 1; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample.design.open.close.bad; | |
/** | |
* | |
* Distibution under GNU GENERAL PUBLIC LICENSE Version 2, June 1991 | |
* | |
* @author dmalalan | |
* @created Apr 11, 2016 1:30:21 PM | |
* | |
* @blog https://malalanayake.wordpress.com/ | |
*/ | |
public interface Shape { | |
public int getType(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment