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
















