Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 寻找合适的设计模式_Java_Android_Oop_Design Patterns - Fatal编程技术网

Java 寻找合适的设计模式

Java 寻找合适的设计模式,java,android,oop,design-patterns,Java,Android,Oop,Design Patterns,好的,由于服务器端响应配置不好,我很难找到有效的方法来编写代码。 由于我试图传达的示例相当复杂,我将尝试使用一个真实的示例来提供帮助 假设我负责一项体育奖励计划,我正试图创建一个“顶级运动员页面”。该页面将显示三项运动中的顶尖男女运动员;棒球、篮球和足球。一个转折点是,可能有一个男性赢家,而没有女性赢家,反之亦然,或者根本没有赢家。除此之外,女性只能是棒球运动员或篮球运动员,而男性可以是三人中的任何一人,也可以同时是篮球运动员和足球运动员。棒球与其他任何东西都没有结合。最后,如果同时存在男性和女

好的,由于服务器端响应配置不好,我很难找到有效的方法来编写代码。
由于我试图传达的示例相当复杂,我将尝试使用一个真实的示例来提供帮助

假设我负责一项体育奖励计划,我正试图创建一个“顶级运动员页面”。该页面将显示三项运动中的顶尖男女运动员;棒球、篮球和足球。一个转折点是,可能有一个男性赢家,而没有女性赢家,反之亦然,或者根本没有赢家。除此之外,女性只能是棒球运动员或篮球运动员,而男性可以是三人中的任何一人,也可以同时是篮球运动员和足球运动员。棒球与其他任何东西都没有结合。最后,如果同时存在男性和女性玩家,则必须首先显示女性。这三个类别都有不同的属性,比如足球会有“tds=43”属性,而棒球则有“本垒打=32”

因此,服务器响应会引起混淆:

<player>
  <baseballList>
   <baseball
    name="Adam"
    sex="male"
    HomeRuns="32"
    reward="True"/>
 </baseballList>
 <basketballList>
 <basketball
  name="John"
    sex="male"
    Points="322"
    reward="False"/>
  <basketball
   name="Sandra"
    sex="female"
    Points="332"
    reward="True"/>
  </basketballList>
  <footballList>
   <football
    name= doug
    Touchdowns= 33
    sex=male
    reward="false"/>
   </footballList>
</player>
我为对象创建了一个类文件:

public class Player implements Serializable{
  public String category;
  public String rewards;
  public String TouchDowns;
  public String Points;
  public String HomeRuns;
  public String sex;
  public String Name;   
}
我使用一个名为“playerSorter”的类和一个addPlayer()方法进行排序,该方法仅在满足指定条件的情况下填充列表,然后我有一个getPlayer()方法,该方法调用我的CheckForAthletWithIntwoSports()方法(查看是否有篮球和足球运动员)然后返回排序后的列表,首先显示女性(如果适用)。从我的主页调用getPlayers()方法,然后将其设置为适配器类。
更好的xml响应将使这样的任务变得更容易,但事实并非如此,我希望找到一种更有效的方法来实现这一点。如果有人能帮我找到一个好的设计模式来解决这个问题,或者给我一些建议,我会非常感激。(而且,这些类别的属性不仅仅是“本垒打、得分或触地得分”,只是试图简化。)

我不知道这里是否有一个特定的设计模式来解决您的问题;在我看来,您的模型缺少一些抽象,因为您主要使用字符串来表示域模型。这与OOP背道而驰,OOP的思想是用对象表示事物,这样您就可以将行为委托给对象。作为一个例子,考虑这段代码:

if (localName.equalsIgnoreCase("football"))
 player.category = "football"
 player.TouchDowns=attributes.getValue("TouchDowns");

else if (localName.equalsIgnoreCase("baseball"))
  player.HomeRuns=arrtibutes.getValue("HomeRun");
  player.category = "baseball"

else{
  player.category = "basketball";
  player.Points=attributes.getValue("Points");}
通过创建三个类来表示每项运动成绩(
football performance
baseball performance
basketball performance
),可以很容易地改进这一点,其中每个类都包含适用于它们的属性。一旦具备了这一点,就可以将XML节点的读取委托给类本身(请记住,我不是Java程序员,所以我将使用伪代码):

足球性能和棒球性能这两个类非常相似,它们采用了一组属性并基于这些属性进行填充。通过对
Player
类应用相同的思想,您还可以将对象创建分散到以下内容中:

public class PlayerHandler implements XmlHandler {

private static PlayerHandler handler = new PlayerHandler();

private PlayerHandler() {

}

public static PlayerHandler getInstance() {
    return handler;
}

public void load(String localName, String qName, Attributes attributes) {
    if (localName != null && attributes != null) {
        if       (localName.equalsIgnoreCase("football")||localName.equalsIgnoreCase("baseball")||localName.equalsIgnoreCase("basketball")) {
Player player = new Player();

if (localName.equalsIgnoreCase("football"))
 player.category = "football"
 player.TouchDowns=attributes.getValue("TouchDowns");

else if (localName.equalsIgnoreCase("baseball"))
  player.HomeRuns=arrtibutes.getValue("HomeRun");
  player.category = "baseball"

else{
  player.category = "basketball";
  player.Points=attributes.getValue("Points");}

  player.sex=attributes.getValue("sex");
  player.name=attributes.getValue("name");   
}
playerSorter.addPlayer(player);}}
public Sport createSportPerformanceInstance(String name, Attributes attributes) 
{
if (name.equalsIgnoreCase("football"))
    {return new BasketballPerformance(attributes);}
else 
if (name.equalsIgnoreCase("baseball"))
    {return new BaseballPerformance(attributes);}
...
}


public void load(String localName, String qName, Attributes attributes) 
{
SportPerformance sportPerformance = this.createSportPerformanceInstance(localName, attributes);
Player player = new Player(Attributes attributes);
player.sportPerformance = sportPerformance;
}
请注意,作为一个很好的副作用,如果以后添加一个新运动,则只需实现新类并在
createSportPerformanceInstance
方法中添加一个新分支,而不是跳入一个大型方法

稍后可以通过让
玩家
持有一组表演而不是一组,并让
玩家手柄
在创建新的玩家之前检查是否存在玩家来改进代码。新方法的外观如下所示:

public void load(String localName, String qName, Attributes attributes) 
{
SportPerformance sportPerformance = this.createSportPerformanceInstance(localName, attributes);
String playerName=attributes.getValue("name");
Player player;
  if (!this.playerExists(playerName)) 
  {
     player = new Player(attributes);
  } else 
     {
       player = this.getPlayerByName(playerName);
     }
  player.addPerformance(sportPerformance);
}
很好的一点是,现在您可以通过实现
Compariable
接口将排序顺序委托给球员自己,并且该模型也更适合您尝试建模的现实,因为您有一名球员在不同的运动中有不同的表现

话虽如此,你可能会从中找到一些灵感,特别是在,和


哇,我喜欢。有时,我最终会偏离更为OOP类型的方法=(。非常感谢您利用我的虚拟数据并花时间帮助我!很抱歉,我现在不能投票支持您(声誉不够)。@user1823974:没问题!很高兴帮助:)
public void load(String localName, String qName, Attributes attributes) 
{
SportPerformance sportPerformance = this.createSportPerformanceInstance(localName, attributes);
String playerName=attributes.getValue("name");
Player player;
  if (!this.playerExists(playerName)) 
  {
     player = new Player(attributes);
  } else 
     {
       player = this.getPlayerByName(playerName);
     }
  player.addPerformance(sportPerformance);
}