Skip to content

Instantly share code, notes, and snippets.

@DhavalDalal
Created April 17, 2020 10:54
Show Gist options
  • Save DhavalDalal/54a9e0961011c7afabe0168d5ef82137 to your computer and use it in GitHub Desktop.
Save DhavalDalal/54a9e0961011c7afabe0168d5ef82137 to your computer and use it in GitHub Desktop.
Refactoring - ocp
  • How can I use circle, square, triangle and any shape that comes in future to draw?
  • How can I draw pictures that are composed from triangles, squares, or circles or any future shape?
  • I want to make sure that always circles are drawn first, squares are second and triangles are third?
class Canvas {
public void draw(Circle c) {
System.out.println("Canvas drawing a circle.");
}
public void draw(Square s) {
System.out.println("Canvas drawing a square.");
}
public void draw(Triangle t) {
System.out.println("Canvas drawing a triangle.");
}
public void draw(List<?> shapes) {
// loop through each shape and draw
System.out.println("Canvas drawing a picture here using the square, circle etc...");
}
}
class Circle {
public void draw() {
System.out.println("Drawing Circle...");
}
}
class Square {
public void draw() {
System.out.println("Drawing square....");
}
}
class Triangle {
public void draw() {
System.out.println("Drawing triangle....");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment