The Adapter pattern is a structural design pattern that allows incompatible interfaces to work together by creating a new interface that converts one interface into another. This pattern is particularly useful when integrating new components or libraries into existing codebases. In this example, the Adapter pattern is used to create a uniform drawing function for different shapes, despite their different drawing methods.
In this example, we have a simple drawing application where users can choose a shape (circle or square) and color, and draw the selected shape on a canvas by clicking on it. The Circle
and Square
classes have different methods for drawing (i.e., drawCircle
and drawSquare
), but we use the ShapeAdapter
class to create a uniform interface with a single draw
method. This way, we can easily extend our application with additional shapes without changing the core drawing functionality.