java 橋形式(Bridge Pattern)詳解。本站提示廣大學習愛好者:(java 橋形式(Bridge Pattern)詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是java 橋形式(Bridge Pattern)詳解正文
java 橋形式(Bridge Pattern)
Bridge形式解耦,其實行的界說。它是一種構造形式。本形式觸及充任橋的接口。這座橋使詳細的類自力的接話柄施者類。
Bridge形式解耦,其實行的界說。它是一種構造形式。
本形式觸及充任橋的接口。這座橋使詳細的類自力的接話柄施者類。
這兩品種型的類可以在不影響彼此被轉變。
實例:
interface Printer { public void print(int radius, int x, int y); }//from www.j a v a2 s . c om class ColorPrinter implements Printer { @Override public void print(int radius, int x, int y) { System.out.println("Color: " + radius +", x: " +x+", "+ y +"]"); } } class BlackPrinter implements Printer { @Override public void print(int radius, int x, int y) { System.out.println("Black: " + radius +", x: " +x+", "+ y +"]"); } } abstract class Shape { protected Printer print; protected Shape(Printer p){ this.print = p; } public abstract void draw(); } class Circle extends Shape { private int x, y, radius; public Circle(int x, int y, int radius, Printer draw) { super(draw); this.x = x; this.y = y; this.radius = radius; } public void draw() { print.print(radius,x,y); } } public class Main { public static void main(String[] args) { Shape redCircle = new Circle(100,100, 10, new ColorPrinter()); Shape blackCircle = new Circle(100,100, 10, new BlackPrinter()); redCircle.draw(); blackCircle.draw(); } }
感激浏覽,願望能贊助到年夜家,感謝年夜家對本站的支撐!