70 likes | 267 Views
Orphans, Adoption, and Surrogates Singletons. Josh Mason June 1, 2009. Adoption and Orphan. Adoption Taken ownership of object Orphan Relinquish object What’s the purpose? Deciding where to put adoption/orphan functions Component? Leaf? Composite? Case by case basis. Singleton.
E N D
Orphans, Adoption, and SurrogatesSingletons Josh Mason June 1, 2009
Adoption and Orphan • Adoption • Taken ownership of object • Orphan • Relinquish object • What’s the purpose? • Deciding where to put adoption/orphan functions • Component? Leaf? Composite? • Case by case basis.
Singleton • Creational Pattern • Restricts instantiation of a class to a single instance
Uses • To implement other patterns • Abstract Factory • Builder • Prototype • Façade • State Objects
Singleton - Benefits • Single Instance • Controlled Access • Reduced Namespace • Lazy Initialization • Easily extends to allow for a variable number of instances. • Can assist in making thread safety simpler.
Singleton - Examples • Traditional • staticMyClass* Instance(); • Alternative 1 • staticMyClass* Instance(inti_id); • static MyClass* Cleanup(inti_id); • Alternative 2 • staticCreate(inti_val, float i_val2); • staticMyClass* Instance(); • staticCleanup();
Stinkleton* - Issues • Why not just use global variable? • An anti-pattern? • Creation • Cleanup • Tight coupling * Robert Penner