惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
酷 壳 – CoolShell
酷 壳 – CoolShell
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
腾讯CDC
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
N
Netflix TechBlog - Medium
有赞技术团队
有赞技术团队
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
P
Proofpoint News Feed
L
LangChain Blog
aimingoo的专栏
aimingoo的专栏
GbyAI
GbyAI
B
Blog
F
Fortinet All Blogs
T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
Google Developers Blog
A
About on SuperTechFans
C
Check Point Blog
Microsoft Security Blog
Microsoft Security Blog
MyScale Blog
MyScale Blog
B
Blog RSS Feed

Yusuf Aytas

When Code Is Cheap, Does Quality Still Matter? Why Crouching Tiger, Hidden Dragon Is a Masterpiece Why We Ignore Advice The Mirror Is Part of the Machine When Too Many Maps Overlap on One Person The Work Runs on Different Maps Your Work Introduces You Trial By Fire The Dude Why Headcount Math Lies Capacity Is the Roadmap The Roadmap Is Not the System Torres del Paine W Trek Escaping Status Theater Incentives Drive Everything Scaling Culture Without Dilution What Good Looks Like Why Airport Security Feels Random Why Politics Appear How to Work with Me The Janus Protocol Multi-Horizon Delivery Framework What Good Execution Looks Like Managing Your Manager Why Kingdom of Heaven’s Director’s Cut Is Better AI Broke Interviews Most of What We Call Progress Managers Have Been Vibe Coding All Along Stop Wasting Brainpower Why Over-Engineering Happens
Mediator
Yusuf Aytas · 2010-11-02 · via Yusuf Aytas

Published · 2 min read

Bu yazımda size Mediator adındaki nesneye dayalı yazılım şablonunu (Object-Oriented Design Pattern) anlatacağım.Mediator bize, bjeler arasında fazla bağ kurmadan (tight-coupled) işlemlerimizi yapabilmeyi sağlar. Buradaki amacımız, objelerin biribirini bilmesine gerek kalmadan, bizim yarattığımız mediator sınıfını kullanarak işlerini yapabilmesidir. Genellikle ara yüz yazılımında kullanılan bu şablona şöyle bir örnek verebiliriz.  3 tane ışığımız var ve biz bunların kontrolünü sağlayacağız. Bu ışıklardan her seferinde birini açmak istiyoruz. İşte burada bu isteğimizi mediator sınıfında uyguluyoruz. Her ışığa basıldığında diğer ışıklarla ilgili işlemleri mediator sınıfında yapıyoruz. Dolayısıyla ışıkların biribirlerini bilmesine gerek kalmıyor. Aşağıda verdiğim kod örneğinde daha iyi anlamanızı sağlayacaktır.

Mediator tasarım deseni şemasıMediator tasarım deseni şeması

public interface Command{
	public void turnOn();
}
abstract public class Lamp implements Command{
	boolean isOn = false;
	Mediator mediator;
	public Lamp(Mediator mediator){
		this.mediator = mediator;
	}
	public void on(){
		this.isOn = true;
	}
	public void off(){
		this.isOn = false;
	}
	abstract public void turnOn();
}
public class GreenLamp extends Lamp{
	public GreenLamp(Mediator mediator){
		super(mediator);
	}
	public void turnOn(){
		mediator.turnOnGreenLamp();
	}
	public String toString(){
		if(this.isOn)
			return "GreenLamp is on";
		return "GreenLamp is off";
	}
}
public class RedLamp extends Lamp{
	public RedLamp(Mediator mediator){
		super(mediator);
	}
	public void turnOn(){
		mediator.turnOnRedLamp();
	}
	public String toString(){
		if(this.isOn)
			return "RedLamp is on";
		return "RedLamp is off";
	}
}
public class YellowLamp extends Lamp{
	public YellowLamp(Mediator mediator){
		super(mediator);
	}
	public void turnOn(){
		mediator.turnOnYellowLamp();
	}
	public String toString(){
		if(this.isOn)
			return "YellowLamp is on";
		return "YellowLamp is off";
	}
}
public class Mediator{
	Lamp yLamp,gLamp,rLamp;
	public void turnOnYellowLamp(){
		yLamp.on();
		gLamp.off();
		rLamp.off();
	}
	public void turnOnGreenLamp(){
		yLamp.off();
		gLamp.on();
		rLamp.off();
	}
	public void turnOnRedLamp(){
		yLamp.off();
		gLamp.off();
		rLamp.on();
	}
	public void selectYellowLamp(YellowLamp yLamp){
		this.yLamp = yLamp;
	}
	public void selectGreenLamp(GreenLamp gLamp){
		this.gLamp = gLamp;
	}
	public void selectRedLamp(RedLamp rLamp){
		this.rLamp = rLamp;
	}
}
public class MediatorDemo {
	public static void main(String \[\]args){
		Mediator m = new Mediator();
		YellowLamp yLamp = new YellowLamp(m);
		GreenLamp gLamp = new GreenLamp(m);
		RedLamp rLamp = new RedLamp(m);
		m.selectGreenLamp(gLamp);
		m.selectRedLamp(rLamp);
		m.selectYellowLamp(yLamp);
		yLamp.turnOn();
		System.out.println(yLamp);
		System.out.println(gLamp);
		System.out.println(rLamp);
		System.out.println("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*");
		rLamp.turnOn();
		System.out.println(yLamp);
		System.out.println(gLamp);
		System.out.println(rLamp);
	}
}