Java設(shè)計(jì)模式之中介者模式(Mediator Pattern)簡介
Java設(shè)計(jì)模式的中介者模式是行為模式之一。定義一個(gè)中介對象來封裝系列對象之間的交互。中介者使各個(gè)對象不需要顯式地相互引用,從而使其耦合性松散,并且可以獨(dú)立地改變他們之間的交互。
如下圖所示:
生產(chǎn)者、消費(fèi)者、代理商之間的中介模式實(shí)例:
public abstract class PersonColleague { protected Mediator mediator; public Mediator getMediator() { return mediator; } public void setMediator(final Mediator mediator) { this.mediator = mediator; } protected abstract void message(String context); } public class Mediator { private PersonColleague consumer = null; private ProducerColleague producer = null; public ProducerColleague getProducer() { return producer; } public void setProducer(final ProducerColleague producer) { this.producer = producer; } public PersonColleague getConsumer() { return consumer; } public void setConsumer(final PersonColleague consumer) { this.consumer = consumer; } public Mediator() { } public synchronized void message(final String context) { if (consumer != null) { System.out.println(context); } else if (producer != null) { System.out.println(context); } } } public class ConsumerColleague extends PersonColleague { public ConsumerColleague(final Mediator mediator) { this.mediator = mediator; } @Override protected void message(final String context) { this.mediator.setConsumer(this); this.mediator.message("hello! i am a consumer"); } } public class ProducerColleague extends PersonColleague { public ProducerColleague(final Mediator mediator) { this.mediator = mediator; } @Override protected void message(final String context) { this.mediator.setProducer(this); this.mediator.message(context); } } public class Client { public static void main(final String[] args) { final Mediator mediator = new Mediator(); final PersonColleague person = new ConsumerColleague(mediator); final PersonColleague person1 = new ProducerColleague(mediator); person.message("I am a consumer"); person1.message("I am a producer"); } }
運(yùn)行結(jié)果:
hello! i am a consumer I am a producer
相關(guān)文章
SpringCloud Feign轉(zhuǎn)發(fā)請求頭(防止session失效)的解決方案
這篇文章主要介紹了SpringCloud Feign轉(zhuǎn)發(fā)請求頭(防止session失效)的解決方案,本文給大家分享兩種解決方案供大家參考,感興趣的朋友跟隨小編一起看看吧2020-10-10Java使用wait() notify()方法操作共享資源詳解
這篇文章主要為大家詳細(xì)介紹了Java使用wait() notify()方法操作共享資源,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10SpringCloud?eureka(server)微服務(wù)集群搭建過程
這篇文章主要介紹了微服務(wù)SpringCloud-eureka(server)集群搭建,?項(xiàng)目搭建的主要步驟和配置就是創(chuàng)建項(xiàng)目和引入pom依賴,本文通過圖文示例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07springmvc配置線程池Executor做多線程并發(fā)操作的代碼實(shí)例
今天小編就為大家分享一篇關(guān)于springmvc配置線程池Executor做多線程并發(fā)操作的代碼實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03SpringCloud服務(wù)注冊和發(fā)現(xiàn)組件Eureka
對于微服務(wù)的治理而言,其核心就是服務(wù)的注冊和發(fā)現(xiàn)。在SpringCloud 中提供了多種服務(wù)注冊與發(fā)現(xiàn)組件,官方推薦使用Eureka。本篇文章,我們來講解springcloud的服務(wù)注冊和發(fā)現(xiàn)組件,感興趣的可以了解一下2021-05-05java中將科學(xué)計(jì)數(shù)法轉(zhuǎn)換普通計(jì)數(shù)法的簡單方法
下面小編就為大家?guī)硪黄猨ava中將科學(xué)計(jì)數(shù)法轉(zhuǎn)換普通計(jì)數(shù)法的簡單方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12Mybatis動(dòng)態(tài)SQL之if、choose、where、set、trim、foreach標(biāo)記實(shí)例詳解
動(dòng)態(tài)SQL就是動(dòng)態(tài)的生成SQL。接下來通過本文給大家介紹Mybatis動(dòng)態(tài)SQL之if、choose、where、set、trim、foreach標(biāo)記實(shí)例詳解的相關(guān)知識,感興趣的朋友一起看看吧2016-09-09