除了 MVC,iOS 还使用了哪些设计模式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12436497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What Design Patterns are used on iOS other than MVC?
提问by Santosh Gurram
I need to know about the design patterns used in iPhone development other than MVC.
我需要了解除 MVC 之外的 iPhone 开发中使用的设计模式。
Please reply with any sample explanation or example with code snippet.
请回复任何示例说明或带有代码片段的示例。
Thanks.
谢谢。
回答by DrummerB
Abstract Factory
抽象工厂
The Abstract Factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. The client is decoupled from any of the specifics of the concrete object obtained from the factory.
抽象工厂模式提供了一个接口,用于创建相关或依赖对象的系列,而无需指定它们的具体类。客户端与从工厂获得的具体对象的任何细节分离。
Adapter
适配器
The Adapter design pattern converts the interface of a class into another interface that clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. It decouples the client from the class of the targeted object.
适配器设计模式将类的接口转换为客户端期望的另一个接口。适配器让那些因为接口不兼容而不能一起工作的类一起工作。它将客户端与目标对象的类分离。
Chain of Responsibility
责任链
The Chain of Responsibility design pattern decouples the sender of a request from its receiver by giving more than one object a chance to handle the request. The pattern chains the receiving objects together and passes the request along the chain until an object handles it. Each object in the chain either handles the request or passes it to the next object in the chain.
责任链设计模式通过为多个对象提供处理请求的机会,将请求的发送者与其接收者分离。该模式将接收对象链接在一起并沿着链传递请求,直到对象处理它。链中的每个对象要么处理请求,要么将其传递给链中的下一个对象。
Command
命令
The Command design pattern encapsulates a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. The request object binds together one or more actions on a specific receiver. The Command pattern separates an object making a request from the objects that receive and execute that request.
Command 设计模式将请求封装为一个对象,从而让您可以将具有不同请求、队列或日志请求的客户端参数化,并支持可撤销的操作。请求对象将特定接收器上的一个或多个操作绑定在一起。命令模式将发出请求的对象与接收和执行该请求的对象分开。
Composite
合成的
The Composite design pattern composes related objects into tree structures to represent part-whole hierarchies. The pattern lets clients treat individual objects and compositions of objects uniformly. The Composite pattern is part of the Model-View-Controller aggregate pattern.
复合设计模式将相关对象组合成树结构以表示部分-整体层次结构。该模式允许客户端统一处理单个对象和对象的组合。复合模式是模型-视图-控制器聚合模式的一部分。
Decorator
装饰器
The Decorator design pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. As does subclassing, adaptation of the Decorator pattern allows you to incorporate new behavior without modifying existing code. Decorators wrap an object of the class whose behavior they extend. They implement the same interface as the object they wrap and add their own behavior either before or after delegating a task to the wrapped object. The Decorator pattern expresses the design principle that classes should be open to extension but closed to modification.
装饰器设计模式动态地将附加职责附加到对象上。装饰器提供了一种灵活的替代子类来扩展功能。与子类化一样,装饰器模式的改编允许您在不修改现有代码的情况下合并新行为。装饰器包装了它们扩展其行为的类的对象。它们实现与它们包装的对象相同的接口,并在将任务委派给包装对象之前或之后添加自己的行为。装饰者模式表达了类应该对扩展开放但对修改关闭的设计原则。
Facade
正面
The Facade design pattern provides a unified interface to a set of interfaces in a subsystem. The pattern defines a higher-level interface that makes the subsystem easier to use by reducing complexity and hiding the communication and dependencies between subsystems.
Facade 设计模式为子系统中的一组接口提供了一个统一的接口。该模式定义了一个更高级别的接口,通过降低复杂性和隐藏子系统之间的通信和依赖关系,使子系统更易于使用。
Iterator
迭代器
The Iterator design pattern provides a way to access the elements of an aggregate object (that is, a collection) sequentially without exposing its underlying representation. The Iterator pattern transfers the responsibility for accessing and traversing the elements of a collection from the collection itself to an iterator object. The Iterator defines an interface for accessing collection elements and keeps track of the current element. Different iterators can carry out different traversal policies.
Iterator 设计模式提供了一种顺序访问聚合对象(即集合)的元素而不暴露其底层表示的方法。迭代器模式将访问和遍历集合元素的责任从集合本身转移到迭代器对象。Iterator 定义了一个用于访问集合元素的接口并跟踪当前元素。不同的迭代器可以执行不同的遍历策略。
Mediator
调解员
The Mediator design pattern defines an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently. These objects can thus remain more reusable. A "mediator object” in this pattern centralizes complex communication and control logic between objects in a system. These objects tell the mediator object when their state changes and, in turn, respond to requests from the mediator object.
Mediator 设计模式定义了一个对象,该对象封装了一组对象如何交互。中介器通过防止对象显式地相互引用来促进松散耦合,并且它允许您独立地改变它们的交互。因此,这些对象可以保持更高的可重用性。此模式中的“中介对象”集中了系统中对象之间的复杂通信和控制逻辑。这些对象在其状态发生变化时通知中介对象,并反过来响应中介对象的请求。
Memento
纪念
The Memento pattern captures and externalizes an object's internal state—without violating encapsulation—so that the object can be restored to this state later. The Memento pattern keeps the important state of a key object external from that object to maintain cohesion.
Memento 模式在不违反封装的情况下捕获并外部化对象的内部状态,以便稍后可以将对象恢复到该状态。备忘录模式将关键对象的重要状态保持在该对象之外,以保持内聚。
Observer
观察员
The Observer design pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. The Observer pattern is essentially a publish-and-subscribe model in which the subject and its observers are loosely coupled. Communication can take place between the observing and observed objects without either needing to know much about the other.
观察者设计模式定义了对象之间的一对多依赖关系,这样当一个对象改变状态时,它的所有依赖项都会被自动通知和更新。观察者模式本质上是一种发布-订阅模型,其中主体及其观察者是松散耦合的。观察对象和被观察对象之间可以进行通信,而无需对对方了解太多。
Proxy
代理
The Proxy design pattern provides a surrogate, or placeholder, for another object in order to control access to that other object. You use this pattern to create a representative, or proxy, object that controls access to another object, which may be remote, expensive to create, or in need of securing. This pattern is structurally similar to the Decorator pattern but it serves a different purpose; Decorator adds behavior to an object whereas Proxy controls access to an object.
代理设计模式为另一个对象提供代理或占位符,以便控制对另一个对象的访问。您可以使用此模式创建一个代表或代理对象,该对象控制对另一个对象的访问,该对象可能是远程的、创建成本高的或需要保护的。这种模式在结构上类似于装饰器模式,但它的用途不同;装饰器向对象添加行为,而代理控制对对象的访问。
Receptionist
接待员
The Receptionist design pattern addresses the general problem of redirecting an event occurring in one execution context of an application to another execution context for handling. It is a hybrid pattern. Although it doesn't appear in the “Gang of Four” book, it combines elements of the Command, Memo, and Proxy design patterns described in that book. It is also a variant of the Trampoline pattern (which also doesn't appear in the book); in this pattern, an event initially is received by a trampoline object, so-called because it immediately bounces, or redirects, the event to a target object for handling.
接待员设计模式解决了将在应用程序的一个执行上下文中发生的事件重定向到另一个执行上下文进行处理的一般问题。这是一种混合模式。虽然它没有出现在“四人帮”一书中,但它结合了该书中描述的命令、备忘录和代理设计模式的元素。它也是 Trampoline 模式的一种变体(也没有出现在书中);在这种模式中,一个事件最初是由一个蹦床对象接收的,之所以这么叫是因为它会立即将事件反弹或重定向到目标对象进行处理。
Singleton
单身人士
The Singleton design pattern ensures a class only has one instance, and provides a global point of access to it. The class keeps track of its sole instance and ensures that no other instance can be created. Singleton classes are appropriate for situations where it makes sense for a single object to provide access to a global resource.
单例设计模式确保一个类只有一个实例,并提供对它的全局访问点。该类跟踪其唯一实例并确保不能创建其他实例。单例类适用于单个对象提供对全局资源的访问的情况。
Template Method
模板方法
The Template Method design pattern defines the skeleton of an algorithm in an operation, deferring some steps to subclasses. The Template Method pattern lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
模板方法设计模式定义了操作中算法的骨架,将一些步骤推迟到子类中。模板方法模式允许子类重新定义算法的某些步骤,而无需更改算法的结构。
Source:Cocoa Design Patterns.
来源:可可设计模式。
回答by Jani
回答by Ahmed Askar
Using MVVM Design pattern in your application is related to your business logic that you will do in your project to display some contents on view. Incase your view doesn't need more logic to display it's content you can use MVC but if you have to make some business logic to display these contents on view the best practise in this case is to separate this logic to be in another layer so MVVM will better in this case,ViewModel in MVVM will contain this logic.
在您的应用程序中使用 MVVM 设计模式与您将在项目中执行以在视图中显示一些内容的业务逻辑相关。如果您的视图不需要更多逻辑来显示其内容,您可以使用 MVC,但是如果您必须制作一些业务逻辑来在视图上显示这些内容,在这种情况下的最佳实践是将此逻辑分离到另一层,因此 MVVM在这种情况下会更好,MVVM 中的 ViewModel 将包含此逻辑。
In my opinion MVVM is better than MVC on level design due to these reasons
由于这些原因,我认为 MVVM 在关卡设计上优于 MVC
- MVVM is compatible with your existing MVC architecture.
- MVVM makes your apps more testable.
- MVVM works best with a binding mechanism.
- MVVM 与您现有的 MVC 架构兼容。
- MVVM 使您的应用程序更具可测试性。
- MVVM 与绑定机制配合使用效果最佳。
How MVVM is compatible with MVC
MVVM 如何与 MVC 兼容
- MVC > Model,View,Controller
- MVVM > Model,View ,ViewModel > Model,(ViewController),ViewModel
- MVC > 模型、视图、控制器
- MVVM > Model,View ,ViewModel > Model,(ViewController),ViewModel