Bridge mode of Java design mode (bridge)

Source: Internet
Author: User

1. Bridge mode is very similar to strategy mode

(in fact, a lot of design patterns are similar, because all the models are designed according to design principles , the principle of design is equivalent to the heart of martial arts, design mode is a move, as long as the heart is perfect, you can no recruit win a recruit .) )

There is also a more detailed description of the strategy mode VS. Bridge mode ( drawing, using crayons and brushes, and the difference between pigments )

Okay, here's the bridge model, and finally the difference between the two.

2. Bridge mode (Still, the design pattern is for code robustness, code reuse, so cohesion, low coupling is always so popular, what abstract class Ah, interface ah ...)

Scene, the farmer fertilizing.

Here farmers call Fertilizer class, using a combination of the way, that is, the peasant class has a member attribute is the type of fertilizer ( This is the role of the bridge),

In order to be extensible, the peasant class writes abstract class, the fertilizer class writes the interface;

In this action, not only the type of fertilizer will change, and the farmers will also change the side, artificial fertilization, or machine fertilization, and so on, so here are two variable factors.

And these two variables can also be randomly combined, such as artificial fertilizer, artificial organic fertilizer, or robot fertilizer, robot application of organic fertilizer.

Fertilizer interface Manure:

1  Package Design.patterns.bridge2; 2 /** 3 * Fertilizer Class 4  */5publicinterface  Manure {6     void manuring (); 7 }
View Code

The reason to use the interface is simple, polymorphic.

Organic fertilizer category, Animal fertilizer class Animalmanure:

1  PackageDesign.patterns.bridge2;2 3  Public classAnimalmanureImplementsmanure{4 5 @Override6      Public voidmanuring () {7System.out.println ("= = This is an animal fertilizer, that is, Xiang"));8     }9 Ten}
View Code

Fertilizer class Chemicalmanure:

1  PackageDesign.patterns.bridge2;2 3  Public classChemicalmanureImplementsmanure{4 5 @Override6      Public voidmanuring () {7System.out.println ("= = I am a fertilizer! ");8     }9 Ten}
View Code

Then to the abstract class farmer class Farmer:

1  PackageDesign.patterns.bridge2;2 /**3 * Peasant Class4  * */5  Public Abstract classFarmer {6     protectedmanure Manure;7     8      PublicFarmer (manure manure) {9          This. Manure =manure;Ten     } One      A     Abstract voiddomanuring (); -      -}
View Code

Human farmer Humanfarmer:

1  PackageDesign.patterns.bridge2;2 3  Public classHumanfarmerextendsfarmer{4     5      PublicHumanfarmer (manure manure) {6         Super(manure);7     }8 9 @OverrideTen     voiddomanuring () { OneSystem.out.println ("I am a big peasant, I love Labor"); A manure.manuring (); -     } -  the}
View Code

When technology is developed, there may be robotfarmerof robots to fertilize:

1  PackageDesign.patterns.bridge2;2 3  Public classRobotfarmerextendsfarmer{4 5      PublicRobotfarmer (manure manure) {6         Super(manure);7     }8 9 @OverrideTen     voiddomanuring () { OneSystem.out.println ("Robot can also fertilize the OH.") "); A manure.manuring (); -     } -  the}
View Code

Finally, Test the class:

1  PackageDesign.patterns.bridge2;2 3  Public classTest {4      Public Static voidMain (string[] args) {5         //start fertilizing6 //        //1. Preparation of fertilizers7 //manure manure = new Animalmanure ();8         //4. Suddenly want to apply fertilizer,9Manure manure =Newchemicalmanure ();Ten         //2. Artificial fertilization, the application of organic fertilizer, animal fertilizer OneFarmer Farmer =NewHumanfarmer (manure); A //        //5. Artificial fertilization, indeed slower, there will be a robot fertilization in the future - //Farmer Farmer = new Robotfarmer (manure); -          the          -         //3. Commencement - Work (farmer); -          +     } -      +      Public Static voidWork (Farmer Farmer) { A farmer.domanuring (); at     } -}
View Code

Hey, really ashamed, feel my example persuasive generally, if not understand, refer to Here Bridge Mode (bridge) (different platform to prepare a different format of the log file)

3. Summary:

Strategy (Policy mode) is the caller 's constant, changing only the method being called ; (candidate mode)
Bridge (bridge mode) is both the caller and the callee can be changed . --can be seen as a strengthened version of the strategy model (more step)

Bridge mode of Java design mode (bridge)

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.