Design Patterns Note 3: bridge Yakuu [original]

Source: Internet
Author: User

Sorry, I did not care about it for some reasons. This will be done. Pai_^

Concept:
Bridge: separates abstract parts from their implementations so that they can all change independently.

--------------------------------------
The hot sun, when empty; there is no wind, it really makes people feel overwhelmed. Think of writing in a house without air conditioning last summerCode^_^ Is a test of human nature. Andytao was thinking about it, and smiled. Lunch break is almost over. Continue to write my code.

"Andy, come and help me see it !" A bunch of silver ringtones were passed in.

"Alas, how can we stay calm when you invite a beautiful girl ." Andytao thought, not dare to say it. "I said, what's wrong with you? You have a lot of things ."

"I heard from you at the last technical seminar that if an abstract class or interface has multiple concrete implementation classes (concrete subclass, in order not to use inheritance alone for the specific implementation of abstract classes or interfaces, leading to code confusion and poor reusability, you should adopt the bridge design mode. What is the same thing? Do you think it is good for me to use inheritance in this example ?"

"Oh, let me see ."

Public interface draw {
Public void paint ();
}

Public class drawcircle implements draw {
Public void paint (){
System. Out. println ("paint circle ");
......
}
......
}

Public class drawangle implements draw {
Public void paint (){
System. Out. println ("paint angle ");
......
}
......
}

"You see, I am not doing everything here, but I am doing well ."

"Well, let me explain it in detail. Generally, when an abstract class or interface has multiple concrete implementations (concrete subclass), the relationships between these concrete may be in the following two situations: the first is, these multiple implementations are in a parallel relationship. Just like your code, there are two concrete classes: circle and triangle; the two shapes are in parallel, there is no relative conceptual repetition, so we only need to use inheritance. ......"

"Don't sell off !" "……" "Okay. Can I invite you to drink cola ?"

Hey hey, you are successful, andytao continues, "but we have to consider the second case. If there is a conceptual repeat between two or more implementations, therefore, we need to separate the abstract common part and the behavior common part. It was originally intended to be placed in an interface. Now we need to design two interfaces to separate the abstract part and the behavior part."

"It's so abstract. I don't understand it !"

"Well, let's give an example, um ......, Take cola for example. The Cola we drink is divided into a big cup and a small cup, and there are ice and ice-free. In this way, if we use simple inheritance to implement these four specific implementations (big cups with ice, big cups without ice, small cups with ice, and small cups without ice), it is easy to overlap the concepts of each other, in addition, the Code is messy and cannot be easily maintained. So ......"

"So, why? Continue ."

"So, we have to adopt the Bridge Mode to redesign the class structure. If the bridge mode is used, we need to define two interfaces or abstract classes to separate the abstract part from the behavior part ."

"Wait a moment. Let's have a drink first ." "Come and come, use mine ." "That ......, Sorry, hey ......"

"Let's use Cola as an example. Cola is defined as an abstract class, and some common implementation code can be put in it. Ice and ice are behaviors, So we define it as a behavior interface ."

"Then, we can implement the following abstract class ."

Public abstract class coke {
Cokeimp;

Public void setcokeimp (cokeimp ){
This. cokeimp = cokeimp;
}

Public cokeimp getcokeimp (){
Return this. cokeimp;
}

Public abstract void distributecoke ();
}

Public abstract class cokeimp {
Public abstract void distributecokeimp ();
}

"Now we have two abstract classes (or interfaces ). Above, coke is the abstract part, and cokeimp is the defined behavior abstract class. In order to implement the four types of dynamic integration, we need to work hard on the specific implementation class ."

"This is Cola :"

Public class bigcoke extends Coke
{
Public bigcoke (){}

Public void distributecoke ()
{
System. Out. Print ("bigcoke ");
Cokeimp = This. getcokeimp ();
Cokeimp. distributecokeimp ();
}
}

"This is Cola :"

Public class smallcoke extends Coke
{
Public smallcoke (){}

Public void distributecoke ()
{
System. Out. Print ("smallcoke ");
Cokeimp = This. getcokeimp ();
Cokeimp. distributecokeimp ();
}
}

"I want to add ice :"

Public class icecokeimp extends cokeimp
{
Icecokeimp (){}

Public void distributecokeimp ()
{
System. Out. Print ("Ice Added ");
}
}

"Don't go ice :"

Public class nonicecokeimp extends cokeimp
{
Nonicecokeimp (){}

Public void distributecokeimp ()
{
System. Out. Print ("havn't ice ");
}
}

"Note that since our cokeimp and coke are one-to-one relationships, finding this * IMP in our use case is a key and difficult task ."

"Okay, now, what kind of cola do you want to drink ?"

"I want a small cup of cola and ice !" "This is simple. Here you are ......"

Coke coke = new smallcoke ();
Coke. setcokeimp (New icecokeimp ());
Coke. distributecoke ();

"I want a big cup of cola without ice !" "Oh, here !"

Coke coke = new bigcoke ();
Coke. setcokeimp (New nonicecokeimp ());
Coke. distributecoke ();

"The Bridge Mode is almost complete. In addition, bridge is widely used in GUI and other graphic image applications.ProgramJava AWT. In addition, Abstract Factory (Abstract Factory mode) is often used to create and set a bridge. The bridge mode is similar to the Object Adapter mode in adapter mode. The two models mentioned above have not been mentioned to you last time ."

"Oh, you said I remembered it." Helen bit his finger and looked very cute. "Wow, it's dead !" Andytao only has what you want. Pai_^

"However, you should pay attention to the fact that when designing the bridge class, you should pay attention to the modification of an abstract implementation part to avoid any impact on the customer, that is, the customer's Code does not have to be re-compiled. In addition, bridge will have many classes to generate. Such a hierarchical structure of classes indicates that you must break down an object into two parts: Abstract part and implementation part. In a word, the abstract part is separated from its implementation part so that they can all change independently. This is the essence of the bridge model, and ......"

"What else, like a Tang Monk ." "Well, don't forget your cola! Haha !!"

"You can't remember anything, that's exactly what you are doing ."

"Wow, you know me that well ." "Let's die, you ......" "Haha !!!"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.