package com.maom.design.P_5_Law_Of_Demeter;
/**
* @Title: P_5_Law_Of_Demeter
* @Description: 5_迪米特法则
* @Author maohb
* @Version 1.0
*/
public class P_5_Law_Of_Demeter {
public static void main(String[] args) {
SomeOne zhangsan = new SomeOne();
zhangsan.setFriend(new Friend());
zhangsan.setStranger(new StrangerA());
zhangsan.play();
}
}
class SomeOne {
private Friend friend;
private Stranger stranger;
public Stranger getStranger() {
return stranger;
}
public void setStranger(Stranger stranger) {
this.stranger = stranger;
}
public Friend getFriend() {
return friend;
}
public void setFriend(Friend friend) {
this.friend = friend;
}
public void play() {
System.out.println("someone play");
friend.play();
stranger.play();
}
}
class Friend {
public void play(){
System.out.println("friends play");
}
}
abstract class Stranger {
public abstract void play();
}
class StrangerA extends Stranger {
public void play() {
System.out.println("strangerA play");
}
}
设计模式笔记_5_迪米特法则
最新推荐文章于 2024-09-08 21:56:57 发布