Design Patterns Flashcards

1
Q

Two main philosophies behind design patterns ?

A
  • Code reuse
  • Flexibility
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Name 5 creational design patterns

A

Singleton pattern
Builder pattern

Factory method pattern
Abstract factory pattern
Prototype pattern

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the singleton pattern

A

Restricts the initialization of a class to a single instance for the lifetime of the application

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Disadvantages of the singleton pattern

A
  • Introduces global state
  • Can introduce hidden dependencies and tight coupling
  • Dealing with race conditions in multi-threaded apps
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Advantages of the singleton pattern

A

Can share object without having to pass it in every method call (ex: store a Bluetooth connection in singleton to maintain connection across pages)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Code Example of Singleton Pattern

A

static variable

class MySingleton {
static let sharedInstance = MySingleton();
var myNumber = 0
private init() { }
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the builder pattern ?

A

Initializing an instance of a type that requires a large number of configurable values

A separate “builder type” is used that contains information required to initialize the complex type.

Have multiple “builder types” to initialize each type
OR
Single builder type and then set options as needed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Code Example of Builder Pattern

A

protocol BurgerBuilder {
var name: String {get }
var patties: Int {get}
var bacon: Bool { get}
var cheese: Bool {get}

var tomato: Bool: {get}
}

struct HamburgerBuilder: BurgerBuilder {
let name = “Hamburger”
let patties = 1
let cheese = false;

let tomato = false
}

struct CheeseburgerBuilder {
let name = “Cheeseburger”
let patties = 1
let cheese = true

let tomato = false
}

struct Burger {
var name: String
var patties: Int
var cheese: Bool

var tomato: Bool

init(builder: BurgerBuilder_ {
self.name = builder.name
self.patties = builder.patties

}

var myCheeseburger = Burger(builder: CheeseburgerBuilder());

var holdKetchupCB = Burger(CheeseBurgerBuilder())
holdTheKetchupCB.ketchup = false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Alternate Code for Build Pattern

A

struct BurgerBuilder {
var name = “Burger”
var patties = 1
var cheese = false

}

mutating func setPatties(choice: Int) { self.patties = choice }
}

// Bacon Cheeseburger
var burgerBuilder = BurgerBuilder()
burgerBuilder.setCheese(choice: true)
burgerBuilder.setBacon(choice:true)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Name some structural design patterns

A

Bridge
Facade
Proxy

Adapter
Composite
Decorator
Flyweight

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Describe the bridge pattern

A

Can be thought of as 2-layer abstraction

Taking interacting features and separating the functionality that is SPECIFIC to each feature from the SHARED functionality. a BRIDGE type encapsulates the shared functionality

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Code example for the bridge pattern

A

protocol Message {
var messageString: String {get set}
init(messageString: String)
func prepareMessage()
}

protocol Sender {
sendMessage(message: Message)
}

// PlainTextMessage - prepareMessage is empty
// DESEncryptedMessage - prepareMessage does the encryption

// Advanced - Using a bridge
struct MessagingBridge {
static func sendMessage(message: Message, sender: Sender) {
var mySender = sender
message.PrepareMessage()
mySender.message = message
mySender.verifyMessage()
mySender.sendMessage()
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Describe the facade pattern

A

Hide the complexity of the API behind a simple interface

CDP example - cameraCapture (CaptureCheck: sides = 2, source = camera, orient = landscape)
CaptureReceipt(sides = 1, source = camera OR photo roll, orient = portrait)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Describe the proxy pattern

A

There is one type acting as an interface for another type or API

Usually one of two uses:
layer of abstraction between a single API and my code

Need API changes but don’t have the code or there are dependencies on the API elsewhere in the code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Code example for the proxy design pattern

A

struct HouseProxy {
var house= House()
mutating func addStory(floorPlan: FloorPlan) -> Bool {
if house.stories.count <= 2 {
house.addStory(floorPlan: floorplan)
return true
} else {
return false
}
}
}

var ourHouse = HouseProxy()
// create floorplans for each level
ourHouse.addStory(floorPlan: basement)
ourHouse.addStory(floorPlan: firstStory)
ourHouse.addStory(floorPlan: secondStory)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Name some behavioral design patterns

A

Command
Strategy

Chain of responsibility
Iterator
Mediator
Memento
Observer
State
Visitor

17
Q

Describe the command design pattern

A

Separate the execution of a command from its invoker. When a type needs to perform ONE of SEVERAL actions

18
Q

Code example for command pattern

A

protocol Command {
func execute()
}

struct RockerSwitchLightOnCommand: Command { … }

struct RockerSwitchLightOffCommand: Command { … }

struct PullSwitchLightOnCommand: Command { … }

struct PullSwitchLightOffCommand: Command { … }

struct Light {
var LightOnCommand: Command
var lightOffCommand: Command

func turnOnLight() {
self.lightOnCommand.execute()
}

func turnOffLight() {
self.lightOffCommand.execute()
}

var on = PullswitchLightOnCommand()
var off = PullSwitchLightOffCommand()
var light = Light(lightOnCommand: on, lightOffCommand: off)
light.turnOnLight()

19
Q

Describe the strategy design pattern

A

Similar to command pattern but is intended to encapsulate algorithms.

Which also to use needs to be determined at runtime

20
Q

Describe the observer pattern in iOS

A
  • Also known as pubSub
  • iOS example is notifications (which are synchronous)
  • NotificationCenter .default.addObserver(_:selector:name:object:)
21
Q

Best way to abstract a singleton ?

A

Example - Create a protocol for Logging

Place log(0 call in protocol extension (so all classes that implement it get it).

Place call using singleton inside the protocol extension (so only 1 place to change if singleton is not longer wanted).

22
Q

Describe decorator pattern generally and then iOS-specific

A

General - add functionality to an object without modifying its code.

Swift implements this using:
* Delegates
* extensions
* object composition (ex: inputAccessoryView - KB toolbar)

23
Q

Describe flyweight pattern and how Apple uses it in iOS

A

Flyweight pattern allows us to use ONE object in many places to avoid excessive memory use.

iOS example is UIFont. For fonts with same settings, iOS shares a single instance.