Skip to content

Instantly share code, notes, and snippets.

@malalanayake
Last active April 11, 2016 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malalanayake/caa21198dcd56784a226e01992613c87 to your computer and use it in GitHub Desktop.
Save malalanayake/caa21198dcd56784a226e01992613c87 to your computer and use it in GitHub Desktop.
Open-Close Principal - Bad sample code
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;
}
}
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);
}
}
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;
}
}
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