1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Comprehend the definition of Bridge pattern by Gof?

Discussion in 'C++' started by zxjun84, Jul 24, 2010.

  1. zxjun84

    zxjun84 New Member

    Joined:
    Jul 24, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    There is an object varying for two dimensionality which change commonly.we can decouple the two dimensionality ,so they can vary indepently and not mutually affect.For example ,when we want to draw a picture through pen.Pen may have different size like big pen or small pen.It also has different color like red or green.Apperently the pen as an object used to draw will vary through different matching of size and color.Now,we can utilize a "bridge" to connect the CSize class and CColor class.is it that?

    Code:
    abstract class Brush
    2     {
    3         protected Color c;
    4         public abstract void Paint();
    5 
    6         public void SetColor(Color c)
    7         { this.c = c; }
    8     }
    
    1   class BigBrush : Brush
    2     {
    3         public override void Paint()
    4         { Console.WriteLine("Using big brush and color {0} painting", c.color); }
    5     }
    1   class SmallBrush : Brush
    2     {
    3         public override void Paint()
    4         { Console.WriteLine("Using small brush and color {0} painting", c.color); }
    5     }
    1   class Color
    2     {
    3         public string color;
    4     }
    1 class Red : Color
    2     {
    3         public Red()
    4         { this.color = "red"; }
    5     }
    1    class Green : Color
    2     {
    3         public Green()
    4         { this.color = "green"; }
    5     }
    1   class Blue : Color
    2     {
    3         public Blue()
    4         { this.color = "blue"; }
    5     }
     1     class Program
     2     {
     3    public static void Main()
     4   {
     5     Brush b = new BigBrush();
     6     b.SetColor(new Red());
     7     b.Paint();
     8     b.SetColor(new Blue());
     9     b.Paint();
    10     b.SetColor(new Green());
    11     b.Paint();
    12 
    13     b = new SmallBrush();
    14     b.SetColor(new Red());
    15     b.Paint();
    16     b.SetColor(new Blue());
    17     b.Paint();
    18     b.SetColor(new Green());
    19     b.Paint();
    20   }
    
    2.Gof defines the bridge pattern as Decouple an abstraction from its implementation so that the two can vary independently.[/QUOTE]
     

Share This Page