Six rules for iOS design patterns

Source: Internet
Author: User

Design model of the six big Principles

Single principle of responsibility

definition: do not have more than one cause for class changes. In layman's terms, a class is responsible for only one responsibility.

The origin of the problem: class T is responsible for two different duties: responsibility P1, Responsibility P2. When a class T needs to be modified due to a change in the duty P1 requirements, it is possible to cause a normal function to malfunction P2 functionality.

Solution: Follow a single responsibility principle. Set up two classes T1, T2 respectively, so that T1 complete the functions P1 function, T2 complete functions P2 function. In this way, when the class T1 is modified, the responsibility is not P2 the risk of failure, and similarly, when T2 is modified, the responsibility is not P1 the risk of failure.

Richter Replacement principle (Liskov Substitution Principle)

There must have been a lot of doubts about the name of this principle as I have just seen it. The reason is that the principle was first introduced in 1988 by a woman surnamed Barbara Liskov of MIT.

definition 1 : If each object of type T1 is O1, there is an object O2 of type T2, so that all program P defined in T1 is replaced with O1 for all object O2, the behavior of program p does not change, then type T2 is a subtype of type T1.

definition 2 : all references to base classes must be able to use the objects of their subclasses transparently.

problem Origin: There is a functional P1, which is done by Class A. Now need to extend the function P1, the function of the extension is P, where p is composed of the original function P1 and the new function P2. The new function p is done by subclass B of Class A, and sub-class B, while completing the new function P2, may cause the original function P1 to fail.

Solution: Follow the Richter substitution principle when using inheritance. When Class B inherits from Class A, try not to rewrite the parent class A's method, but also try not to reload the parent Class A's method, except to add a new method to complete new functionality P2.

Inheritance contains a layer of meaning: in a parent class, a well-implemented method (as opposed to an abstract method) is actually a set of specifications and contracts, although it does not enforce that all subclasses must comply with these contracts, but if the subclasses arbitrarily modify these non-abstract methods, the entire inheritance system will be destroyed. The principle of replacing the Richter scale is to express this level of meaning.

Dependency inversion principle (dependence inversion Principle)

definition: high-level modules should not be dependent on the lower layers, both should rely on their abstraction; abstractions should not depend on detail; detail should be dependent on abstraction.

The problem is that class A is directly dependent on class B, and if you want to change Class A to dependency Class C, you must modify the code of Class A to achieve it. In this scenario, Class A is typically a high-level module that is responsible for complex business logic, and Class B and Class C are low-layer modules that are responsible for basic atomic operations, and if Class A is modified, it poses unnecessary risks to the program.

Solution: Modify Class A to dependent interface I, Class B and Class C each implement interface I, Class A through interface I indirectly with Class B or Class C, it will greatly reduce the chance to modify Class A.

The dependency inversion principle is based on the fact that abstract things are much more stable relative to the variability of details. Structures built on the basis of abstraction are much more stable than structures built on the basis of detail. In Java, abstract refers to an interface or abstract class, the details are specific implementation classes, the use of interfaces or abstract classes to make good specifications and contracts, not to involve any specific operation, the task of presenting details to their implementation class to complete.

Interface Isolation principle ( Interface Segregation Principle )

definition: The client should not rely on interfaces it does not need, and the dependency of one class on another should be based on the smallest interface.

The problem is that Class A relies on class B through interface I, Class C relies on class D through interface I, and if interface I is not the smallest interface for Class A and Class B, then Class B and Class D must implement methods that they do not need.

Solution: Split the bloated interface I into separate interfaces, and Class A and Class C are dependent on the interfaces they need. That is, the principle of interface isolation.

For example, the principle of interface isolation:

Dimitri Law ( Law of Demeter )

definition: An object should have minimal knowledge of other objects.

The problem is: The closer the relationship between classes and classes, the greater the coupling, and the greater the impact on another class when one class changes.

Solution: Minimize the coupling between classes and classes.

Since we began to touch programming, we have learned the general principles of software programming: low-coupling, high cohesion. Whether it is process-oriented programming or object-oriented programming, it is possible to increase the code reuse rate by minimizing the coupling between the modules. The advantages of low coupling are self-evident, but how do you program to do low-coupling? That's exactly what the Dimitri law is going to do.

The Dimitri rule is also known as the least known principle, which was first proposed by Ian Holland of the American Northeastern University in 1987. In layman's terms, it is a class that knows as little as possible about the classes it relies on. That is to say, for a dependent class, no matter how complex the logic, as far as possible to encapsulate the logic inside the class, outside the public method provided, not to disclose any information. The Dimitri rule also has a simpler definition: communicate only with direct friends. let's start by explaining what a direct friend is: Each object has a coupling relationship with other objects, so long as there is a coupling between the two objects, we say that the two objects are friends. There are many ways of coupling, dependence, association, composition, aggregation and so on. In this case, we call the class in the member variable, method parameter, method return value as a direct friend , and the class appearing in the local variable is not a direct friend. In other words, unfamiliar classes are best not to appear inside the class as a local variable.

Opening and closing principle ( Open Close Principle )

definition: a software entity such as classes, modules, and functions should be open to extensions and closed for modification.

The problem is: during the software lifecycle, because changes, upgrades, and maintenance are needed to modify the software's original code, errors may be introduced into the old code, or we will have to refactor the entire functionality and require the original code to be re-tested.

Solution: When the software needs to change, try to implement changes by extending the behavior of the software entities, rather than by modifying existing code.

The opening and closing principle is the most basic design principle in object-oriented design, and it guides how to establish a stable and flexible system. The open/Closed principle is probably the most ambiguous one in the six principles of the design pattern, it only tells us that opening to the extension, closing the modification, but how to open the extension, the modification is closed, and does not explicitly tell us. In the past, if someone told me "you must obey the opening and shutting principle when designing," I will feel that he did not say anything, but seems to have said everything. Because the opening and closing principle really taixu.

After careful consideration and careful reading of many design patterns, the principle of closure was finally recognized. In fact, we follow the design pattern in front of the 5 major principles, and the use of 23 design patterns is to follow the opening and closing principle. In other words, as long as we adhere to the previous 5 principles of good, the design of the software is naturally in line with the open and closed principle, the opening and closing principle is more like the front five principles adherence to the degree of "average score", the first 5 principles to abide by the good, average natural high, showing the software design closed-closed principle of good If the previous 5 principles are not well observed, then the open and closed principle is not good.

In fact, I believe that the opening and closing principle is nothing more than to express such a layer of meaning: with the abstract framework, with the implementation of extension details. because the abstract flexibility is good, adaptability is wide, as long as the abstract reasonable, can basically maintain the stability of the software architecture. And the variable details in the software, we use the abstract derived implementation class to expand, when the software needs to change, we just need to re-derive an implementation class to expand on demand. Of course, the premise is that our abstraction should be reasonable, to change the needs of the forward-looking and predictable.

Six rules for iOS design patterns

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.