What is “Abstraction”
Abstraction refers to the process of simplifying a complex system, data, or computation into a more understandable and manageable model or concept. Through abstraction, programmers can hide unnecessary details in a system and provide a concise and easy-to-use interface for other program modules or users.
For example, when designing a library system, we can abstract the concept of “book” without considering the specific details such as size, weight, and shape. This simplifies the system design and provides a clearer interface for users to manage library resources.
Abstraction is a crucial concept in programming because it can improve code readability, reusability, and maintainability. It is also a core principle in object-oriented programming, where data and operations are separated, and an abstraction layer is provided to hide implementation details.
There are several methods to achieve abstraction:
-
Classes: Using classes can encapsulate related data and operations to form an abstract class. Users only need to know the interface and usage of this class without understanding the specific implementation details.
-
Interfaces: Interfaces define a collection of operations that are exposed to the outside. Users only need to know the names and parameters of these operations without understanding the specific implementation details. Interfaces can allow different classes to implement the same operation, achieving code reuse.
-
Abstract classes: Abstract classes are classes that cannot be instantiated. They define an interface and some concrete implementations. Users can use the interface of this abstract class without understanding the specific implementation details.
-
Delegation: Delegation is a mechanism that delegates an operation to another object for execution. This can hide implementation details and only expose necessary interfaces.
-
Generics: Generics is a technique that allows programmers to write generic code. Using generics, code that can handle different types of data can be written, achieving code reuse and abstraction.
These are some methods to achieve abstraction, and programmers can choose the most suitable method according to their actual needs.
Abstraction vs Encapsulation
Abstraction and Encapsulation are two different but related concepts in Object-Oriented Programming.
Abstraction refers to the process of representing complex real-world objects as simpler models, focusing only on the key features and functionalities of the object without considering its implementation details. This helps simplify the system and makes it easier to understand and manage.
Encapsulation, on the other hand, refers to the technique of hiding the internal details of an object and only exposing the public interface. It groups related properties and methods into a class, and separates them from other classes’ properties and methods. Encapsulation prevents external code from accessing and modifying an object’s properties and methods improperly, improving code security and reliability.
In essence, Abstraction focuses on the essence of an object, while Encapsulation focuses on hiding the external interface and internal implementation details of the object. Abstraction and Encapsulation are two fundamental concepts in OOP, and they are often used together.
Example
|
|
In the above code, the Animal class is an abstract class because it defines a speak method but does not implement it. This is because different animals have different sounds, and we want to implement this method in subclasses. The Cat and Dog classes inherit from the Animal class and implement the speak method. In this example, the Animal class abstracts a real-world concept - animals, while the Cat and Dog classes are concrete implementations.
The Animal class encapsulates its two properties - name and species. These properties are defined as instance variables and can only be accessed or modified through defined accessor methods (getter/setter). This prevents external code from directly accessing or modifying the object.
attr_reader
, attr_writer
, and attr_accessor
can be considered as one way of implementing encapsulation in Ruby. These methods are built-in shortcuts in Ruby that allow developers to easily define getter and setter methods for instance variables, while preventing direct access or modification of instance variables from external code, thus protecting the object’s internal state.
The actual effect of these methods is to define a method that allows access to an object’s internal state from outside the object without directly accessing the instance variable of the object. This hides the instance variable and provides a more concise syntax. Encapsulation is one of the important means to achieve high cohesion and low coupling in well-designed object-oriented programs, which can improve the maintainability and readability of the code.
抽象化是什麼
抽象化(abstraction)是一個將複雜的系統、資料或運算,簡化成一個更易理解、更易處理的模型或概念的過程。透過抽象化,程式設計師可以將系統中不必要的細節隱藏起來,並提供一個簡潔、易於使用的介面,以供其他程式模組或使用者使用。
舉例來說,當設計一個圖書館系統時,我們可以抽象化出“圖書”這個概念,而不必考慮圖書的具體細節,如大小、重量、形狀等。這樣一來,我們就可以簡化系統的設計,並提供一個更清晰的介面,讓使用者可以更容易地管理圖書館資源。
抽象化在程式設計中是一個非常重要的概念,因為它可以提高代碼的可讀性、可重用性和可維護性。同時,它也是面向對象程式設計中的核心原則之一,即將資料和操作分離,並提供一個抽象層,以隱藏實現細節。
-
類別:使用類別可以將相關的資料和操作封裝起來,形成一個抽象的類別。使用者只需要知道這個類別的介面和使用方法,而不必了解具體實現的細節。
-
介面:介面定義了一個對外暴露的操作集合,使用者只需要知道這些操作的名稱和參數,而不必了解具體實現的細節。介面可以讓不同的類別實現相同的操作,從而達到代碼的重用。
-
抽象類別:抽象類別是一個不能被實例化的類別,它定義了一個介面和一些具體實現。使用者可以使用這個抽象類別的介面,而不必了解具體實現的細節。
-
委託:委託是一個將一個操作轉交給另一個對象來執行的機制。這樣可以將實現細節隱藏起來,只暴露必要的介面。
-
泛型:泛型是一種可以讓程式設計師編寫出具有一般性的代碼的技術。使用泛型,可以編寫出可以處理不同類型資料的代碼,從而達到代碼的重用和抽象化。
以上是實現抽象化的一些方法,程式設計師可以根據實際需求選擇最適合的方法。
抽象化 vs 封裝
抽象化(Abstraction)和封裝(Encapsulation)是面向對象編程中兩個不同但相關的概念。
抽象化是指將複雜的現實世界對象抽象成更簡單的模型,僅關注對象的關鍵特徵和功能,而不考慮其實現細節。這樣做有助於簡化系統,使其更容易理解和管理。
封裝是指隱藏對象的內部細節,只暴露對外的接口。將相關的屬性和方法組成一個類,並將其與其他類的屬性和方法分離開來。封裝可以防止外部代碼不當訪問和修改對象的屬性和方法,提高代碼的安全性和可靠性。
簡單來說,抽象化關注對象的本質,而封裝關注對象的外部可見接口和內部實現細節的隱藏。抽象化和封裝是OOP中兩個基本的概念,它們通常是一起使用的。
範例
|
|
在上面的代碼中,Animal 類是一個抽象類,因為它定義了一個 speak 方法,但沒有實現它。這是因為不同的動物有不同的叫聲,而我們希望在子類中實現這個方法。Cat 和 Dog 類繼承了 Animal 類,實現了 speak 方法。在這個例子中,Animal 類抽象了一個現實世界的概念 - 動物,而 Cat 和 Dog 類則是具體的實現。
在 Ruby 中,可以使用實例變量(Instance Variable)和屬性訪問器(Accessor)來實現封裝
Animal 類封裝了其兩個屬性 - name 和 species。這些屬性被定義為實例變量,並且只能通過定義的屬性訪問器方法(getter/setter)來訪問或修改。
attr_reader
、attr_writer
和 attr_accessor
可以算是 Ruby 中實現封裝的方法之一。這些方法都是 Ruby 內置的快捷方式,可以讓開發者輕鬆地定義實例變數的 getter 和 setter 方法,同時避免外部程式碼直接訪問或設定實例變數,保護了物件的內部狀態。
這些方法的實際效果是定義了一個方法,使得可以從物件外部存取對象的內部狀態,而不需要直接訪問物件的實例變數。這樣可以隱藏實例變數,同時提供更簡潔的語法。在設計良好的物件導向程式中,封裝是實現高內聚低耦合的重要手段之一,能夠提高代碼的可維護性和可讀性。