# Swift Design Pattern 讀書會 --- ## Chapter 1: What are Design Pattern --- 在開始之前... --- 討論: 你為什麼想學 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 只是一種工具 --- 討論:開發過程中常遇到的問題是什麼? --- 我的答案 --- ## 物件導向設計的五原則 SOLID 1. 單一職責原則(Single responsibility principle, SRP) 2. 開放封閉原則(Open-Close principle, OCP) 3. 里氏替換原則(Liskov substitution principle, LSP) 4. 接口隔離原則(Interface segregation principle, ISP) 5. 依賴反轉原則(Dependency inversion principle, DIP) --- ### 單一職責原則(Single responsibility principle, SRP) 每個物件,不管是類別、函數,都應該只做一件事 --- ### 開放封閉原則(Open-Close principle, OCP) 當需求變更時,在不改變現有程式碼的前提之下,藉由增加新的程式碼來實作新的功能,而並非修改程式增加功能。 --- ### 里氏替換原則(Liskov substitution principle, LSP) > "Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it." 子類應該要可以替換掉父類,而不會影響程式 --- ### 接口隔離原則(Interface segregation principle, ISP) 只接會用到的介面,用最少的方法去完成最多的事 可避免不相關的需求介面異動,造成被強迫一同面對異動的情況 --- ### 依賴反轉原則(Dependency inversion principle, DIP) > "Program to an interface, not an implementation." 物件和物件不應該相互相依,而是要依賴於抽象層(相對於實例,抽象層要穩定得多) --- 討論:你們會按這五大原則開發嗎? --- ## Criticisms 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. --- ## Benefits 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. --- ## Types of design patterns 1. Structural design pattern 結構型模式 2. Behavioral design pattern 行為型模式 3. Creational design pattern 創建型模式 ~ 沒有很重要 --- Design patterns aren't concrete implementations, but rather, they are a starting point for writing code. --- 休息三分鐘 ---
{"metaMigratedAt":"2023-06-14T23:27:59.531Z","metaMigratedFrom":"YAML","title":"What are Design Pattern","breaks":true,"description":"2019/08/18","contributors":"[{\"id\":\"c23c28cc-0f80-4485-ad9a-b8afa43619b9\",\"add\":5491,\"del\":3270}]"}
    784 views