1. 程式人生 > >軟體設計精要與模式--筆記1

軟體設計精要與模式--筆記1

一:面向物件的核心要素
封裝(Encapsulation)、繼承(Inheritance)、多型(Polymorphism)
1.封裝
封裝就是合理的隱藏和公開。
In computer science, information hiding is the principle of segregation of the 
design decisions in a computer program that are most likely to change, thus 
protecting other parts of the program from extensive modification if the design 
decision is changed. The protection involves providing a stable interface which
protects the remainder of the program from the implementation (the details that 
are most likely to change).
這是維基百科的解釋,簡單點說就是將最可能發生變化的隱藏起來,公開物件呼叫者需要
的介面。
例:員工(Employee)的年齡(Age)。顯然Age應該作為屬性,而不是作為欄位直接暴露給
呼叫者。