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

推荐订阅源

SecWiki News
SecWiki News
量子位
The Cloudflare Blog
美团技术团队
T
The Exploit Database - CXSecurity.com
博客园 - 【当耐特】
Spread Privacy
Spread Privacy
P
Proofpoint News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 三生石上(FineUI控件)
T
Tor Project blog
博客园 - 司徒正美
宝玉的分享
宝玉的分享
T
Threatpost
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Secure Thoughts
T
Threat Research - Cisco Blogs
Hacker News: Ask HN
Hacker News: Ask HN
Jina AI
Jina AI
博客园 - 聂微东
A
Arctic Wolf
I
Intezer
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Know Your Adversary
Know Your Adversary
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
爱范儿
爱范儿
Hugging Face - Blog
Hugging Face - Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
小众软件
小众软件
T
Tailwind CSS Blog
The Hacker News
The Hacker News
L
LINUX DO - 最新话题
Hacker News - Newest:
Hacker News - Newest: "LLM"
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
TaoSecurity Blog
TaoSecurity Blog
Project Zero
Project Zero
博客园 - 叶小钗
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Cloudbric
Cloudbric
雷峰网
雷峰网
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
大猫的无限游戏
大猫的无限游戏
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Troy Hunt's Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
V2EX - 技术
V2EX - 技术
The GitHub Blog
The GitHub Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog

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 Prisoner's Dilemma Climbing No More The Weekly Win Mevlana Candy Brewing Turkish Tea Onboarding Your Engineering Manager Technical Deep Dives Yapay Zekâ Çağında Bilgisayar Mühendisliği Building Remote Teams From Idea to Launch in 2 Weeks Reflecting on Software Engineering Handbook Representing the Business New Manager Survival Guide Take Self Reviews Seriously Chasing Real Respect The Invisible Difference Learning the Johari Window Management is a Lonely Place Simple Task Management AI Balance in Work PIP Manager Insights Engineering Manager Interview Preparation Work-Life Balance as a Manager Bridging the Management Disconnect Tech Hiring Bubble Bursts Traits for EMs Simple Acts of Recognition Matter The Question I Ask Every New Report The Reality of an Employer's Market Bridging Ideals and Reality Hiring Red Flags Why The Godfather Is So Damn Good Subteam Tenets No Fluff Please Losing a Top Performer Balancing Act of Reliability Building Trust in Engineering Teams Ideal Number of Direct Reports Overriding a People Leader’s Decision From Misperception to Promotion Perception vs Perspective Setting Goals From Engineer to Manager Getting Delegation Right Interviewing Your Future Boss Celebrating Our Book in Iceland Operational Skills Needed On Writing Software Engineering Handbook Charlie Munger Quotes Working with Dependencies From Las Vegas to Canyons Navigating Layoffs Handling Competitive Dynamics A Weekend Getaway to Malta Engineering Health Essentials Should Dev Managers Code? Confronting the Life on Pause Winning Eleven Kindness is A Choice Bireysel Katılımcılar ve Yöneticiler Leading from Where You Are The Subtle Art of Listening Coding in Leadership The Power of Consistency The Making of a Leader The Path to Leadership Embracing TikTok Talent Sourcing Journey Leading Self Managing Teams Cracking Coding Bottlenecks
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);
	}
}