`
darezhong
  • 浏览: 9149 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
社区版块
存档分类
最新评论

设计模式学习之中介者模式

阅读更多

1.名词解释:

中介者模式:用一个中介对象来封装一序列的对象交互。中介者使各对象不需要显式地引用,从而使其耦合松散,而且可以独立第改变它们之间的交互。

 

2.代码示例:同事A、同事B通过中介者互相通信。

主控程序类,通过中介者,ColleagueA与ColleagueB进行通信

package DesignPattern.MeditorPattern;

public class Client {

	public static void main(String[] args) {
		ConcreateMeditor m = new ConcreateMeditor();
		Colleague A = new ColleagueA(m);
		Colleague B = new ColleagueB(m);
		m.SetColleagueA((ColleagueA) A);
		m.SetColleagueB((ColleagueB) B);
		((ColleagueA) A).send("B, May I love you?");
		((ColleagueB) B).send("A, I'm sorry, I already have a boy friend.");
	}
}
运行结果:Colleague B get a message: B, May I love you?
Colleague A get a message: A, I'm sorry, I already have a boy friend.

 

中介者抽象类:
package DesignPattern.MeditorPattern;

abstract class Meditor {

	public abstract void send(String message, Colleague colleague);
}

 

具体中介者类:
package DesignPattern.MeditorPattern;

public class ConcreateMeditor extends Meditor{

	private ColleagueA colleagueA;
	private ColleagueB colleagueB;
	
	public void SetColleagueA(Colleague colleague) {
		if(colleague instanceof ColleagueA) {
			this.colleagueA = (ColleagueA) colleague;
		}
	}
	
	public void SetColleagueB(Colleague colleague) {
		if(colleague instanceof ColleagueB) {
			this.colleagueB = (ColleagueB) colleague;
		}
	}
	
	@Override
	public void send(String message, Colleague colleague) {
		// TODO Auto-generated method stub
		if(colleague == colleagueA) {
			colleagueB.notify(message);
		} else {
			colleagueA.notify(message);
		}
	}
}

 

同事抽象类:
package DesignPattern.MeditorPattern;

public abstract class Colleague {

	protected Meditor meditor;
	
	public Colleague(Meditor meditor) {
		this.meditor = meditor;
	}
}

 

同事A类:
package DesignPattern.MeditorPattern;

public class ColleagueA extends Colleague {

	public ColleagueA(Meditor meditor) {
		super(meditor);
		// TODO Auto-generated constructor stub
	}
	
	public void send(String message) {
		meditor.send(message, this);
	}
	
	public void notify(String message) {
		System.out.println("Colleague A get a message: " + message);
	}

}

 

同事B类:
package DesignPattern.MeditorPattern;

public class ColleagueB extends Colleague {

	public ColleagueB(Meditor meditor) {
		super(meditor);
		// TODO Auto-generated constructor stub
	}
	
	public void send(String message) {
		meditor.send(message, this);
	}
	
	public void notify(String message) {
		System.out.println("Colleague B get a message: " + message);
	}

}

  

分享到:
评论

相关推荐

    设计模式之美—学习笔记

    在这个“设计模式之美”的学习笔记中,我们将探讨一些主要的设计模式,以及它们在实际开发中的应用。 首先,我们从创建型模式开始。这类模式主要用于对象的创建,如单例模式(Singleton)、工厂模式(Factory ...

    设计模式之蝉

    《设计模式之蝉》这本书可能是对设计模式的一种形象化描述,或是以蝉为比喻来讲解设计模式的概念。蝉在地下蛰伏多年,最终破土而出,仅生活在地面上的几周时间。这一生命周期与设计模式的持久价值有异曲同工之妙,即...

    设计模式学习报告

    ### 设计模式学习报告 #### 一、设计模式概述 设计模式是一种被广泛接受和使用的编程解决方案,旨在解决软件设计中的常见问题。它不仅能够帮助开发者编写出更易于理解和维护的代码,还能够促进代码的复用性。设计...

    《设计模式学习笔记》

    《设计模式学习笔记》主要探讨了GOF的23种设计模式以及类设计的基本原则,旨在帮助开发者理解和应用这些经过时间验证的成熟解决方案。设计模式是面向对象软件设计中的核心概念,它们为解决常见的设计问题提供了标准...

    设计模式学习 ppt

    这个“设计模式学习ppt”资料包显然是一份面向初学者或大学生的教学资源,通过十四个PPT文件深入浅出地讲解了设计模式的各个方面。 首先,我们来看设计模式的基本概念。设计模式是对在特定上下文中反复出现的问题...

    设计模式之美——教你写出高质量代码

    "设计模式之美——教你写出高质量代码"这个主题旨在帮助开发者更好地理解和应用设计模式,从而提升代码的质量和可维护性。设计模式不仅对面试有所帮助,也是职场发展中的必备技能,无论你使用哪种开发语言。 设计...

    java设计模式学习

    本资料“java设计模式学习”包含了对设计模式的深入理解和实际应用,通过简单实用的例子,帮助开发者掌握如何在Java项目中运用设计模式。 首先,我们要介绍的是工厂模式。工厂模式是一种创建型设计模式,它提供了一...

    24种设计模式以及混合设计模式

    通过学习和应用这些设计模式,开发者不仅可以提高代码的可读性、可维护性和可扩展性,还能提升团队间的沟通效率,因为设计模式是软件工程中的通用语言。对于任何有志于提升软件开发水平的人来说,理解和掌握设计模式...

    GOF设计模式中英文+设计模式精解中英文

    GOF(Gang of Four)设计模式,由Erich Gamma、Richard Helm、Ralph Johnson和John Vlissides四位专家在他们的著作《设计模式:可复用面向对象软件的基础》中提出,被誉为设计模式的经典之作。本资源包含了GOF设计...

    Head First 设计模式 +Java设计模式(第2版)

    《Head First 设计模式》与《Java设计模式(第2版)》是两本非常重要的IT书籍,专注于软件开发中的设计模式。...无论是初学者还是经验丰富的开发者,都应该把设计模式作为持续学习和改进的重要部分。

    设计模式学习资料

    Java设计模式,解说通俗易懂,推荐新手学习使用,文档中包含类图

    设计模式学习.zip

    本资源"设计模式学习.zip"聚焦于C++编程语言中的设计模式应用,是2017年的一次黑马程序员培训课程的配套代码,旨在帮助学习者通过实际的代码示例来理解和掌握设计模式。 在C++中,设计模式主要分为三大类:创建型...

    设计模式解析.pdf

    #### 描述解析:设计模式的入门与精通之路 描述中提到设计模式和面向对象编程,强调了它们对于软件设计师和开发者的重要性。这表明书籍将从基础出发,逐步深入,不仅教授设计模式的基础知识,还将引导读者理解设计...

    设计模式学习总结.doc

    在《设计模式学习总结》中,作者通过自己的学习经历和实际应用,分享了对23种经典设计模式的理解和感悟。这篇文档主要讨论了设计模式的概念、作用、应用以及学习设计模式时应注意的误区。 设计模式起源于面向对象...

    Java 经典设计模式讲解以及项目实战

    Java 经典设计模式讲解以及项目实战 设计模式简介:主要介绍各种设计模式的概念和运用场景等 设计模式综合运用:主要是笔者在实际工作中运用到的一些设计模式综合运用事例的提炼 Spring设计模式简介:主要是讲述...

    设计模式学习笔记.ppt

    设计模式学习笔记.ppt 自己写的一点学习笔记。

Global site tag (gtag.js) - Google Analytics