Home > Java > javaTutorial > A detailed introduction to the code for implementing the Mediator pattern in Java

A detailed introduction to the code for implementing the Mediator pattern in Java

黄舟
Release: 2017-03-18 11:56:19
Original
1590 people have browsed it

Class diagram


##

public interface IMediator {

	public void createMediator();

	public void work();
}
Copy after login
/**
 * 中介、调节实际上需要交互的两个元素,让其松耦合
 * @author stone
 *
 */
public class Mediator implements IMediator {
	
	private XmlCode xmlCode;
	private XmlPreview xmlPreview;
	
	@Override
	public void createMediator() {
		this.xmlCode = new XmlCode();
		this.xmlPreview = new XmlPreview();
	}

	@Override
	public void work() {
		this.xmlCode.work();
		this.xmlPreview.work();
	}

}
Copy after login
public interface IWork {
	void work();
}
Copy after login
/**
 * 本类描述Android布局里的 预览界面
 * 在预览界面中拖拽组件,代码会变化
 * @author stone
 *
 */
public class XmlPreview implements IWork {

	@Override
	public void work() {
		System.out.println("预览里的组件变化了");
	}

}
Copy after login
/**
 * 本类描述Android布局里的 xml代码
 * 加入了代码,预览界面会变化
 * @author stone
 *
 */
public class XmlCode implements IWork {

	@Override
	public void work() {
		System.out.println("加入布局代码");
	}


}
Copy after login
/*
 * 中介者(Mediator)模式 	Mediator的意思是中介者、调节者、传递物,顾名思义,这个模式在程式中必然负担一个中介、调节、传递的工作
 * 中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互
 */
public class Test {
	public static void main(String[] args) {
		IMediator mediator = new Mediator();
		mediator.createMediator();//内部创建元素,维护元素
		mediator.work();//执行内部元素的work接口
	}
}
Copy after login

Print

加入布局代码
预览里的组件变化了
Copy after login

Related articles:

Mediator of java design pattern Pattern

Introduction to the chain of responsibility pattern of Java design patterns

Introduction to the proxy pattern (Proxy pattern) of Java design patterns

The above is the detailed content of A detailed introduction to the code for implementing the Mediator pattern in Java. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template