工厂的三种设计模式

简单工厂模式 (Simple Factory Pattern):专门定义一个类(工厂类)来负责创建其他类的实例。可以根据创建方法的参数来返回不同类的实例,被创建的实例通常都具有共同的父类。(总结来说,其实就是把一大堆的if-else判断由业务层,放到了工厂类里面)
工厂方法模式 (Factory Method Pattern)又称为工厂模式,工厂父类负责定义创建产品对象的公共接口,而工厂子类则负责生成具体的产品对象,即通过不同的工厂子类来创建不同的产品对象。
抽象工厂模式(Abstract Factory Pattern):提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类。

工厂方法模式的应用

首先不管是工厂模式还是抽象工厂模式都必须具有以下四个结构,区别在于抽象工厂模式的抽象工厂类是不止生成一种产品的,所以抽象工厂模式的抽象产品类会有多个,而工厂模式的抽象产品类只有一个

  • 抽象工厂类
  • 抽象产品类
  • 具体工厂类
  • 具体产品类

抽象工厂类

//抽象工厂
protocol BaseCellFactoryProtocol {
    func createCell(model: CellModel, tableView: UITableView, indexPath: IndexPath) -> BaseCellProtocol?//用来创建 cell
}

抽象产品类

//抽象产品
protocol BaseCellProtocol {
    func configCell(_ model:CellModel)//配置 cell
}

上面两个类即使新增 cell 的类型也是不会修改,符合开闭原则

具体工厂类

根据抽象工厂具体实现,就是根据传过来的model 的 id值确定创建哪一种 cell

class BaseCellFactory: BaseCellFactoryProtocol{

    func createCell(model: CellModel, tableView: UITableView, indexPath: IndexPath) -> BaseCellProtocol? {
        switch model.id {
        case "1":
            let cell =  tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(YellowCell.self), for: indexPath) as! YellowCell
            cell.configCell(model)
            return cell
        case "2":
            let cell =  tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(GreenCell.self), for: indexPath) as! GreenCell
            cell.configCell(model)
            return cell
        case "3":
            let cell =  tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(RedCell.self), for: indexPath) as! RedCell
            cell.configCell(model)
            return cell
        default:
            break
        }
        return nil
    }

}

具体产品类

//黄 CELL
class YellowCell: UITableViewCell,BaseCellProtocol {
    func configCell(_ model: CellModel) {
        self.textLabel?.text = model.string//传值
        self.backgroundColor = UIColor.yellow//自身特别处理 写哪里都无所谓 我就放着举个例子
    }
    
}
//绿 CELL
class GreenCell: UITableViewCell ,BaseCellProtocol{
    func configCell(_ model: CellModel) {
        textLabel?.text = model.string
        self.backgroundColor = UIColor.green

    }
}
// 红 CELL
class RedCell: UITableViewCell ,BaseCellProtocol{
    func configCell(_ model: CellModel) {
        textLabel?.text = model.string
        self.backgroundColor = UIColor.red

    }
}

调用对象类

调用就会非常清晰

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = BaseCellFactory().createCell(model: modelArr[indexPath.row], tableView: tableView, indexPath: indexPath)
        return cell as! UITableViewCell
    }

新增cell 类型也只需要修改具体工厂类 以及 新增具体产品类

抽象工厂模式的应用

其实很简单和上面差不多,我们只要新增一个抽象产品类,比如有不同 tableView头视图的需求
这里有一个iOS交流圈:891 488 181 可以来了解,分享BAT,阿里面试题、面试经验,讨论技术,裙里资料直接下载就行, 大家一起交流学习!

修改抽象工厂类

protocol BaseCellFactoryProtocol {
    func createCell(model: CellModel, tableView: UITableView, indexPath: IndexPath) -> BaseCellProtocol?
    func createCellHeadView(model: HeadModel) -> BaseCellHeadViewProtocol?
}

新增抽象产品类

//抽象产品
//抽象 cell
protocol BaseCellProtocol {
    func configCell(_ model:CellModel)
}
//抽象头视图
protocol BaseCellHeadViewProtocol {
    func configCellHeadView(_ model:HeadModel)
}

新增具体工厂方法

class BaseCellFactory: BaseCellFactoryProtocol{
    func createCellHeadView(model: HeadModel) -> BaseCellHeadViewProtocol? {
        switch model.id {
        case "1":
            
            return BlackView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 40))
        case "2":
           
            return WhiteView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 40))
        default:
            break
        }
        return nil
    }
    
    func createCell(model: CellModel, tableView: UITableView, indexPath: IndexPath) -> BaseCellProtocol? {
        switch model.id {
        case "1":
            let cell =  tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(YellowCell.self), for: indexPath) as! YellowCell
            cell.configCell(model)
            return cell
        case "2":
            let cell =  tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(GreenCell.self), for: indexPath) as! GreenCell
            cell.configCell(model)
            return cell
        case "3":
            let cell =  tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(RedCell.self), for: indexPath) as! RedCell
            cell.configCell(model)
            return cell
        default:
            break
        }
        return nil
    }

}

新增具体产品类

class BlackView: UIView,BaseCellHeadViewProtocol {
    lazy  var label:UILabel = {
        let label = UILabel(frame: self.bounds)
        return label
    }()
    
    func configCellHeadView(_ model: CellModel) {
        self.backgroundColor = UIColor.black
        self.label.text = model.string
        
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(label)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
  
}
class WhiteView: UIView ,BaseCellHeadViewProtocol{
    lazy  var label:UILabel = {
        let label = UILabel(frame: self.bounds)
        return label
    }()
    
    func configCellHeadView(_ model: CellModel) {
        self.backgroundColor = UIColor.white
        self.label.text = model.string
        
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(label)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

  
}

具体实现调用

   func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return BaseCellFactory().createCellHeadView(model: headArr[section]) as? UIView
    }

作者:Mikebanana
链接:https://juejin.cn/post/6904560073801105415/