java 橋模式(Bridge Pattern)詳解
java 橋模式(Bridge Pattern)
Bridge模式解耦,其實(shí)施的定義。它是一種結(jié)構(gòu)模式。本模式涉及充當(dāng)橋的接口。這座橋使具體的類獨(dú)立的接口實(shí)施者類。
Bridge模式解耦,其實(shí)施的定義。它是一種結(jié)構(gòu)模式。
本模式涉及充當(dāng)橋的接口。這座橋使具體的類獨(dú)立的接口實(shí)施者類。
這兩種類型的類可以在不影響彼此被改變。
實(shí)例:
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();
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Java反射之靜態(tài)加載和動(dòng)態(tài)加載的簡單實(shí)例
- java 裝飾模式(Decorator Pattern)詳解
- java 實(shí)現(xiàn)多線程的方法總結(jié)
- Java反射之通過反射獲取一個(gè)對(duì)象的方法信息(實(shí)例代碼)
- Java從控制臺(tái)讀入數(shù)據(jù)的幾種方法總結(jié)
- Java中關(guān)于控制臺(tái)讀取數(shù)字或字符串的方法
- Java反射之類的實(shí)例對(duì)象的三種表示方式總結(jié)
- Java版本的回文字算法(java版本)
- Java創(chuàng)建數(shù)組的幾種方式總結(jié)
- java 過濾器模式(Filter/Criteria Pattern)詳細(xì)介紹
相關(guān)文章
Java實(shí)現(xiàn)二分查找樹及其相關(guān)操作
二分查找樹是一種有組織的二叉樹。我們可以通過鏈接節(jié)點(diǎn)表示這樣一棵樹,二分查找樹(Binary Search Tree)的基本操作有搜索、求最大值、求最小值、求前驅(qū)、求后繼、插入及刪除,對(duì)java二分查找樹相關(guān)知識(shí)感興趣的朋友一起看看吧2021-07-07
關(guān)于Java的ArrayList數(shù)組自動(dòng)擴(kuò)容機(jī)制
這篇文章主要介紹了關(guān)于Java的ArrayList數(shù)組自動(dòng)擴(kuò)容機(jī)制,ArrayList底層是基于數(shù)組實(shí)現(xiàn)的,是一個(gè)動(dòng)態(tài)數(shù)組,自動(dòng)擴(kuò)容,不是線程安全的,只能用在單線程環(huán)境下,需要的朋友可以參考下2023-05-05
Mybatis注解方式完成輸入?yún)?shù)為list的SQL語句拼接方式
這篇文章主要介紹了Mybatis注解方式完成輸入?yún)?shù)為list的SQL語句拼接方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
springboot項(xiàng)目實(shí)現(xiàn)多數(shù)據(jù)源配置使用dynamic-datasource-spring-boot-starter
這篇文章主要介紹了springboot項(xiàng)目實(shí)現(xiàn)多數(shù)據(jù)源配置使用dynamic-datasource-spring-boot-starter,本文分步驟結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
使用AOP+反射實(shí)現(xiàn)自定義Mybatis多表關(guān)聯(lián)查詢
這篇文章主要介紹了使用AOP+反射實(shí)現(xiàn)自定義Mybatis多表關(guān)聯(lián),目前的需求是增強(qiáng)現(xiàn)有的查詢,使用簡單的注解即可實(shí)現(xiàn)多表關(guān)聯(lián),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05

