C # design patterns (10) - bridge mode

Source: https://www.cnblogs.com/wyy1234/

 

Read catalog

 


 1. Introduction bridge mode

  Bridge mode for realization of the abstract and decoupling, so that both can be varied independently. In simple words using object oriented instructions: a class may be classified by a multi-angle, each classification may change, so isolated then put all angles vary independently between the various angles reduce coupling a multi-angle. Say may not be well understood, an example of the geometry Videos: we draw geometry can be classified according to the shape and angle of the two colors, according to the shape classification, divided circular, rectangular, triangular, divided by color classification graphics blue, yellow and red graphic pattern and shape, and color can all be added, for example, we can also add a five-pointed star shape, color can add a green. If implemented by inheritance, then, as shown in FIG. 1, we need specific subclasses have the nine kinds (type * color type shape), if we add a five-pointed star shape, you must add pentagram blue, yellow pentagram red five-pointed star and three concrete subclasses, addition of a color is the same need to add the color of various shapes. When the shape and color of the type we have a lot, you need a lot of specific sub-categories, resulting in a subclass explosion.
  Examples drawing only two angles classification, when a classified class has more angles, the concrete subclass type (Class 1 type 2 type * * Categories Category type 3 ...) even more. Then we can use the bridge mode optimization, the shape and color produced by inheritance relations strongly coupled into weak coupling relationship, where the combination is greater than the idea of using inheritance . The figure below, when using bridge mode, if we want to add a five-pointed star, a subclass only need to add a five-pointed star shape class access, do not need to go to add specific colors of the five-pointed star, if we want a blue five-pointed star and five-pointed star will be combined to obtain blue. Such design reduces the coupling of shapes and colors, the type of reducing concrete subclasses.

The role of bridge mode

Abstraction: abstraction generated class, such as the shape of the class ;
Implementor:行为实现接口 抽象化后关注的其他的特性,如例子中颜色接口。注意:我们可以把颜色抽象化生成抽象类,把形状作为行为实现接口;
RefinedAbstraction:抽象类子类,如圆形,长方形等;
ConcreteImplementor:行为实现接口的实现类,如黄色,红色等;
 
画几何图形例子的代码实现
 
形状抽象类和三种子类的形状:
复制代码
   public abstract class Shape
    {
        //形状内部包含了另一个维度:color
        protected IColor color;
        public void SetColor(IColor color)
        {
            this.color = color;
        }
        //设置形状
        public abstract void Draw();
    }

    /// <summary>
    /// 圆形
    /// </summary>
    public class Circle : Shape
    {
        public override void Draw()
        {
            color.Paint("圆形");
        }
    }
    /// <summary>
    /// 长方形
    /// </summary>
    public class Rectangle : Shape
    {
        public override void Draw()
        {
            color.Paint("长方形");
        }
    }
    /// <summary>
    /// 三角形
    /// </summary>
    public class Triangle : Shape
    {
        public override void Draw()
        {
            color.Paint("三角形");
        }
    }
复制代码

颜色接口和三种实现类:

复制代码
   /// <summary>
    /// 颜色接口
    /// </summary>
    public interface IColor
    {
        void Paint(string shape);
    }
    /// <summary>
    /// 蓝色
    /// </summary>
    public class Blue : IColor
    {
        public void Paint(string shape)
        {
            Console.WriteLine($"蓝色的{shape}");
        }
    }
    /// <summary>
    /// 黄色
    /// </summary>
    public class Yellow : IColor
    {
        public void Paint(string shape)
        {
            Console.WriteLine($"黄色的{shape}");
        }
    }
    /// <summary>
    /// 红色
    /// </summary>
    public class Red : IColor
    {
        public void Paint(string shape)
        {
            Console.WriteLine($"红色的{shape}");
        }
    }
复制代码

客户端调用代码:

复制代码
    class Program
    {
        static void Main(string[] args)
        {
            Shape circle = new Circle();
            IColor blue = new Blue();
            circle.SetColor(blue);//设置颜色
            circle.Draw();//画图

            Shape triangle = new Triangle();
            triangle.SetColor(blue);
            triangle.Draw();

            Console.ReadKey();
        }
    }
复制代码

程序运行结果

2.小结

上边例子的类图:

桥接模式的使用场景:

  当系统实现有多个角度分类,每种分类都可能变化时使用。近几年提出的微服务概念采用了桥接模式的思想,通过各种服务的组合来实现一个大的系统。

桥接模式的优点:

  1.实现抽象和具体的分离,降低了各个分类角度间的耦合;

  2.扩展性好,解决了多角度分类使用继承可能出现的子类爆炸问题。

桥接模式的缺点:

  桥接模式的引进需要通过聚合关联关系建立抽象层,增加了理解和设计系统的难度。

Guess you like

Origin www.cnblogs.com/frank0812/p/11280213.html