Design for the store - several classes of products

I read about Spring and came across an example consisting of an abstract product class with fields nameand price.

Next, there is a class Batterythat extends the class Productand adds a field rechargable. Then a class CDDrive(also) extending Product, but adding a field capacity.

In the real world, when we often have products that have many disparate attributes, how can we model arbitrary works with arbitrary properties and fields? Does the class for each product make sense?

So, can you please suggest a template to achieve this?

Thanks, Ouney

+4
source share
3 answers
. , GUI, , , . . - , . . -, , , . - . , , , . .

:

public abstract class Product {
 String name;
 Double price;
 Map<String, Object> propMap;

 public Product(String name, Double price) {
    this.name = name;
    this.price = price;
    propMap = new HashMap<>();
 }

 public void add2propMap(String key, Object value) {
    propMap.put(key, value);
 }

 public String toString() {
    return "Product [name=" + name + ", price=" + price + ", propMap=" + propMap + "]";
 }
}

CdDive:

public class CdDrive extends Product {

String capacity;

public CdDrive(String name, Double price, String capacity) {
    super(name, price);
    this.capacity = capacity;
}

}

:

public class Battery extends Product {

Boolean rechargable;

public Battery(String name, Double price, Boolean rechargable) {
    super(name, price);
    this.rechargable = rechargable;
}

}

:

public class Client {

public static void main(String[] args) {
    List<Product> productList = new ArrayList<>();

    Battery energizer = new Battery("Energizer", 12d, true);
    energizer.add2propMap("numInPackage", new Integer(8));
    energizer.add2propMap("make", "US");
    productList.add(energizer);

    CdDrive superDrive = new CdDrive("Apple Drive", 200d, "200 GB");
    superDrive.add2propMap("type", "External");
    superDrive.add2propMap("expandable", false);
    productList.add(superDrive);

    productList.forEach(p -> System.out.println(p));
}

}

:

Product [name=Energizer, price=12.0, propMap={numInPackage=8, make=US}]
Product [name=Apple Drive, price=200.0, propMap={expandable=false, type=External}]

, . , , . .

+1

?

. . , , , . , .

?

, Factory .

Product, .

, . .

, play. initialize startPlay .. , play .

public abstract class Game {
   abstract void initialize();
   abstract void startPlay();
   abstract void endPlay();

   //template method
   public final void play(){

      //initialize the game
      initialize();

      //start game
      startPlay();

      //end game
      endPlay();
   }
}

- , Product .

interface Product{
String NAME="defaultName";
Integer PRICE=5;
initialCost(); // example of a generic method
}

//Note that name & price if you declare those in interface will be treated as constants.

class Battery implements Product{
 Boolean rechargable =false; 
 public void initialCost(){
 //method definition
}
}

class CdDrive implements Product{
 Integer capacity = xxxx;
 public void initialCost(){
 //CdDrive method definition
}
}

Product product = new Battery();
Product nextProduct = new CdDrive();

. .

0

?

. , .

, - -, MD , - . ?

, . (, Product, PhysicallyDeliverableProduct, - . , )

, , - (, Map<String,Object>, ["rechargeable", true] , .

Design scheme? I think that what you are looking for is still far from having to use templates. Personally, I will suggest you take a look at Martin Fowler's book, An Analysis of the Picture. You may not be able to use the design in it directly, but it gives you the feeling that the lifestyle looks like

0
source

Source: https://habr.com/ru/post/1589333/


All Articles