Chapter 18: Access Control and Code Organization
如何透過access control來控制interface的使用。
大綱
Playground sources
Problems introduced by lack of access control
雖然balance在protocol是宣告成read-only, 但在實作時並沒有限制balance的操作,所以外部可以隨時更改balance的狀態。
Introducing access control
正確做法是要加入access identifier到balance中
目前Swift中的access modifiers
private: Accessible only to the defining type, all nested types and extensions on that type within the same source file.
fileprivate: Accessible from anywhere within the source file in which it’s defined.
internal: Accessible from anywhere within the module in which it’s defined. This is the default access level.
public: Accessible from anywhere within the module in which it is defined, as well as another software module that imports this module.
open: The same as public, with the additional ability of being able to be overridden by code in another module.
Private
CheckingAccount中有個nested type check可以讀取CheckingAccounty在private value。
Fileprivate
這樣的做法,可以避免外部可以任意產生check,但也因為宣告成private,所以也無法讀取外部class的CheckingAccount,因此改成Fileprivate,既可以避免外部使用,也可以使用外部的class的CheckingAccount。
使用時機:
The fileprivate modifier is ideal for code that is “cohesive” within a source file; that is, code that is closely related or serves enough of a common purpose to have shared but protected access. Check and CheckingAccount are examples of two cohesive types.
Internal
在Xcode中的playground的設計中,Sources目錄下的所有的檔案都算是在同一個module內,playground檔案是在另外一個module裡。

當在playground中,新增下列code, 會出現錯誤,這是因為swift中所有type的default值是 internal,只允許在同一個module內讀取,然而playground中要用Sources中的class,是屬於另外一個module,所以無法讀取。
Public
要讓CheckingAccount在不同的module下也可以使用,那就是要宣告成public
但這樣只是表示這個type是public,裡面所有member也要根據需求宣告成public才行。
Open
目前CheckingAccount和BasicAccount,都宣告成public,這樣就可以在playground直接使用,那如果要在playground中建立SavingsAccount繼承BasicAccount並overridden相關的code,則會報錯。
如果想要在不同module繼承另外一個moudle中的class,則就要宣告成open。
Organizing code into extensions
“A theme of access control is the idea that your code should be loosely coupled and highly cohesive. Loosely coupled code limits how much one entity knows about another, which in turn makes different parts of your code less dependent on others. Highly cohesive code, as you learned earlier, helps closely related code work together to fulfill a task.”
loosely coupled: 兩個不同entity盡量彼此之前越少交集越好。
highly cohesive: 相關的code進量越靠近越好。
Extensions by behavior
“An effective strategy in Swift is to organize your code into extensions by behavior”
根據不同的行為模式建構不同的extension。
private extension: 表示其中所有的member都是private。
“Putting these two methods together also connects two related, cohesive methods. It’s clear to yourself and anyone else maintaining the code that these two are cohesive and help solve a common task.”
Extensions by protocol conformance
Adding Protocol Conformance with an Extension
利用 extension 讓型別遵從 protocol 和定義 protocol 的功能
available()
用來宣告某個method即將deprecated, 改用另外一個method。
Key points
Access control modifiers are private, fileprivate, internal, public and open. The internal access level is the default.
Modifiers can be used to control your code’s visible interface and hide complexity.
private and fileprivate protect code from being accessed by code in other types or files, respectively.
public and open allow code to be accessed from another module. The open modifier additionally allows entities to be overridden by other modules.
When access modifiers are applied to extensions, all members of the extension receive that access level.
Extensions that mark protocol conformance cannot have access modifiers.
The keyword available can be used to evolve a library by deprecating APIs.
Last updated