iT邦幫忙

2023 iThome 鐵人賽

DAY 19
0
Software Development

深入淺出設計模式 - 使用 C++系列 第 19

[Day 19] 模式動物園 — 23種模式的總結及補充

  • 分享至 

  • xImage
  •  
  • 目前我們介紹了 14 種基本設計模式,以及 MVC 複合模式。這邊我們來簡單總結一下 GoF (四人幫) 設計模式中所提到的 23 種設計模式以及簡短定義
  • 此外,也介紹除了基本設計模式之外,還有哪些本書沒提及的特殊模式 (又可以再用另一本書介紹了)

GoF 23 種基本模式總結

https://ithelp.ithome.com.tw/upload/images/20231001/20138643mSLJ8heesJ.jpg
https://ithelp.ithome.com.tw/upload/images/20231001/20138643xfyL4DJ16F.jpg

  • Singleton
    • 保證一個類別僅有一個實例
    • Ensures a class has only one instance
  • Prototype
    • 用原型實例指定建立對象的種類
    • Creates new objects by copying an existing object, known as the "prototype"
  • Builder
    • 將一個複雜對象的構建與表示分離
    • Separates the construction of a complex object from its representation
  • Factory Method
    • 定義一個用於創建對象的介面,將實際創建對象的工作推遲到子類中,這樣子類就可以根據需要創建不同類型的對象
    • Defines an interface for creating an object but allows subclasses to alter the type of objects that will be created
  • Abstract Factory
    • 提供一個接口以創建一系列相關或相互依賴的對象
    • Provides an interface for creating families of related or dependent objects
  • Adapter
    • 使接口不兼容的類可以一起工作
    • Allows incompatible interfaces to work together
  • Bridge
    • 將抽象部分與實現部分分離
    • Separates an object's abstraction from its implementation.
  • Composite
    • 將對象組合成樹形結構以表示部分-整體層次
    • Composes objects into tree structures to represent part-whole hierarchies
  • Decorator
    • 給一個對象添加一些額外的職責
    • Adds new functionality to an object without altering its structure
  • Flyweight
    • 用共享對象支持大量細粒度對象
    • Minimizes memory usage or computational expenses by sharing as much as possible
  • Facade
    • 為子系統中的一組接口提供一個統一的界面
    • Provides a unified interface to a set of interfaces in a subsystem
  • Proxy
    • 為其他對象提供一種代理以控制對此對象的訪問
    • Provides a surrogate or placeholder for another object to control access to it
  • Chain of Responsibility
    • 讓多個對象都有機會處理請求
    • Passes a request along a chain of handlers
  • Command
    • 封裝一個請求作為一個對象
    • Encapsulates a request as an object
  • Interpreter
    • 為語言建立表示
    • Defines a representation for a language
  • Iterator
    • 提供一種方法來訪問聚合對象
    • Provides a way to access elements of an aggregate object
  • Mediator
    • 定義一個封裝一組對象如何交互的對象
    • Defines an object that encapsulates how a set of objects interact
  • Memento
    • 在不破壞封裝性的前提下捕獲一個對象的內部狀態
    • Captures and restores an object's internal state
  • Observer
    • 允許對象與其依賴者建立一個一對多的依賴關係,以便當對象狀態改變時,所有依賴者都會得到通知
    • Establishes a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically
  • State
    • 允許對象在內部狀態改變時改變行為
    • Allows an object to alter its behavior when its internal state changes
  • Strategy
    • 允許定義一系列演算法,並在執行時動態地選擇使用哪一種
    • Allows defining a set of algorithms and interchangeably use them during runtime based on the context
  • Template Method
    • 定義一個操作中的演算法的框架
    • Defines the skeleton of an algorithm in a method
  • Visitor
    • 允許你在不改變既有類別結構的前提下,加入新的操作或行為
    • Enables adding new functionalities to an existing class hierarchy without altering its structure

模式動物園

架構模式 (Architectural Patterns)

  • Layered (N-tier) Architecture
    • 將軟體分為不同的層次,以隔離各自的責任和關注點
    • Divides the software into separate layers to isolate responsibilities
  • Client-server Architecture
    • 將工作負載分配在提供者(伺服器)和請求者(客戶端)之間
    • Splits the workload between providers (servers) and requestors (clients)
  • Event-driven Architecture
    • 使用事件來觸發和溝通於解耦合的服務之間
    • Uses events to trigger and communicate between decoupled services
  • Pipe and Filter Pattern
    • 由一系列處理元素組成,這些元素的排列方式是使得每個元素的輸出成為下一個元素的輸入
    • Consists of a chain of processing elements, arranged so that the output of each element is the input of the next
  • Microkernel Architecture
    • 將功能性分為一個最小核心和一組可選擇的擴展
    • Splits functionality into a minimal kernel and a set of optional extensions
  • Service Oriented Architecture (SOA)
    • 將系統分解為多個功能單元,每個單元都有定義的接口並可作為網絡服務
    • Decomposes a system into various functional units, each with defined interfaces and available as a network service
  • Microservices Architecture
    • 將應用程序分解為松散耦合,獨立可部署的服務
    • Decomposes an application into loosely coupled, independently deployable services
  • Service Broker Pattern
    • 集中外部訪問數據和功能,為不同的服務提供單一訪問點
    • Centralizes external access to data and functions, providing a single access point for different services
  • Peer-to-Peer Pattern
    • 在具有相同權限的對等方之間分配任務和工作負載
    • Distributes tasks and workloads between peers who are equally privileged
  • Cloud-native Architecture
    • 一種構建和運行應用程序的方法,該方法充分利用了雲計算模型的優勢
    • An approach to building and running applications that fully exploit the advantages of the cloud computing model
  • CQRS (Command Query Responsibility Segregation)
    • 分隔讀取和修改數據源的架構模式
    • An architecture pattern that separates reading and modification of a data source

應用模式 (Application Patterns)

  • MVC (Model-View-Controller)
    • 將應用程式分為三個主要組件:模型(Model)、視圖(View)和控制器(Controller)
    • Separates an application into three main components: the model, the view, and the controller
  • MVVM (Model-View-ViewModel)
    • 類似於MVC,但在視圖(View)和視圖模型(ViewModel)之間提供數據綁定
    • Similar to MVC but provides data binding between the View and the ViewModel
  • MVP (Model-View-Presenter)
    • 將用戶界面的管理責任與業務邏輯的責任分開
    • Separates the responsibility of managing the user interface from that of the business logic

領域專屬模式 (Domain-Specific Patterns)

筆者註: 這些模式在嵌入式軟韌體開發中還滿常見到的

用於 Concurrent Systems

  • Producer-Consumer Pattern
    • 管理生成數據的生產者和處理它的消費者之間的互動,通常通過一個共享隊列
    • Manages the interaction between producers that generate data and consumers that process it, usually through a shared queue
  • Futures and Promises Pattern
    • 允許異步地檢索計算結果,實現更好的資源利用
    • Allows computation results to be retrieved asynchronously, enabling better resource utilization
  • ReadWrite Lock Pattern
    • 以一種允許多個讀取或獨占寫入的方式來管理對共享資源的讀取和寫入訪問
    • Manages read and write access to shared resources in a way that allows multiple reads or exclusive writes

用於 Real-Time Systems

  • Rate Monotonic Scheduling Pattern
    • 根據任務的周期性進行優先級排序,頻繁的任務具有更高的優先級
    • Prioritizes tasks based on their periodicity, with more frequent tasks given higher priority
  • Time-Triggered Pattern
    • 任務是基於預定的時間間隔執行的,使其具有可預測性
    • Tasks are executed based on pre-determined time intervals, making it predictable

使用者介面設計模式 (User Interface Design Patterns)

  • Breadcrumbs
    • 使用連結標籤提供從首頁到當前站點頁面的次要導航
    • Use linked labels to provide secondary navigation that shows the path from the front to the current site page in the hierarchy
  • Lazy Registration
    • 允許用戶先體驗站點/應用功能,再顯示註冊表單
    • Use this sign-up pattern to let users sample what your site/app offers for free or familiarize themselves with it. Then, show them a sign-up form
  • Forgiving Format
    • 讓用戶以多種格式輸入資料 (例: 城市/鎮/村或郵政編碼)
    • Let users enter data in various formats (e.g., city/town/village or zip code)
  • Clear Primary Actions
    • 用顏色突出按鈕,以便用戶知道該做什麼 (例: "提交")
    • Make buttons stand out with color so users know what to do (e.g., "Submit")

Reference

  1. https://wayne265265.pixnet.net/blog/post/115771769-%E3%80%90%E7%AD%86%E8%A8%98%E3%80%91architectural-pattern-%28%E6%9E%B6%E6%A7%8B%E6%A8%A1%E5%BC%8F%29
  2. https://en.wikipedia.org/wiki/Architectural_pattern
  3. https://www.interaction-design.org/literature/topics/ui-design-patterns
  4. http://www.celinio.net/techblog/?p=65

上一篇
[Day 18] 複合模式的王者 — MVC (Model-View-Controller)
下一篇
[Day 20] 將實作與抽象放在不同類別階層 — 橋接器模式 (Bridge Pattern)
系列文
深入淺出設計模式 - 使用 C++37
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言