iOS design mode-Simple factory

Source: Internet
Author: User

iOS design mode-Simple factory

Schematic diagram

Description

1. The factory class and its instantiated subclasses are not abstract classes, so it is called a simple factory (the manufacturer and the product are concrete implementation classes)

2. Simple factory flexibility is poor, but its simple and straightforward features, suitable for simple hierarchical structure of the scene

Source

Https://github.com/YouXianMing/FactoryPattern

////DeviceCreator.h//Factorypattern////Created by youxianming on 15/7/29.//Copyright (c) 2015 youxianming. All rights reserved.//#import<Foundation/Foundation.h>#import "DeviceProtocol.h"#import "iPhoneDevice.h"#import "AndroidDevice.h"#import "WindowsDevice.h"typedefenum: Nsuinteger {kandroid, Kiphone, Kwindows,} DeviceType;@interfaceDevicecreator:nsobject/** * Create phone according to tag * * @param devicetype phone tag * * @return corresponding phone*/+ (Basedevice *) Devicecreatorwithdevicetype: (DeviceType) DeviceType;@end
////DEVICECREATOR.M//Factorypattern////Created by youxianming on 15/7/29.//Copyright (c) 2015 youxianming. All rights reserved.//#import "DeviceCreator.h"#import "BaseDevice.h"@implementationDevicecreator+ (Basedevice *) Devicecreatorwithdevicetype: (DeviceType) DeviceType {if(DeviceType = =Kiphone) {                return[IphonedeviceNew]; } Else if(DeviceType = =kandroid) {            return[AndroiddeviceNew]; } Else if(DeviceType = =kwindows) {            return[WindowsdeviceNew]; } Else {            return[BasedeviceNew]; }}@end
// //   BaseDevice.h//  factorypattern////  Created by Youxianming on 15/7/29. //   Copyright (c) 2015 youxianming. All rights reserved. // #import <Foundation/Foundation.h>#import"DeviceProtocol.h " @interface Basedevice:nsobject <DeviceProtocol>@end
////BASEDEVICE.M//Factorypattern////Created by youxianming on 15/7/29.//Copyright (c) 2015 youxianming. All rights reserved.//#import "BaseDevice.h"@implementationBasedevice- (void) PhoneCall {NSLog (@"... Basedevice ...");}-(NSString *) systeminfomation {return @"Basedevice";}@end

Analysis

Specific producers and specific subclasses

Relationship Ethics Chart

iOS design mode-Simple factory

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.