--- tags: software design, UML, Design Pattern --- # Software Design [TextBook](https://drive.google.com/file/d/1W07EnhhhnRbMxEQIFsP5iQSUzOoAoNfc/view?usp=sharing) ## OOP Concept [物件導向(Object Oriented Programming)概念](https://totoroliu.medium.com/%E7%89%A9%E4%BB%B6%E5%B0%8E%E5%90%91-object-oriented-programming-%E6%A6%82%E5%BF%B5-5f205d437fd6) * 類別 (class) * 定義一件事物的抽象特點 * 包含屬性、資料的操作 * 物件 (object) * 類別的實例 * 封裝 (encapsulation) * 將物件內部的資訊隱藏起來 * 其他物件只需知道該物件的外在即可 * 繼承 (inheritance) * 子類別通常會比父類別更為具體化 * 多型 (polymorphism) * 多載 * 指說在相同類別中,定義名稱相同,但是參數個數不同,或是參數型態不同的函式 * Signature same but parameter and implement varies * 覆寫 * 指覆寫掉父類別中的函式。例如:動物類別(父類別) getLegs()方法被鳥類別(子類別)覆蓋。 * Signature and parameter same, implements varies --- * Primitive type: single piece of data * call by value * Class type: multiple pieces of data aka methods * call by reference * class 裡的變數會被改變,因為是找該遍數的地址。 ```java= public class Duck{ public boolean canfly = false;// attribute (instance variable) public void quack(){ // method signature System.out.println('quack');// method body } } ``` --- * When two or more methods in the same class have the same name but different parameters, it's called Overloading. When the method signature (name and parameters) are the same in the superclass and the child class, it's called Overriding. * Overloading * 相同的函數名字,不同的參數 * 雖然名字相同,但是只要參數不同就是 different signatures * signature consists of the name of a method together with its parameter list * Overriding * 相同 signature,名字跟參數都相同。 --- [物件導向中的介面與抽象類別是什麼](https://medium.com/%E7%A8%8B%E5%BC%8F%E6%84%9B%E5%A5%BD%E8%80%85/%E7%89%A9%E4%BB%B6%E5%B0%8E%E5%90%91%E4%B8%AD%E7%9A%84%E4%BB%8B%E9%9D%A2%E8%88%87%E6%8A%BD%E8%B1%A1%E9%A1%9E%E5%88%A5%E6%98%AF%E4%BB%80%E9%BA%BC-1199804ccc5f) ![](https://i.imgur.com/ceKwWco.png) * Abstract class * It cannot be private * 只有 signature 沒有 implement * A class that has at least one abstract method is called an abstract class * An abstract class can have any number of abstract and or fully defined methods (abs class 可以有一個以上的 abs method 以及任意數量的 defined methods) * A class that has no abstract methods is called a concrete class (沒有任何 abs method 的 class 就是 concrete class) * Interface * It contains method headings and constant definitions only * 只有未實作的 method 還有固定的常數 * Any variables defined in an interface must be public, static, and final --- [Java 多型 後連結(late binding)與前連結(early binding) 向上轉型(upcast)](https://www.itread01.com/content/1546933896.html) * Early binding * which method is to be called is decided at compile-time * Overloading: an invocation can be operated on arguments of more than one type ```java= public class Test{ public String hello(String name){ return 'Hello' + name; } /*Overloading*/ public String hello(String name, String gender){ retrun 'Hello' + name + gender; } } public static void main(String[]args){ Test test = new Test(); System.out.println(test.hello('test_name')); System.out.println(test.hello('test_name', 'male')); //decide at compile time //early binding } ``` * Late Binding aka Dynamic Binding * which method is to be called is decided at run-time * Overriding: a derived class inherits methods from the base class, it can change or override an inherited method * While running to bind the method accroding to the class type ```java= public class Payment{ public void pay(){ System.out.println('Pay in cash'); } } public class CreditCardPayment extends Payment{ public void pay(){ System.out.println('Pay in credit card'); } //Overriding } public class store{ Payment Cash = new Payment(); CreditCardPayment CreditCard = new CreditCardPayment(); Cash.pay(); CreditCard.pay(); //While compiling, the complier does not know which method to bind //While running the PC knows the pay method has been overridden } ``` --- * Upcasting * 往上指派,新增子類別指派給父類別的變數 * 合法 * 子類別必定有父類別的特性 * Downcasting * 往下指派,新增父類別指派給子類別的變數 * 不一定合法 * 父類別不一定有子類別的特性 ## UML Diagram Protocol [UML 意思](http://ashkandi.herokuapp.com/blog/2015/09/14/uml-notes-01/) [UML 關係](https://www.gushiciku.cn/pl/abmv/zh-tw) ### Relationship #### Dependency * Weakest relationship between classes * Use-a * 箭頭指向被依賴的 class ![](https://i.imgur.com/zDcPaA1.png) #### Association * Retain relationship in a period of time * Has-a * 透過持有關聯者的成員變數來實現這個關係 ![](https://i.imgur.com/HkrrJsN.png) #### Aggregation * Implies ownership * 整體以及部份的關係,成員物件是整體物件的一部份,但是**成員物件能夠脫離整體物件存在**。 * Owns-a * 例如,學校與老師的關係,學校包含老師,但如果學校停辦了,老師依然存在。 ![](https://i.imgur.com/Lp6df4M.png) #### Composition * A whole-part relationship * 更為強烈的整體與部分關係,成員物件是整體物件的一部份,**成員物件不能夠脫離整體物件存在**。。 * Is-Part-Of * 例如,頭和嘴的關係,沒有了頭,嘴也就不存在了。 ![](https://i.imgur.com/RnYlggF.png) #### Generalization * 繼承關係 * Each subclass inherits all of the properties of its superclass and add its own unique properties * Has the advantage of code reuse(override) * Is-a ![](https://i.imgur.com/9EtzplL.png) #### Implementation * 介面**不會有任何的方法實作**以及**變數**,次類別也必須要遵守方法的名稱。 ![](https://i.imgur.com/Xn8ywLw.png) ## Design Principle * Encapsulate what varies into concrete class * 將容易改變的程式碼部分封裝起來(封裝成一個 class) * Abstract Common Behaviors into interface/abstract clss * Compose abstract interface/abstract or delegate behaviors ## Design Pattern [Design Pattern 中文 Blog](http://corrupt003-design-pattern.blogspot.com/) ### 必考題: Decorator 與 Composite Pattern 的差異 1. Decorator Pattern 只有一層繼承 2. Composite 可以 composite group 3. Decorator Pattern 是擴增被包裝物件的行為,而 Composite 僅僅是把所有物件的結果加總起來。 4. 當需要動態新增物件行為的時侯 => Decorator 5. 當需要呈現物件組合起來的時候 => Composite 6. Composite: Allows your external code to view the entire structure as a single entity 7. Composite: 能夠讓單體與群體看起來一模一樣 8. Decorator: 能夠讓物件外表看起來沒有改變的情況下改變行為 ### Strategy Pattern [策略模式 (Strategy Pattern)](http://corrupt003-design-pattern.blogspot.com/2016/02/strategy-pattern.html) * 策略模式定義了演算法家族,個別封裝起來,讓它們之間可以互相替換。此模式讓演算法的變動,不會影響到使用演算法的程式。 * 若不封成一個大家族,隨著演算法的數量變多,類別內需要做不同改動 * 將達成相似目標的不同演算法包成一個介面,類別只需要呼叫不同的介面實作即可。 * 讓使用者使用介面去選擇要使用哪個演算法 ![](https://i.imgur.com/Kwn5zBI.png) ### Observer Pattern [觀察者模式 (Observer Pattern)](http://corrupt003-design-pattern.blogspot.com/2016/02/observer-pattern.html) * 觀察者模式定義了物件之間的一對多關係,如此一來,當一個物件改變狀態,其他相依者都會收到通知並自動被更新。 * 觀察者觀察的物件如果發生了狀態變化就會通知觀察者,接著所有與該物件有關的物件就會隨著該狀態變化進行改動。 ![](https://i.imgur.com/dE5Qrll.png) ### Decorator Pattern [裝飾者模式 (Decorator Pattern)](http://corrupt003-design-pattern.blogspot.com/2016/03/decorator-pattern.html) * Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. * 裝飾者跟被裝飾者繼承至相同的元件,這樣裝飾者才能夠對被裝飾者進行動作。 * 透過合成可以讓裝飾者加上希望加上的裝飾行為。 ![](https://i.imgur.com/faEzeYV.png) ```java= // 摩卡是一個裝飾者, 因此讓它繼承自 CondimentDecorator // 而且別忘了, CondimentDecorator 繼承自 Beverage 喔 public class Mocha extends CondimentDecorator { // 要讓摩卡能參考到 Beverage, // 因此需要這個成員變數 Beverage mBeverage; public Mocha(Beverage beverage) { this.mBeverage = beverage; } // 以下兩個 method 的作法都是利用委派(delegation) 的方式, // 從被裝飾者拿到資訊後, 再加上裝飾者的資訊 public String getDescription() { return mBeverage.getDescription() + ", Mocha"; } public double cost() { return .20 + mBeverage.cost(); } } ``` ### Factory Pattern [工廠方法模式 (Factory Method Pattern)](http://corrupt003-design-pattern.blogspot.com/2016/05/factory-method-pattern.html) * 工廠方法模式定義了一個建立物件的介面,但由次類別決定要實體化的類別為何者。工廠方法讓類別把實體化的動作,交由次類別進行。 * 用一個介面創造物件,另外用一個 subclass 來決定要創造甚麼物件出來。 ![](https://i.imgur.com/7HQfbxi.png) ### Abstract Factory Pattern [工廠模式 - 抽象工廠模式 (Abstract Factory Pattern)](http://corrupt003-design-pattern.blogspot.com/2016/06/abstract-factory-pattern.html) * 抽象工廠模式提供了一個介面,建立相關或相依物件之家族,而不需要明確指定具象類別 * 抽象工廠提供的是工廠的介面,不同工廠的不同工法使用不需要知道,只需要說用哪個工廠就好。 ![](https://i.imgur.com/D9EjbKT.png) ### Iterator Pattern [反覆器模式 (Iterator Pattern)](http://corrupt003-design-pattern.blogspot.com/2016/07/iterator-pattern.html) * 反覆器模式讓我們能夠取得一個聚集內的每一個元素,而不需要此聚集將其實踐方式暴露出來 * 如果不同物件有不同的遍歷方式,則創建一個迭代器根據不同的資料結構進行遍歷即可。 ![](https://i.imgur.com/yRsXF2l.png) ### Composite Pattern [合成模式 (Composite Pattern)](http://corrupt003-design-pattern.blogspot.com/2016/08/composite-pattern.html) * 合成模式允許你將物件合成樹狀結構,呈現「部份 / 整體」的階層關係。合成能讓客戶程式碼以一致的方式處理個別物件,以及合成的物件。 * 合成模式讓用戶能夠用**一致的方式處理個體以及組合的物件**。提供個別以及個別組成的合成物。 ![](https://i.imgur.com/HtQnLKT.png) ### Facade Pattern [表象模式 (Facade Pattern)](http://corrupt003-design-pattern.blogspot.com/2016/07/facade-pattern.html) * 表象模式提供了一個統一的介面,用來存取次系統中的一群介面。表象定義了一個較高層次的介面,讓次系統更容易使用。 * 將複雜一連串的操作作為次系統,Facade 作為次系統的門面,使用者只要透過 Facade 來 access 次系統裡面的東西。 ![](https://i.imgur.com/PSts2WS.png) ### Template Pattern [樣板方法模式 (Template Method Pattern)](http://corrupt003-design-pattern.blogspot.com/2016/07/template-method-pattern.html) * 樣板方法模式將一個演算法的骨架定義在一個方法中,而演算法本身會用到的一些方法,則是定義在次類別中。樣板方法讓次類別在不改變演算法架構的情況下,重新定義演算法中的某些步驟。 * Template 提供一個做事的 template,但不同物件在處理 template 中的相同階段時可能需要不同的實作,而這些不同的實作就交由該物件去分別完成他們需要的部分。 * Abs class 中有相同的 method,而分別繼承的物件會各自實作不同部分的 method * 如下圖的 primitive method 都是繼承的 concrete class 各自實作。 ![](https://i.imgur.com/gCeqVZm.png) ### Command Pattern [命令模式 (Command Pattern)](http://corrupt003-design-pattern.blogspot.com/2016/06/command-pattern.html) * 命令模式將「請求」封裝成物件,以便使用不同的請求、佇列、或者日誌,參數化其他物件。命令模式也支援可復原的作業。 ![](https://i.imgur.com/I3OjXwy.png) ### Adapter Pattern [轉接器模式 (Adapter Pattern)](http://corrupt003-design-pattern.blogspot.com/2016/07/adapter-pattern.html) * 轉接器模式將一個類別的介面,轉換成別一個介面以供客戶使用。轉接器讓原本介面不相容的類別可以合作無間。 * 使用相同的介面,但根據不同的類別進行不同的實作。 * 令有一介面有**打擊**這個方法,棒球物件以及壘球物件能用不同的方式實作這個介面。 ![](https://i.imgur.com/VH2KbzV.png)