I've got a class Shop
which contains a collection of Item
objects. You can create a shop in two different ways:
- Create a shop filled with test data (for debug purposes);
- Create a shop by reading the data from file
I also need to write the shop back to file.
There is any pattern that can elegantly wrap this behavior?
This is the implementation right now (in Java):
class Item {
public final String name;
public Item(String n) {
name = n;
}
}
class Shop {
public List<Item> items;
private Shop() {
items = new LinkedList<>();
}
public static Shop generateTestData() {
Shop shop = new Shop();
shop.items.add(new Item("Product A"));
shop.items.add(new Item("Product B"));
shop.items.add(new Item("Product C"));
return shop;
}
public static Shop readFromFile(String filepath) {
// read from file
}
public static void writeToFile(Shop shop, String filepath) {
// write to file
}
}
N.B. I considered applying a sort of Prototype pattern in this way: I create a static instance of the Shop
filled with test data, then the user can get a copy of that and modify it as he/she wants. Is it a good idea?