Abstract Factory mode

Source: Internet
Author: User

A simple factory is an entity class that contains methods to create objects directly from different parameters. Abstract Factory pattern is a further abstraction (such as a UML diagram) of the plant class on the basis of a simple factory:

  

In the project, we created two entity classes: Animal (Animal) and Plant (Plant), which follow protocol animalprotocol and Plantprotocol, respectively, subclass Dog, Fish, Bird (inherited from Animal Class) and Rose, Sakura (inherited from the plant Class), the two entity classes are instantiated by the factory class Animalfactory and Plantfactory (all inherit from the Bioligyfactory factory Class), on this basis, the factory class is further abstracted, The factory class is instantiated through the abstract factory class Abstractfactory.

Code implementation:

1.Animal, Animalfactory, biologyfactory

Animal, Animalfactory code has been created in simple Factory mode, just inherit animafactory from Biologyfactory and change the class method to an instance method.

1 //BiologyFactory.h2 #import<Foundation/Foundation.h>3 #import "Plant.h"4 #import "Animal.h"5 6 typedef ns_enum (Nsinteger, efactory) {7 Kanimalfactory,8 Kplantfactory,9 };Ten  One typedef ns_enum (Nsinteger, eplanttype) { A Krose, - Ksakura, - }; the  -typedefenum { - Kdog, - Kbird, + Kfish, - }eanimaltype; +  A @interfaceBiologyfactory:nsobject at  --(plant*) Createplantwithtype: (eplanttype) type; -  --(animal*) Createanimalwithtype: (eanimaltype) type; -  - @end

2.Plant, Plantfactory

1 //Plant.h2 #import<Foundation/Foundation.h>3 4 @protocolPlantprotocol <NSObject>5 @optional6- (void) Flower;7- (void) color;8 @end9 Ten @interfacePlant:nsobject<plantprotocol> One  A @end -  - //Rose.h the #import "Plant.h" -  - @interfacerose:plant -  +- (void) Thorn; -  + @end A  at //Sakura.h - #import "Plant.h" -  - @interfacesakura:plant -  -- (void) Enjoyflower; in  - @end
1 //PlantFactory.h2 #import "BiologyFactory.h"3 #import "Rose.h"4 #import "Sakura.h"5 6 @interfaceplantfactory:biologyfactory7 8-(plant*) Createplantwithtype: (eplanttype) type;9 Ten @end One  A //plantfactory.m - #import "PlantFactory.h" -  the @implementationplantfactory -  --(Plant *) Createplantwithtype: (eplanttype) type{ -Plant *plant =Nil; +     if(Type = =krose) { -Plant = [RoseNew]; +}Else if(Type = =Ksakura) { APlant = [SakuraNew]; at     } -     returnplant; - } -  - @end

3.AbstractFactory creation

1 //AbstractFactory.h2 #import<Foundation/Foundation.h>3 #import "BiologyFactory.h"4 #import "AnimalFactory.h"5 #import "PlantFactory.h"6 7 @interfaceAbstractfactory:nsobject8 9+ (biologyfactory*) Createfactorywithtype: (efactory) type;Ten  One @end A  - //abstractfactory.m - #import "AbstractFactory.h" the  - @implementationabstractfactory -  -+ (biologyfactory*) Createfactorywithtype: (efactory) type{ +Biologyfactory *factory =Nil; -     if(Type = =kanimalfactory) { +Factory = [AnimalfactoryNew]; A}Else if(Type = =kplantfactory) { atFactory = [PlantfactoryNew]; -     } -     returnFactory; - } -  - @end

Abstract Factory mode

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.