Before model learning
What is the design model: when we design the program, we gradually formed some typical problems and solutions, which is the software model; each mode describes a problem that often occurs in our program design and the solution to the problem. When we encounter the problem described by the mode, the corresponding solution can be used to solve the problem. This is the design mode.
A design pattern is an abstract thing. It is not learned but used. Maybe you don't know any pattern at all, and you don't consider any pattern, but write the best code, even from the perspective of "pattern experts", they are all the best designs and have to say "best pattern practices". This is because you have accumulated a lot of practical experience, knowing "where to write code" is a design pattern.
Some people say: "If the level is not reached, you can learn it in vain. If the level is reached, you can do it yourself ". It is true that the mode is well-developed and may still not be able to write good code, let alone design a good framework. OOP understanding and practical experience reach a certain level, and it also means to summarize a lot of good design experience, however, it may not be the case that "no teacher can do anything", or you can say that, on the basis of level and experience, it is time for the system to learn the "mode, it is helpful to learn the experts' summary and verify your own shortcomings.
This series of design patterns learning notes is actually a Learning Record for the book "Java and patterns.
Definition of Bridge Mode
The purpose of the bridge model is to "decouple abstract actions and Implementation so that the two can change independently ".
Strong and weak associations:
The so-called strong association is the association that has been determined during the compilation period and cannot be dynamically changed during the runtime. The so-called weak Association is the association that can be dynamically determined and can be dynamically changed during the runtime.
Inheritance is strongly correlated, while aggregation is weak Association.
The bridge mode is designed to solve the problem of insufficient flexibility of "inheritance". It provides such a weak coupling solution implemented by the aggregation relationship.
The bridge mode is also called the Handle and Body mode or Interface mode.
Structure of the Bridge Mode
Structure chart
Examples/examples + examples/Wu6 + examples/examples + y/examples + PGJyPgo8L3A + CjxwPqOoMaOps + samples/zu6 + samples/zu6 + samples + csf-vcd4kpha + examples/Wu6 + examples/bKtc/Wu6 + examples/examples + examples/ejrLb4s + examples/a7 + dPatdey47LZ1/e1xLj8uN/examples + csf-vcd4kpha + o6w.o6m + examples examples/WoaM8YnI + CjwvcD4KPHA + s + nP87uvvcfJq77Nz/examples + examples/Wu6 + examples + PGJyPgo8L3A + CjxoMj60 + slryrxp11_vadi + Images + animated "brush: java; "> abstract class extends action {protected Implementor imp; public void operation () {imp. operationImp () ;}} class refined?action extends =action {public void operation () {// improved logic }} abstract class Implementor {public abstract void operationImp ();} class ConcreteImplementorA extends Implementor {public void operationImp () {System. out. println ("Do something... ");}}
Encapsulation of changes
Find the variable factors of the system and encapsulate them. It is usually called "encapsulation of changes ". Number encapsulation is actually a way to achieve the "open-closed" principle, and is complementary to the principle of combination/aggregation reuse.
The bridge mode is an excellent example of the principle of "encapsulation of changes" and the principle of combination/aggregation reuse.
A reconstruction based on the Bridge Model
Airbus, Boeing, And McDonnell-Douglas are both aircraft manufacturers that produce Passenger Plane and Cargo Plane ). A system is now designed to describe these aircraft manufacturers and their aircraft types. The structure of the two classes is as follows.
Structure chart without the Bridge Mode:
Structure chart after the bridge mode is used:
Use Cases
The bridge mode should be used in the following scenarios:
(1) If a system needs to increase more flexibility between the abstract role and the specific role of the component, avoid establishing static connections between the two layers.
(2) Any change to the role should not affect the client, or the change to the role should be completely transparent to the client.
(3) If one component has more than one abstract role and actual role, the system must dynamically couple them.
(4) Although there is no problem in using inheritance in the system, abstract roles and specific roles need to be changed independently, and design requirements need to be managed independently.
Structure Pattern Summary
There are seven schema patterns: adapter, decoration, synthesis, proxy, metadata, facade, and bridge.
Summary: