Bridging mode (bridge) (object-structured mode)

Source: Internet
Author: User
Tags abstract

Reference Documentation:

1. Design mode-the basis of reusable object-oriented software

2.http://blog.csdn.net/hguisu/article/details/7529194 (design mode (vi) bridge mode bridges (structural type))


Bridging mode (bridge) (object-structured mode)

Theoretical knowledge, you can refer to the reference document.

Look at the structure of bridge:


The structure of the window example that is spoken in the book:


Code implementation:

Brushpenabstraction.java:
Package com.rick.designpattern.bridge2;

/**
 * Created by MyPC on 2017/6/26.
 */Public
abstract class Brushpenabstraction {
    protected implementorcolor implementorcolor;


    public void Setimplementorcolor (Implementorcolor implementorcolor) {
        this.implementorcolor = Implementorcolor;
    } Public

    abstract void Operationdraw ();

}
Bigbrushpenrefinedabstraction.java:
Package com.rick.designpattern.bridge2;

/**
 * Created by MyPC on 2017/6/26.
 */Public
class Bigbrushpenrefinedabstraction extends Brushpenabstraction {
    @Override public
    Void Operationdraw () {
        System.out.println ("Big and" + implementorcolor.bepaint () + "drawing");
    }
}
Middlebrushpenrefinedabstraction.java:
Package com.rick.designpattern.bridge2;

/**
 * Created by MyPC on 2017/6/26.
 */Public
class Middlebrushpenrefinedabstraction extends Brushpenabstraction {
    @Override public
    Void Operationdraw () {
        System.out.println ("Middle and" + implementorcolor.bepaint () + "drawing");
    }
}
Smallbrushpenrefinedabstraction.java:
Package com.rick.designpattern.bridge2;

/**
 * Created by MyPC on 2017/6/26.
 */Public
class Smallbrushpenrefinedabstraction extends Brushpenabstraction {
    @Override public
    Void Operationdraw () {
        System.out.println ("Small and" + implementorcolor.bepaint () + "drawing");
    }
}
Implementorcolor.java:
Package com.rick.designpattern.bridge2;

/**
 * Created by MyPC on 2017/6/26.
 */Public
class Implementorcolor {
    protected String color;

    Public String Bepaint () {
        return color;
    }
}
Concreteimplementorblack.java:
Package com.rick.designpattern.bridge2;

/**
 * Created by MyPC on 2017/6/26.
 *
/public class Concreteimplementorblack extends Implementorcolor {public
    concreteimplementorblack () {
        This.color = "Black";
    }
}
Concreteimplementorblue.java:
Package com.rick.designpattern.bridge2;

/**
 * Created by MyPC on 2017/6/26.
 *
/public class Concreteimplementorblue extends Implementorcolor {public
    concreteimplementorblue () {
        This.color = "Blue";
    }
}
Concreteimplementorgreen.java:
Package com.rick.designpattern.bridge2;

/**
 * Created by MyPC on 2017/6/26.
 *
/public class Concreteimplementorgreen extends Implementorcolor {public
    concreteimplementorgreen () {
        This.color = "green";
    }
}
Concreteimplementorred.java:
Package com.rick.designpattern.bridge2;

/**
 * Created by MyPC on 2017/6/26.
 *
/public class Concreteimplementorred extends Implementorcolor {public

    concreteimplementorred () {
        This.color = "Red";
    }


}
Concreteimplementorwhite.java:
Package com.rick.designpattern.bridge2;

/**
 * Created by MyPC on 2017/6/26.
 *
/public class Concreteimplementorwhite extends Implementorcolor {public
    concreteimplementorwhite () {
        This.color = "white";
    }
}
Brushpenfactory.java:
Package com.rick.designpattern.bridge2;

/**
 * Created by MyPC on 2017/6/26.
 */Public
class Brushpenfactory {
    private static brushpenfactory _instance;
    Private Brushpenabstraction _brushpenabstraction;

    Private Brushpenfactory () {
    } public

    static Brushpenfactory instance () {
        if (null = = _instance) {
            _ Instance = new Brushpenfactory ();
        }
        return _instance;
    }

    Public Brushpenabstraction Makebrushpen () {
        _brushpenabstraction = new Smallbrushpenrefinedabstraction ();
        _brushpenabstraction.setimplementorcolor (New Concreteimplementorblack ());
        return _brushpenabstraction;
    }
}
Client.java:
Package com.rick.designpattern.bridge2;

/**
 * Created by MyPC on 2017/6/26.
 */Public
class Client {public

    static void Main (string[] args) {

        brushpenfactory.instance (). Makebrushpen ( ). Operationdraw ();
    }
}


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.