/Design-Patterns-Swift

Implementations of Design Patterns with Swift

Primary LanguageSwift

Design-Patterns-Swift

A software design pattern is a general, reusable solution to a commonly occuring problem within given context in software design. It is not a finished design, it is a description or template for how to solve a problem that can be used in many different situations.


Why Design Patterns?

  • Improve maintability
  • Decrease dependencies

Types of Design Patterns

1 - Creational Patterns

Creating objects in a manner suitable for the situation.

  • Factory Method
  • Singleton

2 - Structural Patterns

Ease the design by identifying a simple way to realize relationships between entities.

  • Decorator
  • Adapter
  • Facade

3 - Behavioral Patterns

Common communication patterns between objects.

  • Strategy
  • Observer
  • Command
  • Template Method


I used Head First Design Patterns by Eric Freeman and Elisabeth Robson in Java book as reference and implemented them in Swift.



1) Strategy Pattern

-> Implementation in Swift


  • Defines a family of algorithms
  • Encapsulates each one
  • Makes them interchangable objects that can be set or switched at runtime
  • Has three parts: an object using a strategy, a strategy protocol, and a family of strategy objects.

UML Diagram

converter


2) Observer Pattern

-> Implementation in Swift


  • Subject, updates Observers using a common interface
  • Defines one-to-many relationship between set of objects
  • When the state of one object changes, all of its dependents are notified
  • Observers are loosely coupled in that the Subject knows nothing about them

UML Diagram


3) Decorator Pattern

-> Implementation in Swift


  • Attaches additional responsibilities to an object dynamically

UML Diagram


4) Factory Method Pattern

-> Implementation in Swift


  • Defines an interface for creating an object, but lets the subclasses decide
  • It should just return a concrete product

UML Diagram


5) Singleton Pattern

-> Implementation in Swift


  • Ensures a class has only one instance and provides a global point of access to it.

6) Command Pattern

-> Implementation in Swift


  • Encapsulates each request as an object
  • Removes dependency between receiver and invoker

UML Diagram


7) Adapter Pattern

-> Implementation in Swift


  • Converts the interface of a class into another interface the clients expect.

UML Diagram


8) Facade Pattern

-> Implementation in Swift


  • Provides a unified interface to a set of interfaces in a subsystem
  • Defines a higher level interface that makes the subsystem easier to use

UML Diagram


9) Template Method Pattern

-> Implementation in Swift


  • Defines the skeleton of an algorithm in a method, deferring some steps to subclasses.

UML Diagram