Detailed explanation of the six principles of design patterns: interface isolation principle and Dimit's rule

One of the six principles of design patterns-interface isolation principle and Dimit's rule detailed explanation

1. Interface Segregation Principle (ISP)

  • concept

    Before separating the principle of interface isolation, we must first clarify 接口the concept of " ". There are two types of interfaces:

    1. Strength interface

      newTo generate an object through the key in Java is a description of the method characteristics of a certain type of thing, which is a logical abstraction

    2. Class interface

      InterfaceInterfaces strictly defined by keywords in Java , for example java.lang.Runtime is a thread interface

    There are two definitions of the interface isolation principle:

    1. The client should not rely on interfaces that he does not need
    2. The dependencies between classes should be established on the smallest interface
  • content

    The interface isolates the application scenario, only provides the methods needed by the caller, and shields the unnecessary methods

    Example:

    There is an order in an e-commerce system, and it is used in the user portal, external system and management platform, and the methods used in the three places are different. Let's see how to solve it through the following code

    User portal application interface:

    public interface OrderForProtal {
          
          
        public String getOrder();
    }
    

    External system application interface:

    public interface OrderForOtherSys {
          
          
        public void insertOrder();
    }
    

    Management platform application interface:

    public interface OrderForAdmin {
          
          
        public String getOrder();
        public void insertOrder();
        public void updateOrder();
        public void deleteOrder();
    }
    

    The Order class implements the above three interfaces:

    public class Order implements OrderForAdmin,OrderForOtherSys,OrderForProtal{
          
          
        //返给Protal
        public static OrderForProtal getOrderForProtal() {
          
          
            return new Order();
        }
        //返给OtherSystem
        public static OrderForOtherSys getOrderForOtherSys() {
          
          
            return new Order();
        }
        //返给Admin
        public static OrderForAdmin getOrderForAdmin() {
          
          
            return new Order();
        }
        @Override
        public String getOrder() {
          
          
            return "返回订单";
        }
        @Override
        public void insertOrder() {
          
          
            System.out.println("插入订单");
        }
        @Override
        public void updateOrder() {
          
          
            System.out.println("更新订单");
        }
        @Override
        public void deleteOrder() {
          
          
            System.out.println("删除订单");
        }
    }
    

    It is not difficult to find that the Order class can isolate different users through different interfaces through the above three interfaces

  • to sum up

    1. An interface only serves one module or business logic
    2. Only keep public methods needed in the business
    3. Try to modify the contaminated interface, if the risk of modification is high, you can use the adapter mode for conversion processing (will be updated later)
    4. The design of the interface varies from situation to situation, so dogma cannot be copied
    5. When designing an interface, careful planning is required. More experience and attempts should be made to determine the granularity of the interface during design. If the granularity of the interface is too small, it will cause the number of interfaces to increase sharply and increase the difficulty of development. If the granularity of the interface is large, it will reduce the flexibility of development and cannot provide customized services. , It also brings unexpected risks to the project

2. Law of Demeter (LoD)

  • concept

    Dimit's Law is also called the Least Knowledge Principle (LKP). In layman 's terms, an object should know as little as possible about other objects.

    It has many expressions, the most representative ones are as follows:

    1. As for your direct friend correspondence
    2. Don't talk to "strangers"
    3. Each software unit has minimal knowledge of other units, and this understanding is limited to those software units that are closely related to this unit
  • content

Insert picture description here

Next, sample code will be used to demonstrate the calling relationship of the above figure:

Someone:

public class Someone {
    
    
    public void call(Friend friend){
    
    
        friend.forward();
    }
}

friend:

public class Friend {
    
    
    //声明陌生实例,值得注意的是,这里用的是private修饰符
    private Stranger stranger = new Stranger();

    //调用
    public void forward(){
    
    
        stranger.strangerMethod();
    }

    public void friendMethod() {
    
    
        System.out.println("这是朋友自己的方法");
    }
}

stranger:

public class Stranger {
    
    
    public void strangerMethod(){
    
    
        System.out.println("这是陌生人的方法");
    }
}

It is not difficult to see that there is no direct connection between the Someone class and the Stranger class, but indirect access through the Friend class, which reduces the relationship between classes and reduces the coupling between classes

  • to sum up

    1. The core concept of Dimit’s Law is decoupling and weak coupling between classes. Only after weak coupling can the reuse rate of classes be increased.
    2. In design patterns, there are two design patterns that apply Dimit's law as follows:
      • Appearance mode
      • Intermediary model

Guess you like

Origin blog.csdn.net/weixin_44829930/article/details/109774568