In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to understand the bridging pattern of Java design pattern". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand the bridging pattern of Java design pattern".
I. what is the bridging mode
Bridging pattern (Bridge Pattern): separates the abstract part from its implementation part so that they can all change independently. It is a kind of object structural mode, also known as Handle and Body mode or Interface mode.
Second, the structure of the bridging mode
The following roles are included in the bridge mode structure diagram:
Abstraction (abstract class): an interface used to define an abstract class, which is generally an abstract class rather than an interface, in which an object of type Implementor (implementation class interface) is defined and can be maintained. It has an association with Implementor, which can contain both abstract and concrete business methods.
RefinedAbstraction (extended Abstract Class): extends the interface defined by Abstraction. In general, it is no longer an abstract class but a concrete class. It implements abstract business methods declared in Abstraction, and business methods defined in Implementor can be called in RefinedAbstraction.
The Implementor interface declares these basic operations, and the implementation is handed over to its subclasses. Through association relationships, you can not only have your own methods in Abstraction, but also call methods defined in Implementor to replace inheritance relationships with association relationships.
ConcreteImplementor (concrete implementation class): implement the Implementor interface concretely, providing different implementations of basic operations in different ConcreteImplementor. When the program is running, the ConcreteImplementor object will replace its parent object and provide the abstract class with concrete business operation methods.
Third, the use scene of bridge mode
When the object has a variety of changing factors, consider abstracting the changing factors and the scene, and then bridging; for example, the pen has different functions.
Fourth, the advantages and disadvantages of bridging mode
Advantages:
(1) separate the abstract interface and its implementation. The bridging pattern uses "associations between objects" to decouple the inherent binding relationship between abstractions and implementations, so that abstractions and implementations can change along their respective dimensions. The so-called changes of abstraction and implementation along their respective dimensions, that is, abstractions and implementations are no longer in the same inheritance hierarchy, but are "subclassed" so that they each have their own subclasses so that they can be combined with any subclasses. in order to obtain multi-dimensional composite objects.
(2) in many cases, the bridging mode can replace the multi-tier inheritance scheme, which violates the "single responsibility principle", has poor reusability, and has a very large number of classes. Bridging mode is a better solution than multi-tier inheritance scheme, which greatly reduces the number of subclasses.
(3) the bridging mode improves the expansibility of the system. If any one of the two changing dimensions is expanded, there is no need to modify the original system, which accords with the "opening and closing principle".
Disadvantages:
The main results are as follows: (1) the use of bridge mode will increase the difficulty of system understanding and design. Because the association relationship is based on the abstract layer, developers are required to design and program for the abstract layer from the very beginning.
(2) the bridging mode requires the correct identification of two independently changing dimensions in the system, so its scope of use has certain limitations, and how to correctly identify the two independent dimensions also needs some experience.
V. similarities and differences between decoration, bridging and adapter patterns
All three are structural design patterns, and all rely on abstraction.
Adapter mode:
The emphasis is on the function of adaptation. (adapter relies on abstraction)
The key points are:
The principal class and the adapter class implement the same interface A
The principal class depends on the adapter class
The adapter class relies on abstract interface B
The adapted class implements abstract interface B
The end result is that the subject class can use some of the previously unrelated functions of the adapted class.
Bridge mode:
Emphasis is placed on multi-dimensional changes. (the principal class directly relies on abstraction)
The key points are:
The principal class depends on abstract A
The principal class has several different implementation classes
Abstract A has several different implementation classes
The final effect is that the implementation class of the subject class and the abstract implementation class can change respectively in the two dimensions. If the subject class relies on multiple abstractions, the dimensions are increased to facilitate expansion.
Decorator mode:
Emphasis is placed on the decorative function. (the principal class not only depends on the abstraction, but also implements the abstract interface)
The key points are:
Abstract A has multiple concrete subclasses
The decorator class relies on abstract A
The decorator class implements abstract A
There are different subclasses in the decorator class
The end result is that (the decorator implementation class) does some method enhancement to (the original abstract subclass).
VI. The realization of bridging mode
First of all, the TV is abstracted and the behavior method of the remote control is provided.
/ TV set, providing abstract methods / public abstract class TV {public abstract void On (); public abstract void Off (); public abstract void tuneChannel ();}
Create a concrete TV set that inherits from the abstract TV class:
/ three-star TV, override the abstract method of the base class / public class Samsung:TV {public override void On () {Console.WriteLine ("the three-star TV is on");} public override void Off () {Console.WriteLine ("the three-star TV has been turned off") } public override void tuneChannel () {Console.WriteLine ("changing channels for three-star TV sets") }} / Changhong brand TV set, rewrite the abstract method of the base class / provide concrete implementation / public class ChangHong: TV {public override void On () {Console.WriteLine ("Changhong brand TV has been turned on") } public override void Off () {Console.WriteLine ("Changhong brand TV has been turned off");} public override void tuneChannel () {Console.WriteLine ("Changhong brand TV changes channels");}}
Then abstract the remote control in the overview and play the role of abstract words.
/ remote control in abstract concepts, acting as an abstract role / public abstract class RemoteControl {public TV implementor {get; set } / turn on the TV / the implementation is no longer provided in the abstract class, but the implementation / public virtual void On () {implementor.On () in the implementation class is called. } / turn off the TV / public virtual void Off () {implementor.Off ();} / change the channel / public virtual void SetChannel () {implementor.tuneChannel ();}}
Create a specific remote control class: here, I rewrite the method of changing the channel, but there are other methods that can be overridden
/ specific remote control class / public class ConcreteRemote:RemoteControl {/ public override void SetChannel () {Console.WriteLine ("rewrite channel change method"); base.SetChannel ();}}
Client code:
Static void Main (string [] args) {/ / create a remote control RemoteControl remoteControl = new ConcreteRemote (); / / Changhong TV remoteControl.implementor = new ChangHong (); remoteControl.On (); remoteControl.SetChannel (); remoteControl.Off (); Console.WriteLine () / / three-star TV remoteControl.implementor = new Samsung (); remoteControl.On (); remoteControl.SetChannel (); remoteControl.Off (); Console.Read ();}
In this way, the design of the bridge mode is realized, and the function realization method of the remote controller is not realized in the remote controller, but the implementation part is used to encapsulate it by another TV class, and the remote controller contains only one reference of the TV class. Through the bridging mode, we separate the abstraction from the realization, so that we can deal with the changes in these two aspects well.
Thank you for your reading, the above is the content of "how to understand the bridging pattern of Java design pattern". After the study of this article, I believe you have a deeper understanding of how to understand the bridging pattern of Java design pattern. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.