Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between Singleton and Registry design pattern

I have some confusion between those two patterns:

singleton
Check if the instance exists return it, or create a new one.

Registry
Check if the instance exists return it, or create a new one and store it.

What the difference between them?

like image 308
Lion King Avatar asked May 24 '13 22:05

Lion King


3 Answers

Both are about instance control. The difference is that Singleton allows only one instance of a given class while Registry holds a 1-1 map of keys to instances. Typically, the key is (or represents) a class and the value is an instance of that class.

For example, Code Igniter framework holds a registry with an instance of each library/model/controller/helper you load and returning those same instances every time.

like image 101
LexLythius Avatar answered Oct 17 '22 20:10

LexLythius


A Registry differs in that its principal purpose is to allow you to navigate to associated objects. See Martin Fowler.

like image 32
ashatch Avatar answered Oct 17 '22 20:10

ashatch


The main difference between registry and singleton is that singleton allow us to create a single instance of a class at a time, whereas registry allows us to create multiple instance of the same class.

like image 3
Wasim Abbas Avatar answered Oct 17 '22 19:10

Wasim Abbas