在開始之前…
討論:
你為什麼想學 Design Pattern?
你覺得 Design Pattern 會幫到你什麼?
"Design patterns collectively form a set of best practices to help you write more understandable and easier-to-maintain code.""
Design Pattern 是總結出來的有效經驗,幫助我們寫出更易理解且更易於維護的代碼
“there are no silver bullets in software development”
Design Pattern 只是一種工具
討論:開發過程中常遇到的問題是什麼?
我的答案
每個物件,不管是類別、函數,都應該只做一件事
當需求變更時,在不改變現有程式碼的前提之下,藉由增加新的程式碼來實作新的功能,而並非修改程式增加功能。
"Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it."
子類應該要可以替換掉父類,而不會影響程式
只接會用到的介面,用最少的方法去完成最多的事
可避免不相關的需求介面異動,造成被強迫一同面對異動的情況
"Program to an interface, not an implementation."
物件和物件不應該相互相依,而是要依賴於抽象層(相對於實例,抽象層要穩定得多)
討論:你們會按這五大原則開發嗎?
If you overuse design patterns, your project can become overly complex.
Many design patterns are made redundant by modern programming languages.
Design patterns are a lazy substitute for learning object-oriented principles.
Design patterns create a common language.
Design patterns fast-track developer onboarding.
Design patterns make you a better person.
Knowing design patterns allow you to spot similarities between code.
~ 沒有很重要
Design patterns aren't concrete implementations, but rather, they are a starting point for writing code.
休息三分鐘