[Common] Design Patterns by Tutorials

1. Class Diagram
UML.png
2. MVC
MVC.png
3. Strategy
Strategy

This pattern is similar to the delegation pattern: both patterns rely on a protocol instead of concrete objects for increased flexibility. Consequently, any object that implements the strategy protocol can be used as a strategy at runtime.

Unlike delegation, the strategy pattern uses a family of objects.

Delegates are often fixed at runtime. For example, the dataSource and delegate for a UITableView can be set from Interface Builder, and it’s rare for these to change during runtime.

Strategies, however, are intended to be easily interchangeable at runtime.

策略比如游戲里面切換random模式和順序模式,或者根據(jù)ab會(huì)有不同的推薦策略這種,重點(diǎn)是切換/一組。但是delegate其實(shí)是固定的,比如View的delegate基本和某個(gè)對象綁死。

4. Builder
builder

Use the builder pattern when you want to create a complex object using a series of steps. This pattern works especially well when a product requires multiple inputs.

builder
  • The builder pattern is great for creating complex objects in a step-by-step fashion. It involves three objects: the director, product and builder.

  • The director accepts inputs and coordinates with the builder; the product is the complex object that’s created; and the builder takes step-by-step inputs and creates the product.

5. MVVM
MVVM
  • Models hold app data. They’re usually structs or simple classes.
  • Views display visual elements and controls on the screen. They’re typically
    subclasses of UIView.
  • View models transform model information into values that can be displayed on a
    view. They’re usually classes, so they can be passed around as references.

View models are classes that take objects and transform them into different objects, which can be passed into the view controller and displayed on the view. They’re especially useful for converting computed properties such as Date or Decimal into a String or something else that actually can be shown in a UILabel or UIView.

盡量不要把很多view綁定一個(gè)vm,會(huì)比較混亂哦,拆分會(huì)好一點(diǎn)。

6. Factory

A factory is very useful when you have a group of related products, such as polymorphic subclasses or several objects that implement the same protocol.

這里其實(shí)和我們根據(jù)不同場景進(jìn)入不同的編輯頁很像,就是規(guī)定好protocol然后init不同的concrete class。

7. Adapter
截屏2021-10-03 下午1.54.12.png
8. State
截屏2021-10-03 下午4.24.54.png

莫名覺得其實(shí)狀態(tài)機(jī)和策略模式有一點(diǎn)像,但是策略不會(huì)像狀態(tài)變的這么頻繁。

9. Fly Weight
截屏2021-10-03 下午8.39.49.png

例如如果生成的 UIColor 的 rgba 一樣,就返回同一個(gè)對象,減少內(nèi)存損耗。但這個(gè)要求比較高,不能一個(gè)修改影響別的使用,所以需要不可變最好。字體加載也可以適用。

10. Mediator
截屏2021-10-03 下午8.49.48.png

感覺有點(diǎn)像消息總線~ 也有點(diǎn)多播的意思 0.0 主要也是為了解耦 peer 之間的依賴,就像我們曾經(jīng)處理的組件間依賴。

11. Command
截屏2021-10-03 下午9.28.44.png
  • The invoker stores and executes commands; the command encapsulates an action as an object; and the receiver is the object that's acted upon.

  • This pattern works best for actions that need to be stored and executed later. If you always intend to execute actions immediately, consider calling the methods directly on the receiver instead.

12. Chain-of- Responsibility
截屏2021-10-03 下午9.33.41.png

責(zé)任鏈其實(shí)就是 View 的響應(yīng)傳遞的設(shè)計(jì)模式哦~ 就是一個(gè)個(gè)去問所有handler是不是可以處理醬紫。

13. Coordinator
截屏2021-10-03 下午9.34.56.png

The concrete router knows how to present view controllers, but it doesn’t know exactly what is being presented or which view controller will be presented next. Instead, the coordinator tells the router which view controller to present.

Use this pattern to decouple view controllers from one another. The only component that knows about view controllers directly is the coordinator.

截屏2021-10-03 下午10.20.28.png

在 coordinator 里面可以調(diào)用其他 coordinator 來做跳轉(zhuǎn)哈,其實(shí) coordinator 主要就是負(fù)責(zé)跳轉(zhuǎn),然后 router 負(fù)責(zé) present 動(dòng)作醬紫。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容