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

推荐订阅源

D
Docker
I
InfoQ
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
Y
Y Combinator Blog
博客园_首页
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
A
About on SuperTechFans
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
T
The Blog of Author Tim Ferriss
C
Check Point Blog
B
Blog RSS Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Engineering at Meta
Engineering at Meta
B
Blog
爱范儿
爱范儿
Stack Overflow Blog
Stack Overflow Blog
aimingoo的专栏
aimingoo的专栏
WordPress大学
WordPress大学
F
Fortinet All Blogs
月光博客
月光博客
GbyAI
GbyAI

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);
	}
}