Design Pattern - result0924/iosLearning GitHub Wiki

Singleton

The singleton pattern is a way to make sure 
that a class has only one instance 
and it provides a single point of access to it.

The pattern specifies that the class should be responsible it self
for keeping track of its sole instance.

It can further ensure that no other instance can be created by intercepting
requests for creating new objects and provide a way to access the sole instance.

What's wrong with Singleton

  • Data inconsistency
  • Hidden dependencies
  • Untestable code
  • Breaks SOLID

When should we use Singleton?

  • Do need exactly one instance of that class, and creation of second instance can be dangerous

Refer

Factory

SOLID

Single Responsibility

  • Each class should have only one responsibility

Open-Closed

  • Class should be opened for extension but closed for modification
  • Open for extension
  • Closed for modification

Liskov Substitution

  • Child class should not break the parent class type definitions

Interface Segregation

  • Implement only what you need

Dependency Inversion

  • Depend on abstractions, not on concretions

Refer

State Machine