※ 引述《tester.bbs@bbs.csie.ncu.edu.tw (try or test)》之銘言:
> renderer 大大針對問題的回答大體是對的, 但沒有針對 gsj 問的為何
> 要把 code 與 data 擺在一起, 再深入比較.
> 而 high level data structure 本身就有陷井, data type 本身就隱
> 涵了處理 bit 的規則程序(code), 只是一經過 abstraction 後, 使用
> 的感覺就當 data "object" 就行了. 傳統的命令式語言也是有 OO 這
> 個成份, 就是 abstraction data type 的宣告與處理. 但更高階地利
> 用到 structured data 的各個 function procedure 則沒有被要求要
> 把相關的 function 也擺在一起. OOP 會強迫程設員這樣做, 這樣思考
> 才能進一步有更高階的 abstraction , 同時比較不會因零散而出錯.
> 讓 programmer 習而不察就是養成了好習慣.
就我使用多年 C 之後才轉戰到 C++ 的經驗是,
其實 C 的寫法一直都有 OO 中的封裝性在,
因為 gsj 針對的是封裝性部分,這裡只談封裝性,
所以這篇看起來只有講 object based programming 的部分。
在 C 的時代常看到這樣的設計方式:
/* file unit1.h */
extern int shared_var;
extern void int share_func();
/* file unit1.c */
#include "unit1.h"
static int unit1_global_var;
int shared_var;
static int unit1_local_func()
{
/* doing some operations on unit1_global_var and shared_var. */
}
void int share_func()
{
/* doing some checking and calling the unit1_local_func. */
}
/* file unit2.c */
#include "unit1.h"
static int unit2_global_var;
static int unit2_local_func()
{
/* we can use shared_var here. */
/* we can call shared_func() here. */
}
有經驗的人一看就知道 unit1.[ch] 可以在 C++ 寫成一個 class,
因為 unit1 提供了一些介面,而 unit2 只是使用這個介面:
// file unit1.h
class unit1 {
public:
unit1();
~unit1();
int shared_func();
int set_shared_var(int data);
int query_shared_var();
private:
int unit1_local_var;
int unit1_local_func();
};
// file unit1.cxx
unit1::unit1() { ... }
unit1::~unit1() { ... }
int unit1::share_func() { ... }
int unit1::set_shared_var(int data) { ... }
int unit1::query_shared_var() { ... }
其實早在 C++ 或是一些純粹的 OOPL 風行之前,
C 就已經有相當多人採用前述的寫法來設計一個很像 class 的東西,
只是過去在 C 是把一個「編譯單元」當成 class 來用,
把 static 當成 private 用,
把 extern 當成「很像」public 的東西來用,
我倒是不認為這個變換,
會有什麼 code 跟 data 綁在一起造成不對稱的問題在,
因為在我的觀念裡就很單純是用 class 整理整理 C code 而已。
另外,有一些事情沒力氣重複講,就直接把想說的放在 code 結構裡...
--
Name: Tseng, Ling-hua E-mail Address: uranus@it.muds.net
School: National Chung Cheng University
Department: Computer Science and Information Engineering
Researching: Porting GCC and Implementing VLIW instruction scheduler in GCC
Homepage: https://it.muds.net/~uranus
--
╔═══╗ ┼────────────────────────╮
║狂狷 ║ │* Origin:[ 狂 狷 年 少 ] whshs.cs.nccu.edu.tw ╰─╮
║ 年少║ ┼╮ < IP:140.119.164.16 > ╰─╮
╚╦═╦╝ ╰ * From:218-171-143-156.dynamic.hinet.net
─╨─╨─ KGBBS ─ ◎ 遨翔"BBS"的狂狷不馴;屬於年少的輕狂色彩 ◎
--
* Modify: tinlans 05/08/12 6:54:26 <218-171-143-156.dynamic.hinet.net>