我有一個模型類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);
}
}