[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模式和顺序模式,或者根据ab会有不同的推荐策略这种,重点是切换/一组。但是delegate其实是固定的,比如View的delegate基本和某个对象绑死。

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绑定一个vm,会比较混乱哦,拆分会好一点。

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.

这里其实和我们根据不同场景进入不同的编辑页很像,就是规定好protocol然后init不同的concrete class。

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

莫名觉得其实状态机和策略模式有一点像,但是策略不会像状态变的这么频繁。

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

例如如果生成的 UIColor 的 rgba 一样,就返回同一个对象,减少内存损耗。但这个要求比较高,不能一个修改影响别的使用,所以需要不可变最好。字体加载也可以适用。

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

感觉有点像消息总线~ 也有点多播的意思 0.0 主要也是为了解耦 peer 之间的依赖,就像我们曾经处理的组件间依赖。

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

责任链其实就是 View 的响应传递的设计模式哦~ 就是一个个去问所有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 里面可以调用其他 coordinator 来做跳转哈,其实 coordinator 主要就是负责跳转,然后 router 负责 present 动作酱紫。

你可能感兴趣的:([Common] Design Patterns by Tutorials)