Objective-c design mode-factory method mode virtual constructor (object creation)

Source: Internet
Author: User

Factory method Mode

Factory method mode controls the creation of objects, masks the details of object creation, and directly creates the objects we need to be configured.

The factory method pattern defines the interface for creating methods, letting subclasses decide which class to instantiate, and the factory method pattern to defer instantiation of a class to its subclasses.

Factory method Factory is actually a lot of a classic application, and its production depends on what factory to use, conform to the open and closed principle of object-oriented design, add the product only need to add new classes, and do not need to modify the original code.

Usage Scenarios

1. The class of the object to be created cannot be accurately expected at compile time;

2. Classes want their subclasses to decide what to transcend at runtime;

3. The class has several helper classes for its subclasses, and you want to return that subclass of information localized.

For the simplest example, NSNumber this class, you can use [[NSNumber alloc] init] To create an instance, but it's not useful unless you use a predefined factory method.

For example, the [NSNumber numberwithbool:yes] message Gets an instance of the NSNumber subclass Nscfboolean.

Security methods for creating objects

Because the client does not need to create the implementation details of the object, the details are implemented in the factory method, and the final product is returned, and if you want to add a new product, you do not need to modify the client interface, just modify the factory method.

Object factory methods and class factory methods

Personally think that the class factory method is actually a simple factory embodiment, only need to call different methods or pass different parameters to instantiate different subclasses. But this method adds the corresponding method to the abstract class each time the new product is added, and does not conform to the open closure principle, and the object factory method pattern only needs to add new classes to achieve the desired effect.

Demo

More content:

Factory class


#import<Foundation/Foundation.h>@interfaceFactory:nsobject-(ID) createproduct;@end#import "Factory.h"#import "Product.h"@implementationFactory-(ID) createproduct{return[Productclass]New];}@end

#import "Factory.h"@interfacefactorytype1:factory@end#import "FactoryType1.h"#import "ProductType1.h"@implementationFactoryType1-(ID) createproduct{return[[ProductType1class]New];}@end#import "Factory.h"@interfacefactorytype2:factory@end#import "FactoryType2.h"#import "ProductType2.h"@implementationFactoryType2-(ID) createproduct{return[[ProductType2class]New];}@end

Product Category

#import<Foundation/Foundation.h>@interfaceProduct:nsobject@property (nonatomic, strong) NSString*name;@end#import "Product.h"@implementationProduct-(ID) init{if(self) {_name=@"Product"; }    returnSelf ;}@end#import "Product.h"@interfaceproducttype1:product@end#import "ProductType1.h"@implementationProductType1-(ID) init{if(self) {self.name=@"ProductType1"; }    returnSelf ;}@end#import "Product.h"@interfaceproducttype2:product@end#import "ProductType2.h"@implementationProductType2-(ID) init{if(self) {self.name=@"productType2"; }    returnSelf ;}@end

Client:

Factory *factory1 = [FactoryType1New]; Product*product1 =[Factory1 createproduct]; NSLog (@"%@", Product1.name); Factory*factory2 = [FactoryType2New]; Product*product2 =[Factory2 createproduct]; NSLog (@"%@", Product2.name); return 0; /**2015-07-19 22:11:13.590 Factory methods[32054:8351400] productType1 *2015-07-19 22:11:13.591 Factory METHODS[32054:8351400] ProductType2*/

As you can see, printing results, we only change the initialization subclass, corresponding to produce a different product.

For class factory methods do not do a demonstration, you can see NSNumber

Objective-c design mode-factory method mode virtual constructor (object creation)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.