Design Pattern - Factory Pattern

The factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created.

//
//  main.m
//  app1.27
//
//  Created by qhfz on 16/1/27.
//  Copyright (c) 2016年 ssdfz. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "MobileClass.h"
#import "FactoryClass.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Factory*foxconn=[Factory new];
        Mobile*mySamsung=[foxconn Produce_Phone:@"Samsung"];
        [mySamsung Show];
    }
    return 0;
}


//
//  mobile.m
//  app1.27
//
//  Created by qhfz on 16/1/27.
//  Copyright (c) 2016年 ssdfz. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "MobileClass.h"

@implementation Mobile

@synthesize Inch;
@synthesize Weight;
@synthesize Cost;
@synthesize Performance;
@synthesize Brand;

-(void)Call{
    printf("Mobile Phone is calling!...\n");
    return;
}
-(void)Gprs{
    printf("Mobile Phone is connecting GPRS!...\n");
    return;
}
-(void)Wifi{
    printf("Mobile Phone is connecting WIFI!...\n");
    return;
}
-(void)Show{
    printf("Mobile Phone's brand is ");NSLog(@"%@\n",Brand);
    return;
}

@end

//
//  MobileClassH.h
//  app1.27
//
//  Created by qhfz on 16/1/27.
//  Copyright (c) 2016年 ssdfz. All rights reserved.
//

#ifndef app1_27_MobileClassH_h
#define app1_27_MobileClassH_h


#endif

@interface Mobile : NSObject

@property float Inch;
@property float Weight;
@property int Cost;
@property int Performance;
@property NSString*Brand;

-(void)Call;
-(void)Gprs;
-(void)Wifi;
-(void)Show;

@end

//
//  SanxingClass.m
//  app1.27
//
//  Created by qhfz on 16/1/27.
//  Copyright (c) 2016年 ssdfz. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "SamsungClass.h"

@implementation Samsung
@synthesize Color;

-(void)Wifi{
    printf("Samsung has lower Wifi signal!\n");
    return;
}
-(void)Show{
    printf("hahahaha I have a Samsung and it's perfect!\nOtherwise,the color is \n");
    NSLog(@"%@",Color);
    return;
}

@end

//
//  SanxingClassH.h
//  app1.27
//
//  Created by qhfz on 16/1/27.
//  Copyright (c) 2016年 ssdfz. All rights reserved.
//

#ifndef app1_27_SanxingClassH_h
#define app1_27_SanxingClassH_h


#endif

#import "MobileClass.h"

@interface Samsung : Mobile

@property NSString*Color;//black white pink yellow

-(void)Wifi;
-(void)Show;

@end

//
//  FactoryClass.m
//  app1.27
//
//  Created by qhfz on 16/1/27.
//  Copyright (c) 2016年 ssdfz. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "FactoryClass.h"

@implementation Factory

@synthesize Name;

-(Mobile*)Produce_Phone:(NSString*)Brand{
    if([Brand isEqualToString:@"Samsung"]){
        Samsung*this=[Samsung new];
        this.Color=@"white";
        return this;
    }else if([Brand isEqualToString:@"Mi"]){
        Mi*this=[Mi new];
        return this;
    }return nil;
}

@end

//
//  FactoryClassH.h
//  app1.27
//
//  Created by qhfz on 16/1/27.
//  Copyright (c) 2016年 ssdfz. All rights reserved.
//

#ifndef app1_27_FactoryClassH_h
#define app1_27_FactoryClassH_h


#endif

#import "MobileClass.h"
#import "SamsungClass.h"
#import "MiClass.h"

@interface Factory : NSObject

@property NSString*Name;

-(Mobile*)Produce_Phone:(NSString*)Brand;

@end


//
//  MiClass.m
//  app1.27
//
//  Created by qhfz on 16/1/27.
//  Copyright (c) 2016年 ssdfz. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "MiClass.h"

@implementation Mi

-(void)Show{
    printf("It's Mi's round,hahahaha,it can break bricks!\n");
    return;
}

@end

//
//  MiClassH.h
//  app1.27
//
//  Created by qhfz on 16/1/27.
//  Copyright (c) 2016年 ssdfz. All rights reserved.
//

#ifndef app1_27_MiClassH_h
#define app1_27_MiClassH_h


#endif

#import "MobileClass.h"

@interface Mi : Mobile

-(void)Show;

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值