2008/02/10

Programming Paradigms

重新拿出 The C++ Programming Language 來看 (C++ 的爸爸, Bjarne Stroustrup, 寫的),覺得 Programming Paradigms 這一節寫得真好。

首先,他強調 支援 (support) 和 允許 (enable) 是不相同的:
A language is said to support a style of programming if it provides facilities that make it convenient (reasonably easy, safe, and efficient) to use that style. A language does not support a technique if it takes exceptional effort or skill to write such programs; it merely enables the technique to be used.
在接下來的 programming styles 介紹當中,我們可以更了解這兩個概念的不同之處。

Procedural Programming

這是最早期的程式設計方法,它的法則是:
Decide which procedures you want;
use the best algorithms you can find.
也就是說,先想好一件工作需要由哪些 procedures 完成;再用最好的演算法完成這些 procedures。Fortran 算是 support 這種程式設計方式的始祖,C 也 support 這種設計方法。

Modular Programming

隨著程式規模愈來愈大,程式設計開始重視如何組織資料。將資料和處理這些資料的 procedures 結合在一起,稱之為 module。這種程式設計方法的法則是:
Decide which modules you want;
partition the program so that data is hidden within modules.
也就是說,以資料為中心,將相關的資料和 procedures 結合成各自獨立的 module。Module 和 module 之間,資料是隱藏起來的,所以此種法則也叫做 data-hiding principle。Modula-2 直接 support 這種程式設計方式,但 C 只能算是 enable 這種方法。

Data Abstraction

在某些應用場合,data hiding 這個觀念仍然不夠理想,必須導入 abstract data types 的觀念 (也就是 user-defined types)。這種程式設計方法的法則是:
Decide which types you want;
provide a full set of operations for each type.
也就是說,先想好有哪些資料型態要操作,然後為每一個資料型態設計完整的操作程序。

Object-Oriented Programming

對於類似圖形使用者介面的系統,data abstraction 缺乏彈性和擴充能力。這時,就需要導入 object-oriented 這種可以繼承類別的觀念。這種程式設計方法的法則是:
Decide which classes you want;
provide a full set of operations for each class;
make commonality explicit by using inheritance.
也就是說,先想好有哪些類別要操作,然後為每一個類別設計完整的操作程序;類別和類別之間的共通特性要以繼承的方式來處理。

Generic Programming

這種程式設計方法的法則是:
Decide which algorithms you want;
parameterize them so that they work for
a variety of suitable types and data structures.
這一部份,我還要多多努力學習才行。

沒有留言: