如何利用Swift访问控制符来处理工厂模式的创建方法

版权声明:大熊猫猪·侯佩原创或翻译作品.谢绝转载! hopy https://blog.csdn.net/mydo/article/details/81623902

我们知道在Swift中提供了5种访问控制符,合理的使用它们可以为我们打造更完美的代码生态系统。

现举一例。

通过工厂模式创建Counter对象,Counter只是一个工厂类,实体类是其两个子类:

  • TimeCounter
  • NumberCounter

Counter类通过实际条件决定创建哪个实体类:

enum CounterType{
    case time
    case number
}

Counter工厂方法为:

static func createWith(type:CounterType)->Counter{
    let counter:Counter
    if type == .time{
        counter = TimeCounter.createInternal()
    }else{
        counter = NumberCounter.createInternal()
    }
}

注意Counter、TimeCounter和NumberCounter类分别放在3个独立的swift代码文件中。

以上这么做的一个问题是TimeCounter和NumberCounter中的createInternal()方法必须设为公有,否则Counter无法访问。但这样一来,其他外部代码可以不通过Counter的create接口,而是直接通过TimeCounter和NumberCounter的createInternal()方法去分别创建对应的实例对象。

这不是我们想要的!

我们希望用户只能通过Counter去创建实体对象。如何解决呢?

一种方法是把Counter、TimeCounter和NumberCounter放在动态库中,将Counter的create()方法设置为public,这样外部只有通过这个public方法来创建对象,而动态库内部可以自由访问其中的所有internal方法。

但如果以上3个类不在库中,而是直接存放在App项目中呢?

办法是利用访问控制符来细化访问控制。具体如下:

  1. 在Counter.swift中分别添加TimeCounter和NumberCounter类的扩展
  2. 将createInternal()方法从原先的TimeCounter和NumberCounter类中移至扩展中
  3. 为createInternal()方法增加fileprivate访问控制符

这样一来,我们可以按原先那样用Counter工厂类创建实体对象,但是外部代码却无法通过实体类去创建对象,这就达到了代码封装的要求。

猜你喜欢

转载自blog.csdn.net/mydo/article/details/81623902
今日推荐