design mode (Java)--bridge mode

Source: Internet
Author: User

Transferred from: Lu Zhenyu

Http://www.cnblogs.com/zhenyulu/articles/67016.html#!comments

I think we all have the experience of crayon painting when we were young. A large box of delight's crayons depicts the format pattern according to the imagination. And the brush under the Chinese painting is meticulous freehand brushwork, each exhibition style. Today, our story starts with a crayon and a brush.

Imagine to draw a picture, blue sky, white clouds, green trees, birds, if the screen size is large, then use crayons to draw a little trouble. After all, thin crayons to paint a blue sky, is a bit of trouble. If possible, it is best to have a large set of crayons, coarse crayons can be applied quickly. As for color, the best color to be thick, in addition to the blue sky and green space. In this way, if a set of 12 colors of crayons, we need two sets of 24, the same color of a thick and thin. Oh, the painting has not been painted, began to dream: if a set of medium crayon is better, so, not a lot of a total of 36 crayons.

Then look at this side of the brush, incredibly simple: a set of watercolor 12 colors, plus a large middle and small three brush. You can not underestimate this "primitive" combination, painting the blue sky with a large brush, painting small birds with a little brush, each with a special feature.

Oh, you are not already looked out, good, I want to say today is bridge mode. For a painting, we need to prepare 36 different types of crayons, and the use of brush three is enough, of course, with 12 kinds of pigments. Through bridge mode, we change the multiplication operation 3x12=36 to the addition Operation 3+12=15, which is not a small improvement. So what's the difference between a crayon and a brush?

In fact, the key difference between a crayon and a brush is whether the pen and color can be separated. The "GOF95" bridge model is intended to "decouple abstraction (abstraction) from implementation (implementation) so that they can vary independently". The key is whether to decouple. The color of crayons and crayons is inseparable, so it is necessary to use 36 colors, different sizes of crayons to draw pictures. and brushes and pigments can be very good decoupling, independent changes, it simplifies the operation. Here, the concept of abstraction is: "Brush paint with paint", and in the realization, the brush has a large medium and small third, the pigment has red and green blue and other 12 kinds, so it can appear 3x12 kind of combination. Each participant (brush and paint) can switch freely on his or her own degree of freedom.

Crayon because the pen and color can not be separated, resulting in pen and color two degrees of freedom cannot be changed individually, so that only 36 objects can be created to complete the task. Bridge mode transforms the inheritance relationship into a composite relationship, which reduces the coupling between systems and reduces the amount of code written. But this is only part of the many benefits of the Bridge model, and more of the content, please refer to design mode (-bridge pattern).

The code for this article is attached here:

 Package B11;  Public class Color {    public  String color;}

 Package B11;  Public class extends color{    public  Blue () {        this. color= "Blue";    }} 

 Package B11;  Public class extends color{    public  Red () {        this. color= "Red";    }} 

 Package B11; // import Java.awt.Color;  Public Abstract class Brush {    protected  Color C;      Public Abstract void Paint ();      Public void SetColor (Color c) {        this. c=C;    }} 

 Package B11;  Public class extends Brush {    publicvoid  Paint () {        System.out.println ("Using big Brush and Color "+c.color);}    }

 Package B11;  Public class extends brush{    publicvoid  Paint () {        System.out.println ("Using small Brush and Color "+c.color);}    }

 PackageB11; Public classb111 { Public Static voidMain (string[] args) {Brush b=NewBigbrush (); B.setcolor (NewRed ());        B.paint (); B.setcolor (NewBlue ());                B.paint (); b=NewSmallbrush (); B.setcolor (NewRed ());    B.paint (); }}

The difference between abstract and interface in Java

Http://www.cnblogs.com/gxinliug/archive/2011/04/27/2031246.html

design mode (Java)--bridge mode

Related Article

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.