The course is part of this learning path
This course takes an in-depth look at how to use design patterns in your Java projects. We will then run through some example questions of what you could expect from the Oracle Certified Java EE exam.
- Understand what design patterns are
- Learn about the myriad of design patterns that you can use in your projects
This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java EE 6.
Basic knowledge of Java programming.
Hello, dear friends. In this video, we will examine another structural pattern known as the Bridge Design Pattern. Let's begin. The official definition for the Bridge Design Pattern introduced by Gang of Four (GoF), is to decouple an abstraction from its implementation, so that the two can vary independently. This means creating a bridge interface that uses OOP principles to separate out responsibilities into different abstract classes. Bridge Design Pattern is one of the Structural Design Patterns. Bridge Design Pattern consists from two parts. Abstraction, Implementation. This is a design mechanism that encapsulates an implementation class inside of an interface class. The Bridge Pattern allows the Abstraction and the Implementation to be developed independently, and the Client code can access only the abstraction part without being concerned about the implementation part. The Abstraction is an interface or abstract class, and the Implementor is also an interface or abstract class.
The Abstraction contains a reference to the Implementor. Children of the abstraction are referred to as RefinedAbstractions and children of the Implementor are ConcreteImplementors. Since we can change the reference to the Implementor in the Abstraction, we are able to change the Abstraction's Implementor at runtime. Changes to the Implementor do not affect Client code. The Bridge UML Schema includes elements such as Abstraction, RefinedAbstraction, Implementor, and ConcreteImplementation. Let us go over these. Abstraction, is the core of the Bridge Design Pattern, and defines the crux. Contains a reference to the Implementor. RefinedAbstraction extends the Abstraction, takes the finer detail one level below. Hides the finer elements from Implementors. The Implementor is an interface. It defines the interface for implementation classes. This interface does not need to correspond directly to the abstraction interface, and can be very different.
Abstraction implement provides an implementation in terms of operations provided by the Implementor interface. The last one is, ConcreteImplementor, which implements the Implementor interface. The Bridge Pattern is also known as Handle Body. So, let's look at the advantages of Bridge Pattern. Bridge enables the separation of implementation from the interface, and improves extensibility. Also, the Bridge allows the hiding of implementation details from the Client. So, when can we select this pattern to implement our project? If you want to avoid a permanent binding between the functional abstraction and it's implementation, or if you need both the functional abstraction and its implementation to be extended using sub classes. Or you can use the Bridge Design Pattern when you want the customer not to be affected by the changes made in the application.
I think it will be easier for you to understand it's practical application. So, right off, let's create a classic scenario for the Bridge Pattern. Let's say you'd like to create a triangle and a square. You also want these shapes in different colors, such as white, and black. In a standard structure, you will first create a shape, then a triangle and a square, followed by white triangle and a black triangle, etc., causing the number of classes to grow exponentially. However, using the Bridge Pattern, this will be much easier. So, let's begin. First, create a new project and name it bridge pattern. First, let's create an interface for color. Let's add a method as setColor. Now, add an abstract class for the shape. Now, in shape, we need color with protected. Add constructor. We also need setColor method. Let's define our shapes. First shape is triangle. Add constructor with color. Now, override setColor method. Now, we will add square. Add constructor and override setColor method. Now, shapes are ready.
Let's add black and white colors. Black implements color. Color will be black. Add white color. Done. The bridge structure is ready. Let's use, in main. First, create a triangle with black color. setColor. Now, let's add a white square. setColor. Done. Now, result will be black triangle with white square. Let's try. And as you can see, we successfully applied the Bridge Pattern to our project. That's all we have for the Bridge Pattern. In the following video, we will look into the Composite Pattern. See you in the following video.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.