Design Patterns by Tutorials Flashcards
Notification message flow
What are design pattens?
* Design patterns aren’t concrete implementations, but rather, they are a starting point for writing code.
* Design patterns collectively form a set of best practices to help you write more understandable and easier-to-maintain code.
UML
Unified Modeling Language
“inherits from,” and it is read as “is a” in UML terms
indicate a property, which is known as an /association/in UML terms.
“has a.”
“Conforms to” or “Implements protocol”
Indicate “uses,” which is called a “dependency” in UML terms
* A weak property or delegate.
* An object that’s passed into a method as a parameter, but not held as a property.
* A loose coupling or callback, such as an IBAction from a view to a controller.
How you’d indicate that Dog delegates to a PetOwning object
How to indicate parameters and methods in UML?
MVC diagram
Controllers are allowed to have strong properties for their model and view so they can
be accessed directly. Controllers may have more than one model and/or view.
Conversely, models and views should nothold a strong reference to their owning controller. This would cause a retain cycle.
Models in MVC
Models hold onto application data. They are usually structs or simple classes.
Views in MVC
Views display visual elements and controls on screen. They are usually subclasses of UIView.
Controllers in MVC
Controllers coordinate between models and views. They are usually subclasses of UIViewController.
What references views and models should have in MVC?
Models and views should not hold a strong reference to their owning controller. This would cause a retain cycle.
Views may have a weak reference to their owning controller through a delegate. For example, a UITableView may hold a weak reference to its owning view controller for its delegate and/or dataSource references. However, the table view doesn’t /know/these are set to its owning controller - they just happen to be.
What references VC have in MVC?
Controllers are allowed to have strong properties for their model and view so they can
be accessed directly. Controllers may have more than one model and/or view.
What should you be careful about in MVC?
have a lot of logic in the controllers
Not every object will neatly fit into the category of model, view or controller.
What level asses control you should choose?
you’ll use public for types, properties and methods that should be publicly accessible to other classes;
you’ll use private if something should only be accessible to the type itself
you’ll use internal if it should be accessible to subclasses or related classes but isn’t intended for general use otherwise
How correctly set main view of view controller while you keep this view and outlets somewhere else?
public var mainView: MainView! {
guard isViewLoaded else { return nil }
return (view as! MainView)
}
You keep outlets in MainView class that inherit from UIView
Show delegation pattern diagram and explain each part of diagram
The delegation pattern has three parts: an object needing a delegate, a delegate protocol and a delegate.
An object needing a delegate, also known as the delegating object.It’s the object that /has/a delegate. The delegate is usually held as a weak property to avoid a retain cycle where the delegating object retains the delegate, which retains the delegating object.
A delegate protocol, which defines the methods a delegate may or should implement.
A delegate, which is the helper object that implements the delegate protocol.
When should you use a delegation pattern?
Use this pattern to break up large classes or create generic, reusable components.
The delegation pattern allows an object to use a helper object to perform a task, instead of doing the task itself.
This allows for code reuse through object composition, instead of inheritance.
What DataSource- and Delegate-named objects delegate pattern follow?
They follow the delegation pattern, as each involves one object asking another to provide data or do something.
Apple frameworks commonly use the term DataSource for what?
to group delegate methods that /provide/data. For example, UITableViewDataSource is expected to provide UITableViewCells to display.
if dataSource and delegate to be set to the /same/ object?
they don’t have to be, and it can be very beneficial at times to have them set to different objects.
The common convention in iOS to set delegate objects?
/after/an object is created
It’s common convention in delegate pattern
It’s common convention to pass the delegating object.
This way, the delegate can use or inspect the caller if needed.
What should you be careful about in delegation pattern?
they can be overused. Be careful about creating /too many/delegates for an object.
If an object needs several delegates, this may be an indicator that it’s doing too much. Consider breaking up the object’s functionality for specific use cases, instead of one catch-all class.
f you find yourself constantly switching between classes to understand what’s happening, then that’s a sign you have too many. Similarly, if you cannot understand why a certain delegate is useful, then that’s a sign it’s too small, and you’ve split things up too much.
be careful about creating retain cycles - Delegates should be weak properties in the vast majority of use cases
What delegation pattern allows you to do?
allows you to break up large classes and create generic, reusable components.
Strategy Pattern diagram and explain each part
The object using a strategy*. This is most often a view controller when the pattern is used in iOS app development, but it can technically be any kind of object that needs interchangeable behavior.
The strategy protocol*defines methods that every strategy must implement.
The strategies*are objects that conform to the strategy protocol.
What strategy patten defines?
The strategy pattern defines a family of interchangeable objects.
What strategy pattern makes to app?
Makes apps more flexible and adaptable to changes at runtime, instead of requiring compile-time changes.
When should you use strategy pattern?
Use the strategy pattern when you have two or more different behaviors that are interchangeable.
This pattern is similar to the delegation pattern: both patterns rely on a protocol instead of concrete objects for increased flexibility. Consequently, /any/object that implements the strategy protocol can be used as a strategy at runtime.
Unlike delegation, the strategy pattern uses a /family/of objects.
Delegates are often fixed at runtime. For example, the dataSource and delegate for a UITableView can be set from Interface Builder, and it’s rare for these to change during runtime.
Strategies, however, are intended to be easily interchangeable at runtime.
Strategy vs Delegation pattern?
Delegates are often fixed at runtime. For example, the dataSource and delegate for a UITableView can be set from Interface Builder, and it’s rare for these to change during runtime.
Strategies, however, are intended to be easily interchangeable at runtime.
This pattern is similar to the delegation pattern: both patterns rely on a protocol instead of concrete objects for increased flexibility. Consequently, /any/object that implements the strategy protocol can be used as a strategy at runtime.
Unlike delegation, the strategy pattern uses a /family/of objects.
Both patterns use a protocol for flexibility. Unlike the delegation pattern, however, strategies are /meant/to be switched at runtime, whereas delegates are usually fixed.
What should you be careful about using strategy pattern?
Be careful about overusing this pattern. In particular, if a behavior /won’t/ever change, it’s okay to put this directly within the consuming view controller or object context. The trick to this pattern is knowing /when/to pull out behaviors, and it’s okay to do this lazily as you determine where it’s needed.
What strategy pattern defines?
defines a family of interchangeable objects that can be set or switched at runtime.
Singleton vs Singleton Plus
The singleton pattern restricts a class to have only /one/ instance.
The “singleton plus” pattern is also common, which provides a “shared” singleton instance, but it also allows other instances to be created too.
\* The singleton pattern restricts a class to only one instance. \* The singleton plus pattern provides a "default" shared instance but also allows other instances to be created too.
When should you use singleton pattern?
Use the singleton pattern when having more than one instance of a class would cause problems, or when it just wouldn’t be logical.
What should you be careful about when using singleton pattern?
A very most common reason why singletons are problematic is testing. If you have state being stored in a global object like a singleton then order of tests can matter, and it can be painful to mock them. Both of these reasons make testing a pain.
Lastly, beware of “code smell” indicating your use case isn’t appropriate as a singleton at all. For example, if you often need many custom instances, your use case may be better as a regular object.
Be careful about overusing this pattern! Before you create a singleton, consider other ways to solve the problem without it. If a singleton really is best, prefer to use a singleton plus over a singleton.
If you encounter a situation where you’re tempted to use a singleton,
first consider other ways to accomplish your task. Instead, consider passing models via an initializer or property.
If you determine you actually /do/need a singleton, consider whether a singleton plus makes more sense.
When you decided to use singleton - what to ask yourself and what they determine?
Will having more than one instance cause problems? Will it ever be useful to have custom instances? Your answers will determine whether its better for you to use a true singleton or singleton plus
Memento pattern diagram and explaining
and explaining
- The originator is the object to be saved or restored.
- The memento represents a stored state.
- The caretaker requests a save from the originator and receives a memento in response. The caretaker is responsible for persisting the memento and, later on, providing the memento back to the originator to restore the originator’s state.
The originator is the object to be saved; the memento is a saved state; and the caretaker handles, persists and retrieves mementos.
When should you use memento pattern?
Use the memento pattern whenever you want to save and later restore an object’s state.
What should you be careful about when using memento pattern?
Be careful when adding or removing Codable properties: both encoding and decoding can throw an error. If you force unwrap these calls using try! and you’re missing any required data, your app will crash!
To mitigate this problem, avoid using try! unless you’re /absolutely/ sure the operation will succeed. You should also plan ahead when changing your models.
What iOS provides for memento pattern?
iOS provides Encoder for encoding a memento to, and Decoder for decoding from, a memento. This allows encoding and decoding logic to be used across originators.