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 the Bridge pattern - Java Tutorial
From the course: Java Design Patterns: Structural
Implement the Bridge pattern
- [Instructor] When you have numerous hierarchies, your apps can get quite complex and inflexible. This shapes up is a good example of when to use the bridge pattern. So let's refactor it to make it more flexible. The first thing I'm going to do is make an interface for colored shapes. So I'll right click on the Java package, and go to New, Java Class. I'll call this color shape. And I'll choose the Interface option and press Enter. This interface is going to have a method called set color. So I'll do public void set color. And I need to pass in a graphics object, because in Swing, the color gets set on the graphics objects. The next step is to create some classes that implements this new color shape interface. So I'll create another new class and I'll call it blue color shape. So I'll go to New, Java Class and name it blue color shape. And I'll make sure that this implements the color shape interface. I need to…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.