From the course: Java Design Patterns: Structural

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Implement a Decorator class

Implement a Decorator class - Java Tutorial

From the course: Java Design Patterns: Structural

Start my 1-month free trial

Implement a Decorator class

- [Instructor] This is an app that draws components on a canvas, which is a real-life example of where the decorator pattern can be used when I want to extend the app. To keep this app simple for the demo, this version only has three classes. The first is an interface called Component which represents objects that can be drawn and it defines a single method called draw. In a real-life app there would loads of concrete implementations of this component interface for things such as text boxes, images, shapes, tool bars, and so on. In this simple example, I've just created one called circle. When a circle object is created, the X and Y coordinates for the position of the circle on the canvas are passed into the constructor. In the draw method, there's some functionality to draw a circle on the canvas. If you're not familiar with swing, don't worry. You just need to know that this line of code, graphics.drawoval, draws a…

Contents