面向可複用性和可維護性的設計模式

2022113415-罗昊然發表於2024-05-13

面向可複用性和可維護性的設計模式

HIT Software Consturction

哈工軟體構造課程 內容回顧

——Design Patterns for Reuse and Maintainbality

面向可複用性和可維護性的設計模式

Outline(課件)

  • Creational patterns
    • Factory method pattern creates objects without specifying the exact class.
  • Structural patterns
    • Adapter allows classes with incompatible interfaces to work together by wrapping its own interface around that of an already existing class.
    • Decorator dynamically adds/overrides behavior in a method of an object.
  • Behavioral patterns
    • Strategy allows one of a family of algorithms to be selected at runtime.
    • Template method defines the skeleton of an algorithm as an abstract class, allowing its subclasses to provide concrete behavior.
    • Iterator accesses the elements of an object sequentially without exposing its underlying representation.
    • Visitor separates an algorithm from an object structure by moving the hierarchy of methods into one object

總述

all for Reuseability, Maintainablitity and Extensibility.

設計模式(Design pattern)代表了最佳的實踐,通常被有經驗的物件導向的軟體開發人員所採用。設計模式是軟體開發人員在軟體開發過程中面臨的一般問題的解決方案。這些解決方案是眾多軟體開發人員經過相當長的一段時間的試驗和錯誤總結出來的。

設計模式是一套被反覆使用的、多數人知曉的、經過分類編目的、程式碼設計經驗的總結。使用設計模式是為了重用程式碼、讓程式碼更容易被他人理解、保證程式碼可靠性。 毫無疑問,設計模式於己於他人於系統都是多贏的,設計模式使程式碼編制真正工程化,設計模式是軟體工程的基石,如同大廈的一塊塊磚石一樣。專案中合理地運用設計模式可以完美地解決很多問題,每種模式在現實中都有相應的原理來與之對應,每種模式都描述了一個在我們周圍不斷重複發生的問題,以及該問題的核心解決方案,這也是設計模式能被廣泛應用的原因。

來自https://www.runoob.com/design-pattern/design-pattern-intro.html*

暈暈,看不懂

簡單來說就是一套經過碼農先輩反覆使用得到驗證被證明行之有效的軟體中模組的程式碼設計“公式”

物件導向的設計模式中,通常來講除了類本身,這些設計模式更加強調多個類/物件之間的關係與互動過程——這一內容的顆粒度要比介面/類複用的顆粒度要大。

省略:四人幫GOF

根據設計模式的參考書 Design Patterns - Elements of Reusable Object-Oriented Software,我們可以認為一般將設計模式劃分為三類:

  • 創造型模式(Creational Patterns):更關注物件建立的過程。相較於直接使用new運算子,這一模式期望在建立物件的同時隱藏建立的邏輯。
  • 結構型模式(Structural Patterns):關注物件之間的組合和關係,解決類/物件的結構組成問題
  • 行為類模式(Behavioral Patterns):關注物件之間的通訊和互動,“符號化”類/物件的互動與責任分配方式。

這三類中又包含著豐富的具體設計模式,它們的關係be like:

img

同樣來自https://www.runoob.com/design-pattern/design-pattern-intro.html*

設計模式介紹(課內)

下面對課程內涉及的設計模式進行介紹:

創造型模式 (Creational Patterns)

工廠模式(Factory Method)

結構型模式 (Structural Patterns)

介面卡模式(Adapter)

裝飾器模式(Decorator)

行為類模式 (Behavioral Patterns)

策略模式(Strategy)

模板模式(Template)

迭代器(Iterator)

訪問者(Visitor)

設計模式共性/差異比較

總結

相關文章