Dimit's rule (at least know the principle, only communicate with direct friends, reduce the coupling between classes)

There are two classes A class B

Direct friends:

B是A的 成员变量
B是A中 方法的参数
A的 方法返回值是B类型
The member variable is

class A{
    
    
	private B b;
}

Parameters in the method

class A{
    
    
	public void hello(B b){
    
    
	}
}

Method return type

class A{
    
    
	public B hello(){
    
    
	}
}

Unfamiliar class:

The unfamiliar class is equivalent to the tool class, like B is the tool class of A, but the class is used in the method

class A{
    
    
	public hello(){
    
    
		B b=new B(); //B是陌生的类
	}
}

Guess you like

Origin blog.csdn.net/qq_41813208/article/details/106651771
Recommended