IOS Design Patterns Explore the 23 commonly used design patterns

Whether C# in.net, Java, VB.NET, C++, or objective-c, object-oriented languages are the same at the level of design patterns, but the syntax of the implementation of design patterns is slightly different:

The Strategy pattern, which defines a family of algorithms and encapsulates them separately so that they can be replaced with each other, enables changes in the algorithm without affecting the customers using the algorithm. Decorators, which dynamically add additional responsibilities to an object, are more flexible in terms of adding functionality than subclassing. Proxy, which provides a Proxy for other objects to control access to this object. Factory Method, which defines an interface for creating objects and lets subclasses decide which class to instantiate. The factory method delays the instantiation of a class to its subclasses. Prototype pattern, using Prototype instances to specify the type of object to create, and by copying these prototypes to create new objects. The Template Method pattern defines the skeleton of an algorithm in an operation, deferring some steps to subclasses. The template approach allows subclasses to redefine specific steps of an algorithm without changing the structure of that algorithm. A Facade pattern that provides a consistent interface for a set of interfaces in a subsystem. This pattern defines a high-level interface that makes the subsystem easier to use. The Builder pattern, which separates the construction of a complex object from its representation, allows the same construction process to create different representations. The Observer pattern defines a one-to-many dependency that allows multiple Observer objects to listen on a subject object at the same time. The topic object notifies all observer objects when their state changes, enabling them to update themselves automatically. The Abstract Factory pattern provides an interface for creating a series of related or interdependent objects without specifying their concrete classes. The State mode allows an object to change its behavior when its internal State changes, as if the object has changed its class. Adapter pattern, which translates the interface of one class into another interface that the customer wants. The Adapter pattern enables classes to work together that would otherwise not work together due to interface incompatibilities. The Memento pattern captures the internal state of an object and stores the state outside of the object without breaking encapsulation. You can then restore the object to its original saved state. Composite, which combines objects into a tree structure to represent a parties-whole hierarchy. The composite pattern makes the use of single objects and composite objects consistent. The Iterator pattern provides a way to access the elements of an aggregate object sequentially without exposing the internal representation of the object. The Singleton pattern guarantees that a class has only one instance and provides a global access point to it. The Bridge pattern separates the abstract part from its implementation part so that they can all change independently. Command, which encapsulates a request as an object, allowing you to parameterize clients with different requests; Queue or log requests, and support undoable operations. The Chain of Responsibility pattern avoids coupling between the sender and receiver of the request by giving multiple objects the opportunity to process the request. Chain the object and pass the request along the chain until an object processes it. Mediator pattern, which encapsulates a series of object interactions with a Mediator object. The mediator loosens the coupling by eliminating the need for objects to explicitly refer to each other, and can change their interactions independently. The Flyweight pattern uses sharing technology to effectively support a large number of fine-grained objects. The Interpreter pattern, given a language, defines a representation of its grammar and an Interpreter that uses that representation to interpret sentences in the language. Visitor pattern, which represents an operation that acts on elements in an object structure. It allows you to define new operations on elements without changing their classes.Copy the code

The basic principles of design patterns are very important, and once you really understand them deeply, many design patterns are just applications of them, and you may be using them unconsciously:

The single responsibility principle (SRP), for a class, should have only one cause for its change. The open-closed principle (OCP) states that software entities (classes, modules, functions, and so on) should be extensible but not modifiable. Dependency inversion principle (DIP), a. High-level modules should not depend on low-level modules, both should depend on abstractions. B. Abstractions should not depend on details. Details should depend on abstractions. The Richter substitution principle (LSP) states that child types must be able to replace their parent types. Demeter's law (LoD) states that two classes should not interact directly if they do not have to communicate directly with each other. If one class needs to call a method of another class, the call can be forwarded by a third party. The Principle of composition/aggregation reuse (CARP), use composition/aggregation whenever possible, do not use class inheritance whenever possible.Copy the code

# Reference blog recommendations:

How does Cocoa apply design patterns