Skip to content

HagerElsayed/Design-Patterns

Repository files navigation

Design-Patterns

Factory Pattern

its one of a Creational Patterns which consist of:

  • Factory: which creates objects.
  • Protocol: which defines the methods that the products should implement.
  • Products: the objects that are created
UML Digram's Example:

Builder Pattern

its one of the Creational Patterns which consist of:

  • Product: which is a complex object to be created.
  • Builder: accepts step by step inputs and handle the creation of product.
  • Director: accepts inputs and coordinates with the builder.
UML Digram's Example:

Delegation Pattern

its one of a Behavioral Patterns because it's all about objects communication[one-to-one] which consist of three parts:

  • Delegating object: It’s the object that has a delegate. The delegate is usually held as a weak property to avoid a retain cycle.
  • Delegate protocol: which defines the methods that the delegate should implement.
  • Delegate: This is the helper object that implements the delegate protocol.

Example 1 UML Digram:
which represents communication between View Controller and its Service

Example 2 UML Digram:
which represents communication between View Controller and Presenter

Adapter Pattern

its one of a Behavioral Patterns which consist of four parts:

  • Client: Object using an adapter and depends on the protocol
  • protocol: which defines the methods we need
  • Adaptee/legacy object: cannot be modified directly so we need add extension or create new adapter
  • adapter: which helps us to make the incompatible objects deal with each other

UML Digram's Example:

protocol Target {
    func request()
}

class Adaptee {
    func specificRequest() {
        print("Specific request")
    }
}

class Adapter: Target {
    private let adaptee: Adaptee

    init(adaptee: Adaptee) {
        self.adaptee = adaptee
    }

    func request() {
        adaptee.specificRequest()
    }
}

let adaptee = Adaptee()
let adapter = Adapter(adaptee: adaptee)

adapter.request() // Prints "Specific request"

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages