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

推荐订阅源

B
Blog
V
Vulnerabilities – Threatpost
Apple Machine Learning Research
Apple Machine Learning Research
V
V2EX
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
Latest news
Latest news
博客园 - 三生石上(FineUI控件)
美团技术团队
aimingoo的专栏
aimingoo的专栏
Google Online Security Blog
Google Online Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Threatpost
Y
Y Combinator Blog
T
Tailwind CSS Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
A
Arctic Wolf
C
Cyber Attacks, Cyber Crime and Cyber Security
小众软件
小众软件
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
Tenable Blog
W
WeLiveSecurity
L
LINUX DO - 热门话题
D
Docker
Cyberwarzone
Cyberwarzone
量子位
A
About on SuperTechFans
The Last Watchdog
The Last Watchdog
雷峰网
雷峰网
C
CERT Recently Published Vulnerability Notes
P
Palo Alto Networks Blog
The Hacker News
The Hacker News
Blog — PlanetScale
Blog — PlanetScale
P
Proofpoint News Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
F
Full Disclosure
The Cloudflare Blog
T
The Blog of Author Tim Ferriss
T
The Exploit Database - CXSecurity.com
Engineering at Meta
Engineering at Meta
O
OpenAI News
Hacker News - Newest:
Hacker News - Newest: "LLM"
Scott Helme
Scott Helme
IT之家
IT之家
S
Secure Thoughts
MongoDB | Blog
MongoDB | Blog
L
Lohrmann on Cybersecurity
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News

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
Observer
Yusuf Aytas · 2010-08-13 · via Yusuf Aytas

Published · 2 min read

Bu yazımda size Observer adındaki nesneye dayalı yazılım şablonunu (Object-Oriented Design Pattern) anlatacağım. Bu şablonun genel amacı bir objeyle başka objeler arasında bağ kurmaktır (One to many dependency). Yani eğer objenin birinde güncelleme olduysa diğerlerinde de bunun yansımasını sağlamaktır. Burada objeler arasında da çok fazla bağ(not tightly coupled) kurmadan işlemleri yapmaya yarar. Peki nerelerde kullanılır ? Eğer bir objede olan değişiklik diğer objelerin güncellenmesini gerektiriyorsa kullanılabilir. Yada bir obje diğer objelerin hakkında bilgi sahibi olmadan onları güncellemesi gerektiğinde yine bu şablon kullanılabilir. Size aşağıda verdiğim kod kısmında ise bu şablonun nasıl kullanıldığını göstermek istiyorum. Kodu kısaca anlatacak olursak Observer adlı bir interface kullanıyoruz. Bu interface den yararlanarak(implement) yazdığımız iki obje var. Bunlardan biri BoxChart diğeri ise PieChart. Başka bir sınıf(class) olarakta Observable ve bundan yararlanarak ürettiğimiz ConcreteObservable sınıfımızı görmekteyiz. Bunlarda sınıflar arası iletimi sağlayacak elemanlarımız. En sonunda ChartHandler adlı bir sınıfta da uyguladığımız şablonu çalıştırıyoruz. Bu şablon genel olarak kullanıcı arayüzü yapımında kullanılır. Örnek verecek olursak Java bir çok kütüphanesi bu şablonu kullanır.

Observer tasarım deseni şemasıObserver tasarım deseni şeması

public interface Observer{
    public void handleNotification();
}
public interface Observable{
	public void addObserver(Observer observer);
	public void removeObserver(Observer observer);
	public void notifyObservers();
}
public class BoxChart implements Observer{
	int \[\] data;
	public BoxChart(int \[\] data){
		this.data = data;
	}
	public void handleNotification(){
		System.out.println("Box Chart data is : "+data\[0\]);
	}
}
public class PieChart implements Observer{
	int \[\] data;
	public PieChart(int \[\] data){
		this.data = data;
	}
	public void handleNotification(){
		System.out.println("Pie Chart data is : "+data\[0\]);
	}
}
import java.util.ArrayList;
public class ConcreteObservable implements Observable{
	private ArrayList list;
	public ConcreteObservable(){
		list = new ArrayList();
	}
	public void addObserver(Observer observer){
		list.add(observer);
	}
	public void removeObserver(Observer observer){
		list.remove(observer);
	}
	public void notifyObservers(){
		for(int i=0;i