SlideShare a Scribd company logo
1 of 52
Download to read offline
Acquiring Behavior
Through Inheritance
Rails新⼿手村讀書會#2015/10/05
Practical Object-Oriented Design in Ruby [Ch6]
GitHub(@azi)
書裡提到的重點
繼承是幹嘛的
繼承 Inheritance
何時使⽤用inheritance

繼承怎麼⽤用,如何寫出好的繼承架構
找出抽象
範本⽅方法(Template Method)
⽗父類別與⼦子類別的耦合(coupling)管理
depends on wish list
章節要快速帶過
所以
書裡提到的重點
真的會很快速的帶過XD

請繫好安全帶
before start
OO的三⼤大基礎
OO的三⼤大基礎
封裝 繼承 多型
OO的三⼤大基礎
封裝 繼承 多型
OO的三⼤大基礎
封裝 繼承 多型
OO的三⼤大基礎
封裝 繼承 多型
ch2 設計具有單⼀一職責的類別
OO的三⼤大基礎
封裝 繼承 多型
ch2 設計具有單⼀一職責的類別
ch3 管理依賴關係
OO的三⼤大基礎
封裝 繼承 多型
ch2 設計具有單⼀一職責的類別
ch3 管理依賴關係
ch4 建⽴立靈活的介⾯面
OO的三⼤大基礎
封裝 繼承 多型
ch2 設計具有單⼀一職責的類別
ch3 管理依賴關係
ch4 建⽴立靈活的介⾯面
ch5 Dock typing
OO的三⼤大基礎
封裝 繼承 多型
ch2 設計具有單⼀一職責的類別
ch3 管理依賴關係
ch4 建⽴立靈活的介⾯面
ch5 Dock typing
本章主題
封裝 繼承 多型
我們先來想⼀一下
繼承是要⽤用來做什麼⽤用的?
繼承的⽬目的是什麼?
繼承的⽬目的,是要達到
「程式碼再⽤用」(Code Reuse)
或「介⾯面再⽤用」。︒。
⽽而繼承的⼿手段,
就是「擴充」或「修改」。︒。
這是繼承的重點,請務必牢記。︒。
reference: http://www.jerry-tsai.com/2007/12/oo.html
繼承的⽬目的,是要達到
「程式碼再⽤用」(Code Reuse)
或「介⾯面再⽤用」。︒。
⽽而繼承的⼿手段,
就是「擴充」或「修改」。︒。
這是繼承的重點,請務必牢記。︒。
reference: http://www.jerry-tsai.com/2007/12/oo.html
定義新的⽅方法
繼承的⽬目的,是要達到
「程式碼再⽤用」(Code Reuse)
或「介⾯面再⽤用」。︒。
⽽而繼承的⼿手段,
就是「擴充」或「修改」。︒。
這是繼承的重點,請務必牢記。︒。
reference: http://www.jerry-tsai.com/2007/12/oo.html
定義新的⽅方法
針對超類別中的某⽅方法

重新定義其⾏行為
once upon a time....
FastFeet boss : 來幫
我寫程式吧!
⼩小明: 呵~呵~呵
呵~呵~呵~呵~呵~呵
就交給我吧!
沒有單⼀一職責
沒有單⼀一職責
難以維護
沒有單⼀一職責
難以維護
擴充困難
沒有單⼀一職責
難以維護
擴充困難
code review 會被退回來 (泣)
重覆的code太多
重覆的code太多
類別之間有⼀一定的關係
重覆的code太多
類別之間有⼀一定的關係
繼承
move size
依賴太多
決定處理時機
決定處理時機
提供內容
⼩小結
繼承解決了多種相關類型之間存在的問題。︒。它充許我們
將共⽤用的程式碼隔離起來。︒。並在抽象類別裡實作共同的
演算法,同時⼜又提供⼦子類別其特殊化。︒。
建⽴立抽象⽗父類別最佳辦法是:從具體的⼦子類別裡擷取程
式碼。︒。從⾄至少三個具體類別裡找出正確的抽象是最為輕
鬆的。︒。
抽象⽗父類別可以使⽤用範本⽅方法讓⼦子類別提供特殊化,並
可以使⽤用hook⽅方法降低層次結構中的耦合度。︒。
End
<<gitclone https://github.com/square/maximum-
awesome.git
<<cdmaximum-awesome
<<rake
<<importsolarizedcolorscheme(fileinmaximum-awesome
foldertoo)toiTerm2
<<open.vimrc add setbackground=dark
vim install

More Related Content

Similar to Ch06 inheritance

[DCTPE2010] 從企劃到開發維護的 Drupal 專案經驗
[DCTPE2010] 從企劃到開發維護的 Drupal 專案經驗[DCTPE2010] 從企劃到開發維護的 Drupal 專案經驗
[DCTPE2010] 從企劃到開發維護的 Drupal 專案經驗
Drupal Taiwan
 

Similar to Ch06 inheritance (20)

常用Js框架比较
常用Js框架比较常用Js框架比较
常用Js框架比较
 
语义互操作与关联数据
语义互操作与关联数据语义互操作与关联数据
语义互操作与关联数据
 
「所知有雲」:維基百科雲端運算中文條目編輯行動發起說明
「所知有雲」:維基百科雲端運算中文條目編輯行動發起說明「所知有雲」:維基百科雲端運算中文條目編輯行動發起說明
「所知有雲」:維基百科雲端運算中文條目編輯行動發起說明
 
20230830 淺談 Azure OpenAI.pdf
20230830 淺談 Azure OpenAI.pdf20230830 淺談 Azure OpenAI.pdf
20230830 淺談 Azure OpenAI.pdf
 
Azure Taiwan - Keep azure cost down (Azure 成本管控)
Azure Taiwan - Keep azure cost down (Azure 成本管控)Azure Taiwan - Keep azure cost down (Azure 成本管控)
Azure Taiwan - Keep azure cost down (Azure 成本管控)
 
My DevOps Tour 0.1
My DevOps Tour 0.1My DevOps Tour 0.1
My DevOps Tour 0.1
 
Modern Web Development Introduction
Modern Web Development IntroductionModern Web Development Introduction
Modern Web Development Introduction
 
Artifacts management with CI and CD
Artifacts management with CI and CDArtifacts management with CI and CD
Artifacts management with CI and CD
 
twMVC#42 讓我們用一種方式來開發吧
twMVC#42 讓我們用一種方式來開發吧twMVC#42 讓我們用一種方式來開發吧
twMVC#42 讓我們用一種方式來開發吧
 
從 GitHub Copilot 到 Enterprise Copilot:打造符合企業需求的智能開發助手之路 | .NET Conf 2023 Taiwan
從 GitHub Copilot 到 Enterprise Copilot:打造符合企業需求的智能開發助手之路 | .NET Conf 2023 Taiwan從 GitHub Copilot 到 Enterprise Copilot:打造符合企業需求的智能開發助手之路 | .NET Conf 2023 Taiwan
從 GitHub Copilot 到 Enterprise Copilot:打造符合企業需求的智能開發助手之路 | .NET Conf 2023 Taiwan
 
Build the Blockchain as service (BaaS) Using Ethereum on Kubernetes
Build the Blockchain as service (BaaS) Using Ethereum on KubernetesBuild the Blockchain as service (BaaS) Using Ethereum on Kubernetes
Build the Blockchain as service (BaaS) Using Ethereum on Kubernetes
 
Build your own kubernetes apiserver and resource type
Build your own kubernetes apiserver and resource typeBuild your own kubernetes apiserver and resource type
Build your own kubernetes apiserver and resource type
 
使用 Dependency Injection 撰寫簡潔 C# 程式碼原來這麼簡單 (.NET Conf 2018)
使用 Dependency Injection 撰寫簡潔 C# 程式碼原來這麼簡單 (.NET Conf 2018)使用 Dependency Injection 撰寫簡潔 C# 程式碼原來這麼簡單 (.NET Conf 2018)
使用 Dependency Injection 撰寫簡潔 C# 程式碼原來這麼簡單 (.NET Conf 2018)
 
Introduction Hugging face.pdf
Introduction Hugging face.pdfIntroduction Hugging face.pdf
Introduction Hugging face.pdf
 
Open source culture_in_china_team
Open source culture_in_china_teamOpen source culture_in_china_team
Open source culture_in_china_team
 
[DCTPE2010] 從企劃到開發維護的 Drupal 專案經驗
[DCTPE2010] 從企劃到開發維護的 Drupal 專案經驗[DCTPE2010] 從企劃到開發維護的 Drupal 專案經驗
[DCTPE2010] 從企劃到開發維護的 Drupal 專案經驗
 
资源整合与Web2.0
资源整合与Web2.0资源整合与Web2.0
资源整合与Web2.0
 
Kevenjiaoda
KevenjiaodaKevenjiaoda
Kevenjiaoda
 
资源整合与Web2.0
资源整合与Web2.0资源整合与Web2.0
资源整合与Web2.0
 
Bootstrap4 與他的好搭檔
Bootstrap4 與他的好搭檔Bootstrap4 與他的好搭檔
Bootstrap4 與他的好搭檔
 

Ch06 inheritance