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.

Change component behavior dynamically

Change component behavior dynamically - Java Tutorial

From the course: Java Design Patterns: Structural

Start my 1-month free trial

Change component behavior dynamically

- [Instructor] Now that I've got a decorator class. I can use it to change any of my cycle objects. Let's say I want just the middle cycle, to have a red border and the others to stay the same. To do this I can go to my canvas class and scroll down to the paint method, and I'll put a new line above where I'm drawing cycle two. I want cycle two to have a red border. So I'll do circle two, is equal to a new component with red border, and I'll pass in the circle two object. That's all I need to do. So if I rerun the program now, I can see it's drawn canvas and circle two the middle circle now has a red border. So I've dynamically changed the color of the circle, without effecting any of the others. If I wanted to do things like add stripes to the shapes, or make them 3D, or make them twice the size, I could add decorators for all of these things too. I could also NS them. So if I wanted a circle with a red border and a…

Contents