java 橋模式(Bridge Pattern)詳解
java 橋模式(Bridge Pattern)
Bridge模式解耦,其實施的定義。它是一種結(jié)構(gòu)模式。本模式涉及充當(dāng)橋的接口。這座橋使具體的類獨立的接口實施者類。
Bridge模式解耦,其實施的定義。它是一種結(jié)構(gòu)模式。
本模式涉及充當(dāng)橋的接口。這座橋使具體的類獨立的接口實施者類。
這兩種類型的類可以在不影響彼此被改變。
實例:
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(); } }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- Java反射之靜態(tài)加載和動態(tài)加載的簡單實例
- java 裝飾模式(Decorator Pattern)詳解
- java 實現(xiàn)多線程的方法總結(jié)
- Java反射之通過反射獲取一個對象的方法信息(實例代碼)
- Java從控制臺讀入數(shù)據(jù)的幾種方法總結(jié)
- Java中關(guān)于控制臺讀取數(shù)字或字符串的方法
- Java反射之類的實例對象的三種表示方式總結(jié)
- Java版本的回文字算法(java版本)
- Java創(chuàng)建數(shù)組的幾種方式總結(jié)
- java 過濾器模式(Filter/Criteria Pattern)詳細(xì)介紹
相關(guān)文章
關(guān)于Java的ArrayList數(shù)組自動擴(kuò)容機(jī)制
這篇文章主要介紹了關(guān)于Java的ArrayList數(shù)組自動擴(kuò)容機(jī)制,ArrayList底層是基于數(shù)組實現(xiàn)的,是一個動態(tài)數(shù)組,自動擴(kuò)容,不是線程安全的,只能用在單線程環(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é)合實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06使用AOP+反射實現(xiàn)自定義Mybatis多表關(guān)聯(lián)查詢
這篇文章主要介紹了使用AOP+反射實現(xiàn)自定義Mybatis多表關(guān)聯(lián),目前的需求是增強(qiáng)現(xiàn)有的查詢,使用簡單的注解即可實現(xiàn)多表關(guān)聯(lián),本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05