Factory and Class Cluster

20
Study Group Factory and Class Cluster 2014.07.29 joehsieh

Transcript of Factory and Class Cluster

Study Group Factory and Class Cluster

2014.07.29 joehsieh

Outline

• Simple Factory, Factory and Abstract Factory

• Class Cluster

• Associated Object

Simple Factory, Factory and Abstract Factory(2/2)

• http://www.codeproject.com/Articles/716413/Factory-Method-Pattern-vs-Abstract-Factory-Pattern

Simple Factory

Factory

Abstract Factory

Simple Factory, Factory and Abstract Factory(2/1)

• The intent of Simple Factory

• Simplifies the process of creating object.(It's not a formal pattern)

• The intent of Factory

• Define an interface for creating an object, but let subclasses decide which class to instantiate.

• The intent of Abstract Factory

• Provide an interface for creating families of related or dependent objects without specifying their concrete classes

So...What is Factory?

• Factory encapsulates the process of creating production.(Consumer knows Factory only.)

What is Class Cluster?

• A class cluster is an architecture that groups a number of private, concrete subclasses under a public, abstract superclass.

• Simple Factory + Abstract Factory

Design of NSNumberAt first

At last…

Consumer knows too much strangers!

Better Design of NSNumber

• [NSNumber numberWithChar:’a’];

• [NSNumber numberWithInt:1];

• [NSNumber numberWithFloat:1.0];

• [NSNumber numberWithDouble:1.0];

!

Better way!

Trade-off• Simplicity:Consumers only knows abstract

class.It does not care implementation under the hood.

• Extensibility:Because of the implementation of abstract class is under the hood, we are hard to extend.(Note:Prefer composition over inheritance)

Extend Abstract Class(1/2)

• An abstract class is a class which is not fully functional on its own. It must be subclassed, and the subclass must fill out the missing functionality.In other words, we must implement part interface of abstract class.

Extend Abstract Class(2/2)

• Be a subclass of the cluster’s abstract superclass

• Declare its own storage

• Override all initializer methods of the superclass

• Override the superclass’s primitive methods

How to extend Class Cluster?

• Avoids to extend class cluster

• Extends the functionalities by category

• Prefer composition over inheritance

• Applies the essential information, implements the abstract function and references documents.

(Demo) Customizes array

Associated Object• Associated object can attach property to

existing classes like a dictionary.You do not have to extend an existing class to obtain properties.

• objc_setAssociatedObject

• objc_getAssociatedObject

• objc_removeAssociatedObject

Object Association Types

(Demo) Adds properties by category

and associated object

Reference• https://mikeash.com/pyblog/friday-qa-2010-03-12-

subclassing-class-clusters.html

• https://developer.apple.com/library/ios/documentation/general/conceptual/CocoaEncyclopedia/ClassClusters/ClassClusters.html

• http://nshipster.com/associated-objects/

• http://www.newgenapps.com/blog/bid/184670/Creational-Design-Patterns-in-Cocoa-Touch-Framework

• http://corey.quickshiftconsulting.com/blog/first-post