The course is part of this learning path
In this course, we will learn the concepts of microservice and spring framework with a focus on design patterns.
- Learn about various design patterns
- Beginner Java developers
- Java developers interested in learning how to Build and Deploy RESTful Web Services
- Java Developers who want to develop web applications using the Spring framework
- Java Developers who want to develop web applications with microservices
- Java Developers who wish to develop Spring Boot Microservices with Spring Cloud
- Basic Java knowledge
Hello dear friends. In this video, we will examine another structural pattern known as the Bridge Design Pattern, so 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 00P principles to separate out responsibilities into different abstract classes. Bridge design pattern is one of the structural design patterns. The bridge design pattern consists of 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 implementer is also an interface or abstract class.
The abstraction contains a reference to the implementer. Children of the abstraction are referred to as refined abstractions and children of the implementer are concrete implementers. Since we can change the reference to the implementer and the abstraction, we are able to change the abstractions implementer at runtime. Changes the implementer do not affect client code. The bridge UML schema includes elements such as abstraction, refined abstraction, implementer, and concrete implementation. So, let's go over these. Abstraction is the core of the bridge design pattern and defines the crux. It contains a reference to the implementer. Refined abstraction extends the abstraction takes the finer detail one level below. It hides the finer elements from implementers. The implementer is a 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 implementer interface. The last one is concrete implementer which implements the implementer 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 its 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. So, in my opinion, I think it will be easier for you to understand in 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 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, we need 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 constructer. We also need to setColor method. Let's define our shapes. For shape is triangle. Add a constructor with color. Now override the setColor method. Now we will add square, add constructor and override setColor method. Now the shapes are ready. Let's add black and white colors. Black implements color. Color will be black. Add white color. Okay, 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. Okay, done. Now the result will be black triangle and white square, let's hope. Okay, let's try. And as you can see, we successfully applied the bridge pattern to our project. So, that's all we need to know for the bridge pattern for now. In the following video, we will look into the composite pattern. So, I'll see you in the following video my friends.
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.