Design principles of design patterns (medium)

Source: Internet
Author: User

Design principles of design patterns (medium)
Interface Segregation Principle, then multiple interfaces are used instead. Each interface serves only one submodule. This is similar to the single responsibility principle shared for the last time. Purpose of designing the interface isolation principle: when designing an application, if a module contains multiple sub-modules, We Should abstract the module. Imagine that this module is implemented by a class. We can abstract the system into an interface, but when we want to add a new module extension program, if the module to be added only contains some sub-modules of the original system, it forces us to implement all the methods of this interface. Such an interface is called a fat interface or a contaminated interface. We can generalize this definition into a single sentence, that is, to create a single interface, rather than a bloated interface. In other words, the interface should be refined as much as possible, and there should be as few methods as possible in the interface. You may be confused here. Isn't this the same as the single responsibility principle? Wrong, the interface isolation principle is different from the perspective of a single responsibility. A single responsibility requires a single responsibility for the class and interface, and focuses on the responsibility, which is a logical division of the business, the interface isolation principle requires that the interface methods should be as few as possible. For example, the responsibility of an interface may include 10 methods. These 10 methods are put in one interface and provided to multiple modules for access. Each module can access the interface according to the specified permissions, outside the system, the document restricts "do not use methods to access", which is allowed according to the principle of single responsibility and is not allowed according to the principle of interface isolation, because it requires "try to use multiple special interfaces", what does a special interface mean? It means that each module should be provided with a single interface, and several interfaces should be provided to several modules, rather than creating a large and bloated interface to accommodate access from all clients. The interface isolation principle is to standardize the interface, which contains the following four meanings: 1) the interface should be as small as possible. When splitting an interface based on the Interface isolation principle, you must first meet the single responsibility principle. 2) high cohesion of interfaces 3) Custom Service 4) interface design is to keep up with the same principle. We are also giving a simple example to illustrate the problem: copy the code public interface IWorker {void Work (); void Eat ();} public class Worker: IWorker {public void Work () {Console. writeLine ("to work");} public void Eat () {Console. writeLine ("dinner with customers");} copy the code class diagram as follows: from the code above, we can assume that the worker has to work and eat with the customer again, however, leaders do not need to work at the grass-roots level. They can eat with customers at ordinary times. In this case, for example, a leader class must implement this interface for both work and meals, this is unnecessary for the leadership class, so we need to modify the above interface: copy the Code 1 public interface IWorkable 2 {3 void Work (); 4} 5 6 public interface IFeeadable 7 {8 void Eat (); 9} 10 // The worker is working and has to accompany the customer to dinner 11 public class Workman: IWorker, IFeeadable12 {13 public void Work () 14 {15 Console. writeLine ("to work"); 16} 17 18 public void Eat () 19 {20 Console. writeLine ("dinner with customers"); 21} 22} 23 // lead to dinner with customers directly 24 public class Leader: IFeeadable25 {26 public void Eat () 27 {28 Console. writeLine ("dinner with customers"); 29} 30} copy the code class diagram as follows: The method has a lot of flexibility, so I will not elaborate on it. Liskov Substitution Principle, the Code still works normally. That is to say, when we design a program module, we will create a class hierarchy, and then we will create their subclasses by extending some classes, we must ensure that the replacement of the base class reference quilt class does not affect program functions, otherwise unexpected results will occur when we use them in existing program modules. Let's take a look at a piece of code: Copy code 1 public class Father 2 {3 public string type; 4 public Father () 5 {6 type = "parent class "; 7} 8 9 public void Method () 10 {11 Console. writeLine ("My parent class method"); 12} 13} 14 15 public class Sun: Father16 {17 public Sun () 18 {19 type = "subclass "; 20} 21 22 public void MyMethod () 23 {24 Console. writeLine ("I'm a subclass method"); 25} 26} copy the code class diagram as follows: client call code: Copy code 1 public static void DoSomthing (Father father) 2 {3 switc H (father. type) 4 {5 case "parent class": 6 father. method (); 7 break; 8 case "subclass": 9 (Sun) father ). myMethod (); 10 break; 11} 12} copy the code. We can see from the client call method that the call method is determined based on the input type. The disadvantage is that if there is a 100 subclass, this method will write 100 judgments, and more of them will enter the infinite judgment, so that the reading and maintenance of the code is very poor, then we will transform this method: copy code 1 public class Father 2 {3 public string type; 4 public Father () 5 {6 type = "parent class"; 7} 8 9 public virtual void Method () 10 {11 Console. writeLine ("I am a parent class"); 12} 13} 14 15 pu Blic class Sun: Father16 {17 public Sun () 18 {19 type = "subclass"; 20} 21 public override void Method () 22 {23 Console. writeLine ("I Am a subclass"); 24} 25} copy the code client call as follows: Copy code 1 public static void DoSomthing (Father f) 2 {3 f. method (); 4} 5 public static void DoSomthing (Sun f) 6 {7 f. method (); 8} copy the code. From the code above, you can see that the client does not call the Method based on judgment, and the subclass completely replaces the parent class Method.

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.