橋接模式(Bridge)(對象結構型模式) -开发者知识库

橋接模式(Bridge)(對象結構型模式) -开发者知识库,第1张

參考文檔:

1.設計模式-可復用面向對象軟件的基礎

2.http://blog.csdn.net/hguisu/article/details/7529194(設計模式(六)橋連模式Bridge(結構型))


橋接模式(Bridge)(對象結構型模式)

理論方面的知識,可以參考參考文檔。

看一下bridge的結構:

橋接模式(Bridge)(對象結構型模式) -开发者知识库,第2张

書中講的window例子的結構:

橋接模式(Bridge)(對象結構型模式) -开发者知识库,第3张

代碼實現:

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();    }}


最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复