Re: [問題] 想請問interface的具體意義
※ 引述《jaw109.bbs@ptt.cc (潑文章都是為了養小雞)》之銘言:
> 是這樣子的,我是C++的使用著
> 在閱讀SoftwareSolutions書籍或文章的時候
> 常常會看見JAVA的interface
> 我可以理解interface存在的立場
> 但我更想知道interface更具體的使用....
> (或者,用C++的觀念去理解...... if it is possible)
其實在 OO 設計上,
abstract class 常用來當物種家族的 base class,
而 interface 則是常用來當行為 (或演算法) 家族的 base class,
因為物種本身會有預設行為,
而行為本身又有預設行為是很奇怪的一件事,
所以使用 strategy pattern 的設計,
常會看到 abstract class 對一堆 interface 有 has-a 的關係。
在解關係耦合的角度上來看,
因為 abstract class 會存在一些實作碼,
所以它可能會直接和其它 class 相依,
但 interface 本身並沒有實作碼,
所以 interface 幾乎只會被其它 class 相依,
自己本身幾乎不相依於其它 class,
最多是 method 參數型別會相依其它 class,
而這種 class 通常也是一個 interface。
至於把耦合關係解開能有什麼好處,
我想這個應該就不必我多說了。
--
Name: Tseng, Ling-hua E-mail Address: uranus@it.muds.net
School: National Tsing Hua University Department: Computer Science
Interesting: C++, Compiler, PL/PD, OS, VM, Large-scale software design
Researching: Software pipelining for VLIW architectures
Homepage: https://it.muds.net/~uranus
--
╔═══╗ ┼────────────────────────╮
║狂狷 ║ │* Origin:[ 狂 狷 年 少 ] whshs.cs.nccu.edu.tw ╰─╮
║ 年少║ ┼╮ < IP:140.119.164.252 > ╰─╮
╚╦═╦╝ ╰ * From:61-230-224-125.dynamic.hinet.net
─╨─╨─ KGBBS ─ ◎ 遨翔"BBS"的狂狷不馴;屬於年少的輕狂色彩 ◎
[修改]tinlans:61-230-224-125.dynamic.hinet.net 07/01/23 13:00:12
討論串 (同標題文章)
完整討論串 (本文為第 14 之 29 篇):