What is a Singleton? A singleton is an object that only has one instance for the lifetime of the application. Some things are not safe to have more than one instance running. For example, an updater. Objective-C classes are singletons by design. You can take advantage of this when creating singletons.
Creating a Singleton https://gist.github.com/1116294
Related PatternsAbstract FactoryPlug-in RegistryNotiﬁcation Center
What is a Delegate?A delegate deﬁnes the behavior of a generic class.This keeps implementation speciﬁc behavior out of thegeneric class.Data sources are similar to delegates, except theydeﬁne content rather than behavior.UITableViews use both a delegate and a data source.
GotchasUse “assign” instead of “retain” to avoid retain cycles. When using assign, be sure to nullify the delegate reference when deallocating the object.Use “weak” instead of “assign” when using ARC.
Related PatternsData SourceBlock DelegationCallbackChain of ResponsibilityStateFlyweight
What is Block Delegation?Similar to a delegate object.Rather than give an object that has one or moremethods deﬁned, give a block that has theimplementation of one method.Rule of thumb: If you have three or more delegatemethods, use a delegate. Otherwise, use blockdelegation. Sometimes it’s appropriate to break this rule.
GotchasWatch out for retain cycles, particularly with “self” __block __unsafe_unretained id selfRef = self; __block __weak id selfRef = self;Don’t forget to copy your blocks. They are created onthe stack.
What is MVC?A strict organization of classes into their different roles. Model: Any underlying data. View: The graphical presentation of the data. Controller: Coordinates the interactions between the views and model.Keeps programs modular and makes it easy to swapout views when needed.
Model View ControllerInterface View Controller Xcode Builder Model Core Data
GotchasSome implementations of MVC have the View andModel directly connected. This can cause inﬁniteupdate cycles.
What is the Layer Pattern? Modules of a program can be organized like a stack of layers. Strict Layering: A layer can only use the layer directly below it. Non-strict Layering: A layer can use any layer below it.
Core Data Layers Application NSManagedObject Subclass NSManagedObject Context NSManagedObject NSPersistentStoreCoordinator NSManagedObjectModel NSPersistentStore XML SQLite Binary In Memory Custom
GotchasOccasionally you may ﬁnd a need to call up a layer. Todo this, you will need to use dependency inversion.Dependency inversion: A lower layer deﬁnes aninterface to interact with other layers. This is often usedwith notiﬁcations. An upper layer can register itself toreceive these notiﬁcations through the deﬁnedinterface.
What is a Façade?A façade is a single entry point to a complex collectionof classes.Makes interaction with complex collections easier andless dependent on internal implementation.This is the basis of the layer pattern.
What is an Observer?Sometimes you want to know when an objectchanges.Often used with MVC. When a model object changes, you want to update the view(s) accordingly.The observer pattern is built into every NSObject viaKey-Value Observing (KVO).
GotchasDon’t forget to remove an object as an observer whenthat object is deallocated.
Related PatternsModel View ControllerNotiﬁcation Center
What is a Proxy?A proxy receives actions in behalf of another object.Many types of proxies: Lazy loading proxy Distributed (network) proxy (NSDistantObject) Immutable proxyNSProxy can be used to make any kind of proxyobject.