Talking about the design mode-3 minutes to understand the Bridge Mode: the relationship between pen and Painting

Source: Internet
Author: User

In fact, it does not take three minutes, and three seconds is enough. Remember that the bridge mode is so simple: In a word, there are thousands of PEN shapes and tens of thousands of changes in painting.

The following only helps to understand.

1. Definition

TheBridge patternIs a design pattern used in software engineering which is meant"Decouple an independent action from its implementation so that the two can vary independently"

From Wikipedia, the free encyclopedia: http://en.wikipedia.org/wiki/Bridge_pattern

2. Application scenarios

The Bridge pattern is useful when both the class as well as what it does vary often.

For example, you can use a pen to draw a painting. The pen includes a pencil, a pen, a brush, a pen, and a computer. The figures include straight lines, circles, triangles, and leaves. Independent changes.

3. Structure

Abstract action (abstract class)
Defines the abstract interface
Maintains the implementor reference.
Refinedabstraction (normal class)
Extends the interface defined by specified action
Implementor (Interface)
Defines the interface for implementation classes
Concreteimplementor (normal class)
Implements the implementor Interface

3. Instance

The following Java (SE 6) program contains strates the 'shape' example given below.

A simple description is the relationship between the pen and the image:

Pen: pencil, pen, brush, pen, etc.; graphics: straight lines, circles, triangles, leaves, etc. Independent changes.

/** "Implementor" */interface DrawingAPI {    public void drawCircle(double x, double y, double radius);} /** "ConcreteImplementor"  1/2 */class DrawingAPI1 implements DrawingAPI {    public void drawCircle(double x, double y, double radius) {        System.out.printf("API1.circle at %f:%f radius %f\n", x, y, radius);    }} /** "ConcreteImplementor" 2/2 */class DrawingAPI2 implements DrawingAPI {    public void drawCircle(double x, double y, double radius) {        System.out.printf("API2.circle at %f:%f radius %f\n", x, y, radius);    }} /** "Abstraction" */abstract class Shape {    protected DrawingAPI drawingAPI;     protected Shape(DrawingAPI drawingAPI){        this.drawingAPI = drawingAPI;    }     public abstract void draw();                             // low-level    public abstract void resizeByPercentage(double pct);     // high-level} /** "Refined Abstraction" */class CircleShape extends Shape {    private double x, y, radius;    public CircleShape(double x, double y, double radius, DrawingAPI drawingAPI) {        super(drawingAPI);        this.x = x;  this.y = y;  this.radius = radius;    }     // low-level i.e. Implementation specific    public void draw() {        drawingAPI.drawCircle(x, y, radius);    }    // high-level i.e. Abstraction specific    public void resizeByPercentage(double pct) {        radius *= pct;    }} /** "Client" */class BridgePattern {    public static void main(String[] args) {        Shape[] shapes = new Shape[] {            new CircleShape(1, 2, 3, new DrawingAPI1()),            new CircleShape(5, 7, 11, new DrawingAPI2()),        };         for (Shape shape : shapes) {            shape.resizeByPercentage(2.5);            shape.draw();        }    }}

It will output:

API1.circle at 1.000000:2.000000 radius 7.5000000API2.circle at 5.000000:7.000000 radius 27.500000

Boring to add the following text to meet the length requirements of the blog Park.

BridgeConcept of Pattern

The bridge mode is one of the constructor design patterns. Based on the minimum design principle of classes, the bridge mode enables different classes to assume different responsibilities by using encapsulation, aggregation, inheritance, and other behaviors. Its main feature is to separate abstract actions from implementation, so as to maintain the independence of each part and expand their functions.

BridgeApplication scenarios

Object-Oriented Programming (OOP) has the concept of class inheritance (subclass inherits the parent class). If a class or interface has multiple concrete implementation subclasses, if these subclasses have the following features:
-Subclass attributes that are relatively parallel.
-Conceptual crossover exists.
-Variability.
We can use the bridge mode to abstract and specifically, and reconstruct the relevant classes.

Talking about the design mode-3 minutes to understand the Bridge Mode: the relationship between pen and Painting

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.