How do I refactor my model?

Igor Filippov

I have object model, which describes an instructions for worker:

  • go to location
  • get an item
  • go to another location
  • pick another item
  • ........
  • finish job

In my app I need a list of this instructions to guide worker in his job.

The types of instructions are very different, that's why data which is required to describe instruction can be different. For changing location I just need the location id, but for getting item I also need an item id, quantity and item name, alongside with location id.

So my model looks like that:

public class Instruction{
    int locationId;
    int itemId;
    String itemName;
    String quanity;
    Type itemType; // can be Type.GET_ITEM, Type.CHANGE_LOCATION etc.
}

So I have the situation where I describe different entities with one model.

Should I live with that, or there's a way to eliminate duplication?

What I want is to keep the ability to store all instructions in a list, but to make models more clean.

jpkrohling

How about something like this?

interface Instruction {}

class LocationInstruction implements Instruction { 
    int locationId;
    public String toString() { return "Go to the location " + locationId; } ;
}

class ItemInstruction implements Instruction {
    Item item;
    public String toString() { return "Pick item " + item.toString(); } ;
}

class Item {
  int itemId;
  ...
  public String toString() { return "Item{itemId: "+itemId+"}" ;}
}

class Actions {
  public List<Instruction> getInstructions();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I refactor my model?

From Dev

How do I refactor this loop?

From Dev

How do I refactor this loop?

From Dev

How do I refactor unit tests?

From Dev

How do I refactor a ReadLine loop to Linq

From Dev

How do I refactor Swift in Xcode?

From Dev

How do I refactor these two functions?

From Dev

How do I refactor this Swagger API Spec

From Dev

How do I connect/relate my model to my view?

From Dev

How do I tell what my make and model of my computer is?

From Dev

How can I refactor my kotlin code and make it more clear

From Dev

How can I use the Factory pattern to refactor my Java code?

From Dev

How can I refactor my if-else statement?

From Dev

Ember.js: How do I associate my component with a model?

From Dev

How do I get mouse positions in my view model

From Dev

How do I get the model validation decorations output to my view?

From Dev

How do I format my AngularJS data model?

From Dev

How do I bind the value on the button to my ng-model?

From Dev

How do I load a batch of images to be processed by my tfjs model

From Dev

How do I bind my List model to View?

From Dev

How Do I Model My Results Using Java Collections?

From Dev

How do I get the model validation decorations output to my view?

From Dev

How do I change my model from the view in Angular?

From Dev

How do I get the model name of my processor?

From Dev

How do I write a regex in my Rails model validation?

From Java

Intellij IDEA 13: how do I disable refactor comments and strings?

From Dev

How do i refactor one JSON array into other format in JAVA?

From Dev

How do I refactor a recursion occurring in a for loop to make it a tail call?

From Dev

How do I refactor out GOTO statements in GWBASIC code?

Related Related

HotTag

Archive