Objective-c design Pattern-Abstract Factory mode abstraction Factory (object creation)

Source: Internet
Author: User

Abstract Factory mode

Understand the factory method pattern, in fact, there are many similarities between abstract factory and factory method pattern. Abstract factories are also the separation of client object creation and logic code, but abstract factories often produce a set of data rather than just produce a product.

Abstract factories provide a class that creates a series of related or interdependent object interfaces without having to develop their specific classes.

Abstract Factory and Factory methods

Abstract Factory

Creating abstract products from object combinations

Create multi-Series products

The interface of the parent class must be modified to support the new product

Factory method

Creating abstract products with class inheritance

Create a product

Child Tear creator and Reload factory method to create new product

Demo

More than a few of the paste code, cut-type diagram

Client code:

#import<Foundation/Foundation.h>#import "Factory.h"#import "ProductType1.h"#import "ProductType2.h"#import "ConcreteFactory1.h"#import "ConcreteFactory2.h"intMainintargcConst Char*argv[]) {@autoreleasepool {Factory*factory = [ConcreteFactory1New]; ProductType1*PT1 =[Factory createProductType1]; ProductType2*pt2 =[Factory createProductType2]; NSLog (@"%@\n%@", Pt1.name,pt2.name); Factory*factory2 = [ConcreteFactory2New]; ProductType1*pt11 =[Factory2 CreateProductType1]; ProductType2*pt12 =[Factory2 createProductType2]; NSLog (@"%@\n%@", Pt11.name,pt12.name); }    return 0;}

Run results

---£º04.277 Abstractfactory[35654: 8798008 ] PRODUCTTYPE1FAPRODUCTTYPE2FA--£ º 04.278 Abstract factory[35654:8798008] PRODUCTTYPE1FBPRODUCTTYPE2FB

As you can see, each factory produces a set of products.

It is also obvious to see the shortcomings of the abstract factory, whenever we need to add new products, we need to add products corresponding to the class, but also need to modify each factory to add a method for its creation.

Objective-c design mode-Abstract Factory mode abstraction Factory (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.