Skip to content
This repository has been archived by the owner on Feb 2, 2019. It is now read-only.

设计模式 迪米特法则 #44

Open
mySoul8012 opened this issue Dec 5, 2018 · 0 comments
Open

设计模式 迪米特法则 #44

mySoul8012 opened this issue Dec 5, 2018 · 0 comments

Comments

@mySoul8012
Copy link
Owner

设计模式 迪米特法则

只和朋友交流

Only talk to your immediate friends 只与直接的朋友通信。即每个对象都有耦合关系,对象之间有耦合。

定义老师类

public class Teacher{
	// 老师对学生发布命令,清点学生
	public void commond(GroupLeader groupLeader){
		List listGirls = new ArrayList();
		// 初始化学生
		for(int i = 0; i < 20; i++){
			listGirls.add(new Girl());
		}
		// 然后进行查询任务
		groupLeader.countGirls(listGirls);
	}
}

然后定义体育文员,清查学生

public class GroupLeader{
	// 查询数量
	public void countGirls(List listGirls){
		
	}
}

定义学生类

public class Girl{
	
}

最后定义场景

public class Client{
	public static void main(String[] args){
		Teacher teacher = new Teacher();
		// 发布命令
		teacher.commond(new GroupLeader());
	}
}

上方代码的问题,Teacher类有一个朋友类,即GroupLeader,并且Girl类出现在commond方法体内,不属于朋友类。

朋友类:出现在成员变量,方法的输入参数中的类称为成员朋友类,出现在方法内部的类不属于朋友类,

迪米缇法则 一个类,只和朋友交流。不能和非朋友交流。但是刚刚定义的commond于Girl类有交流,即声明了List数组,即与陌生的Girl类有交流
修改如下

修改后的老师类

public class Teacher{
	// 老师对学生发布命令
	public void commond(GroupLeader groupLeader){
		// 告诉体育委员进行清查任务
		groupLeader.countGirls();
	}
}

体育委员

public class GroupLeader{
	private List listGirls;
	// 将全班学生带入,通过此构造函数Girl产生联系
	public GroupLeader(List _listGirls){
		this.listGirls = _listGirls;
	}
	// 进行学生数量的清理
	public void countGirls(){
		System.out.println(" " + this.listGirls.size());
	}
}

定义场景

public class Client{
	public static void main(String[] args){
		List listGirls = new ArrayList();	// 创建一个群体列表
		// 对学生初始化
		for(int i = 0; i < 20; i++){
			listGirls.add(new Girl());
		}
		Teacher teacher = new Teacher();
		// 发布命令
		teacher.commond(new GroupLeader(listGirls))	
	}
}

总结, 类与类之间的关系是建立在类之间,一个方法中不要引入一个类中不存在的对象。

朋友间有距离

一个软件安装的过程

first定义第一步,second定义第二步,third定义第三 步。

public class Wizard{
	private Random rand = new Random();
	// 第一步
	public int first(){
	
	}
	// 第二步
	public int third(){
		
	}
	// 第三步
	public int third(){
	
	}
}

最后定义installSoftware

public class installSoftware{
	public void installWizard(Wizard wizard){
		int first = wizard.first();
		int second = wizard.second();
		int third = wizard.third();
	}
}

最后定义场景

public class Client{
	public static void main(String[] args){
		installSoftware invoker = new installSoftware();
		invoker.installWizard(new Wizard());
	}
}

根据迪米特法则,两个类之间知道的越少越好,Wizard类的太多方法被installSoftware使用,两者的关系过于亲密,修改后如下

public class Wizard{
	private Random rand = new Random();
	private int first(){
	}
	private int second(){
	}
	privaet int third(){
	}
	// 对外只提供了一个installWizard方法
	public void installWizard(){
		int first = this.first();
		int second = this.second();
		int third = this.third();	
	}
	
}
public class insatllSoftware{
	public void installWizard(Wizard wizard){
		wizard.installWizard();	// 两个类通过此方法连接
	}
}

场景类

public class Client{
	public static void main(String[] args){
		installSoftward invoker = new installSoftware();
		invoker.installWizard(new Wizard());
	}
}

是自己的就是自己的

如果一个方法放在本类中,即不增加类间关系,也不会对本类不产生负面影响,那就放置在本类中。

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant