iOS设计模式-工厂模式

关于工厂模式,有很多新加入的朋友不太理解这种模式的好处,觉得是增加了代码数量。这里简单讲解一下我个人看法
  • 首先,工厂模式是符合开闭原则的,其对扩展开放,对修改关闭。能够有效解耦
  • 其次,在复杂情况下能方便我们理清思路,能将共有特性于独有特性分离,这种情况下能减少代码数量,同时方便后期修改。

这里上一个简单的动物猫狗案例

  • 建议在看的时候,先自己思考一下这个例子中,这个Animal类起到了有什么作用,有没有意义。
OC版本
  • Animal.h文件
#import 

@interface Animal : NSObject
@property(nonatomic, copy)NSString * name;
@property(nonatomic, assign)NSInteger age;

- (void)printSelf;

- (void)haveFun;
@end
  • Animal.m文件
#import "Animal.h"

@implementation Animal
- (void)haveFun {
    NSLog(@"%ld岁的%@在玩耍",self.age,self.name);
}

- (void)printSelf {
    NSLog(@"%ld岁的动物%@",self.age,self.name);
}

-(void)setAge:(NSInteger)age {
    if (age >= 0) {
        _age = age;
    } else {
        _age = 0;
    }
}

- (void)setName:(NSString *)name {
    if (name == nil) {
        _name = @"";
    } else {
        _name = name;
    }
}
@end
  • Cat.h文件
#import "Animal.h"

@interface Cat : Animal
+ (instancetype)createCatWithName:(NSString *)name Age:(NSInteger)age;
@end
  • Cat.m文件
#import "Cat.h"

@implementation Cat
+ (instancetype)createCatWithName:(NSString *)name Age:(NSInteger)age {
    Cat * newCat = [[Cat alloc]init];
    newCat.name = name;
    newCat.age = age;
    return newCat;
}

- (void)printSelf {
    NSLog(@"%ld岁的小猫%@",self.age, self.name);
}
@end
  • Dog.h文件
#import "Animal.h"

@interface Dog : Animal
+ (instancetype)createDogWithName:(NSString *)name Age:(NSInteger)age;
@end
  • Dog.m文件
#import "Dog.h"

@implementation Dog
+ (instancetype)createDogWithName:(NSString *)name Age:(NSInteger)age {
    Dog * newDog = [[Dog alloc]init];
    newDog.name = name;
    newDog.age = age;
    return newDog;
}


- (void)printSelf {
    NSLog(@"%ld岁的小狗%@",self.age, self.name);
}
@end
  • 调用
Animal * dog = [Dog createDogWithName:@"小灰" Age:2];
Animal * cat = [Cat createCatWithName:@"小花" Age:1];

[dog printSelf];
[dog haveFun];
    
[cat printSelf];
[cat haveFun];

swift版本
  • Animal.swift文件
import UIKit

class Animal: NSObject {
    var name : String = ""
    var age : Int = 0 {
        didSet {
            if age < 0 {
                age = 0
            }
        }
    }
    
    func haveFun() {
        print("\(self.age)岁的\(self.name)在玩耍")
    }
    
    func printSelf() {
        print("\(age)的动物\(name)")
    }

}
  • Cat.swif文件
import UIKit

class Cat: Animal {
    static func createCat(name aName : String, age aAge : Int) -> Cat {
        let newCat = Cat.init()
        newCat.name = aName
        newCat.age = aAge
        return newCat
    }
    
    override func printSelf() {
        print("\(self.age)岁的小猫\(self.name)")
    }
    
}
  • Dog.swift文件
import UIKit

class Dog: Animal {
    static func createDog(name aName : String, age aAge : Int) -> Dog {
        let newDog = Dog.init()
        newDog.name = aName
        newDog.age = aAge
        return newDog
    }
    
    override func printSelf() {
        print("\(self.age)岁的小狗\(self.name)")
    }
}
  • 调用
let dog = Dog.createDog(name: "小灰", age: 2)
let cat = Cat.createCat(name: "小花", age: 1)
        
dog.haveFun()
dog.printSelf()
        
cat.haveFun()
cat.printSelf()
  • 上面我建议大家思考的问题不知道大家有没有答案,这里其实我的本意是要说,Animal有一个共有的方法 haveFun ,在Animal中实现后,其它子类中就不需要再实现了。
  • 这个例子可能不是实际开发中的,大家感触不深,下面举一个我实际项目中的运用实例


    iOS设计模式-工厂模式_第1张图片

    可以看到图中cell有三种样式(当然,你要说是两种样式也行,不必纠结),但其中有的共同点是都有点赞按钮和分享,并且这里点赞和分享是都可以点击的(不要问我为啥有这种奇葩设计)。
    那么 这里就可以吧cell设计一个父类,点赞按钮和分享按钮在父类中处理,其它布局就在自己单独的子类中去处理。那么,这种情况中,这种做法就可以将逻辑处理与布局分开。很好的实现了解耦。
    并且,在后期项目中,如果他要添加样式,只需要再添加一个子类就可以了,非常清晰明了。
    欢迎大家提出自己的见解,大家共同成长。

你可能感兴趣的:(iOS设计模式-工厂模式)