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

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
aimingoo的专栏
aimingoo的专栏
F
Fortinet All Blogs
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
MongoDB | Blog
MongoDB | Blog
月光博客
月光博客
The Cloudflare Blog
量子位
T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
MyScale Blog
MyScale Blog
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
G
Google Developers Blog
D
DataBreaches.Net
V
Visual Studio Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
U
Unit 42
博客园 - 聂微东
有赞技术团队
有赞技术团队
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
Abstract Factory
Yusuf Aytas · 2010-01-10 · via Yusuf Aytas

Published · 2 min read

Bu yazımda size Abstract Factory adındaki nesneye dayalı yazılım şablonunu (Object-Oriented Design Pattern) anlatacağım. Abstract factory,  bir veya birden fazla nesnenin farklı türlerinin, ihtiyaca göre yaratılmasını amaçlayan tasarım şablonudur. Örnek vererek daha iyi açıklamak istiyorum. Bir çok farklı türde telefon mevcut ve buda hepsinin kendine has platformu olmasına neden oluyor. Dolayısıyla  telefon için bir uygulama geliştirdiğimizde, yaptığımız bir düğme(button) yada bir panel ortama göre değişmesi gerekecektir. Bu durumda düğmeleri Button, çeşitlerinide Button'dan türeyen düğmeler şeklinde tasarlarız. Aynı işlemleri paneller içinde yaparız. Oluşturacağımız AbstractFactory'den türeyen sınıfları ise platforma göre çeşitlendiririz. Eğer bizden Android buttonı isternirse, dolayısıyla Android düğmesini AndroidFactory'den yararlanarak yaratabiliriz. Aynı şekilde bunu Android Panel içinde yapabiliriz. Eğer yeni bir platform gerekirse tek yapmamız gereken onun factorisini oluşturup, ona ait button ve panelleri eklemek olacaktır. Yazdığımız diğer sınıflar aynen kalabilir.

Abstract Factory tasarım deseni şemasıAbstract Factory tasarım deseni şeması

public abstract class ProductA{}
public class ProductA1 extends ProductA{}
public class ProductA2 extends ProductA{}
public abstract class ProductB{}
public class ProductB1 extends ProductB{}
public class ProductB2 extends ProductB{}
public abstract class AbstractFactory{
	public ProductA getA();
	public ProductB getB();
}
public class Factory1{
	public ProductA getA(){
		return new ProductA1();
	}
	public ProductB getB(){
		return new ProductB1();
	}
}
public class Factory2{
	public ProductA getA(){
		return new ProductA2();
	}
	public ProductB getB(){
		return new ProductB2();
	}
}
public class User{
	public static void main(String \[\]args){
		AbstractFactory factory = new Factory1();
		factory.getA();
	}
}