java 橋模式(Bridge Pattern)詳解
java 橋模式(Bridge Pattern)
Bridge模式解耦,其實施的定義。它是一種結(jié)構(gòu)模式。本模式涉及充當橋的接口。這座橋使具體的類獨立的接口實施者類。
Bridge模式解耦,其實施的定義。它是一種結(jié)構(gòu)模式。
本模式涉及充當橋的接口。這座橋使具體的類獨立的接口實施者類。
這兩種類型的類可以在不影響彼此被改變。
實例:
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(); } }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
關(guān)于Java的ArrayList數(shù)組自動擴容機制
這篇文章主要介紹了關(guān)于Java的ArrayList數(shù)組自動擴容機制,ArrayList底層是基于數(shù)組實現(xiàn)的,是一個動態(tài)數(shù)組,自動擴容,不是線程安全的,只能用在單線程環(huán)境下,需要的朋友可以參考下2023-05-05Mybatis注解方式完成輸入?yún)?shù)為list的SQL語句拼接方式
這篇文章主要介紹了Mybatis注解方式完成輸入?yún)?shù)為list的SQL語句拼接方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11springboot項目實現(xiàn)多數(shù)據(jù)源配置使用dynamic-datasource-spring-boot-starter
這篇文章主要介紹了springboot項目實現(xiàn)多數(shù)據(jù)源配置使用dynamic-datasource-spring-boot-starter,本文分步驟結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-06-06使用AOP+反射實現(xiàn)自定義Mybatis多表關(guān)聯(lián)查詢
這篇文章主要介紹了使用AOP+反射實現(xiàn)自定義Mybatis多表關(guān)聯(lián),目前的需求是增強現(xiàn)有的查詢,使用簡單的注解即可實現(xiàn)多表關(guān)聯(lián),本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-05-05