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

推荐订阅源

大猫的无限游戏
大猫的无限游戏
U
Unit 42
T
Tailwind CSS Blog
罗磊的独立博客
WordPress大学
WordPress大学
小众软件
小众软件
Recent Announcements
Recent Announcements
博客园 - 聂微东
Jina AI
Jina AI
云风的 BLOG
云风的 BLOG
博客园 - 【当耐特】
爱范儿
爱范儿
Microsoft Azure Blog
Microsoft Azure Blog
GbyAI
GbyAI
V
V2EX
博客园 - 三生石上(FineUI控件)
I
InfoQ
雷峰网
雷峰网
G
Google Developers Blog
阮一峰的网络日志
阮一峰的网络日志
B
Blog
腾讯CDC
A
About on SuperTechFans
博客园 - 叶小钗

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
Singleton
Yusuf Aytas · 2010-01-10 · via Yusuf Aytas

Published · 1 min read

Bu yazımda size Singleton adındaki nesneye dayalı yazılım şablonunu(Object-Oriented Design Pattern) anlatacağım. Singleton benim en çok kullandığım dizayn şablonlarından biri. Bu şablonu genelde yaratmak istediğiniz objeden yalnız bir tane olmasını istediğiniz durumlarda kullanırız . Örnek vericek olursak, bir oyun yazıyorsunuz ve bu oyununun GameEngine(Oyun Motoru) adında oyunu çekip çeviren bir sınıfı var ve birden fazla oyun motoru olmasını istemiyorsunuz. Bu GameEngine sınıfını singleton yapıyoruz ve bu nesneden birden fazla yaratılmasını engelliyoruz.

public class Singleton{
	private static Singleton singleton;
	private Singleton(){}
	public Singleton getInstance(){
		if(singleton==null)
			return (singleton = new Singleton());
		return singleton;
	}
}