I'm relatively new to design patterns and in the following example I am using what I believe is a Strategy Pattern. However, I am repeating myself inside some, not all, concrete strategies and wondering is there a way to avoid this? Notice how ACommand and CCommand have the same code before doing something unique.
public interface Command
{
public boolean execute(CommandSender sender, String[] args);
public String getName();
//...
}
public abstract class PlayerCommand implements Command
{
protected BukkitPlugin plugin = BukkitPlugin.getInstance();
private String name;
//...
public PlayerCommand(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
//...
}
ACommand
public class ACommand extends PlayerCommand
{
public ACommand()
{
super("A");
}
public boolean execute(CommandSender sender, String[] args)
{
Player player = (Player) sender;
PlayerInventory inventory = player.getInventory();
ItemStack itemInHand = inventory.getItemInHand();
if(itemInHand.getType() != Material.COMPASS)
{
sender.sendMessage("You must be holding a phone to use this command");
return true;
}
int id = itemInHand.getDurability();
MobilePhoneManager phoneManager = plugin.getMobilePhoneManager();
boolean isMobilePhone = phoneManager.isMobilePhone(id);
if(!isMobilePhone)
{
sender.sendMessage("You must be holding a mobile phone to use this command");
return true;
}
//DO SOMETHING UNIQUE HERE
}
}
BCommand
public class BCommand extends PlayerCommand
{
public BCommand()
{
super("B");
}
public boolean execute(CommandSender sender, String[] args)
{
//SOMETHING ELSE
}
}
CCommand
public class CCommand extends PlayerCommand
{
public CCommand()
{
super("C");
}
public boolean execute(CommandSender sender, String[] args)
{
Player player = (Player) sender;
PlayerInventory inventory = player.getInventory();
ItemStack itemInHand = inventory.getItemInHand();
if(itemInHand.getType() != Material.COMPASS)
{
sender.sendMessage("You must be holding a phone to use this command");
return true;
}
int id = itemInHand.getDurability();
MobilePhoneManager phoneManager = plugin.getMobilePhoneManager();
boolean isMobilePhone = phoneManager.isMobilePhone(id);
if(!isMobilePhone)
{
sender.sendMessage("You must be holding a mobile phone to use this command");
return true;
}
//DO SOMETHING UNIQUE HERE
}
}