【发布时间】:2016-09-17 03:52:19
【问题描述】:
我有一个模型类Team。我需要在 Coach 和 Admin 等不同类中对此类执行多项操作。我的问题是如何在创建所有其他类时维护一次相同的 Team 对象。??
在TestDriver 类中,我最初使用了团队对象来创建Coach。但是如果我想创建新的Admin,我需要传递相同的Team。这里有什么我需要遵循的模式吗?
//Model Classes
public class Player {
String playerName;
}
public class Team {
List<Player> playerList;
}
//Class to modify model
public class Coach {
Team team;
public Coach ( Team team) {
this.team = team;
}
public void deletePlayer(Player) {
//Remove the player form team
}
}
public class Admin {
Team team;
public Admin ( Team team) {
this.team = team;
}
public void addPlayer(Player) {
//Add the player to team
}
}
//Test Driver class
public class TestDriver {
public static void main(String args[]) {
Team team = new Team();
Coach coach = new Coach(team);
coach.deletePlayer(team);
//How to pass the same team
Admin admin = new Admin(???);
admin.addPlayer(team);
}
}
【问题讨论】: