Design Patterns 5 : Bridge -- Independent variations

来源:互联网 发布:java输出语句快捷键 编辑:程序博客网 时间:2024/06/02 00:45

There are two parts in the system Abstraction (consumer) and Implementation (products), the abstraction part use the implementation part to finish some job,  and both the Abstraction and the Implementation are variable.

This is the place the bridge pattern comes to save the world.

 

The Shape aggregate the Drawing, and has no idea how the Drawing actually do the job.

Meanwhile the Drawing has no idea about the Shape at all.

In this way if we add more shapes or introduce new drawing implementations, it won't affect the other part.

 

 

And one more thing, the "One rule, one place" strategy.

In the above design, we add protected methods Shape::drawLine(), Shape::drawCircle(), use them to call the Drawing::drawLine(), Drawing::drawCircle(), instead of calling them directly in XXX::draw().

Which remove redundancy, and encapsulate the Drawing stuff only in Shape class.

Move the commonality upper, and keep the variability lower.  

原创粉丝点击