Creating Object List in Java class - better approach -


a have question related creating object list in java class. can tell me solution better? pros , cons of it?

1) first version of class:

public class productrepositoryimpl implements productrepository {      private list<product> listofproducts = new arraylist<product>();      public productrepositoryimpl() {          addproducts(1, "silnik", "ferrari", 1000, listofproducts);         addproducts(2, "sprzęgło", "opel", 500, listofproducts);         addproducts(3, "kierownica", "fiat", 100, listofproducts);         addproducts(4, "panewka", "maluch", 250.00, listofproducts);         addproducts(5, "akumulator", "autosan", 1700.00, listofproducts);         addproducts(6, "zakrętka", "maseratii", 100.00, listofproducts);     }      private void addproducts(int idproduct, string name, string brand, double price, list list) {         product product = new product();         product.setid(idproduct);         product.setname(name);         product.setbrand(brand);         product.setprice(price);         list.add(product);     }      @override     public list<product> getlistofproducts() {             return listofproducts;     }   } 

2) , second one:

public class productrepositoryimpl implements productrepository {      private list<product> listofproducts = new arraylist<product>();      public productrepositoryimpl() {          product p1 = new product();         p1.setid(1);         p1.setname("silnik");         p1.setbrand("ferrari");         p1.setprice(1000);          product p2 = new product();         p2.setid(2);         p2.setname("hamulec");         p2.setbrand("opel");         p2.setprice(500);          product p3 = new product();         p3.setid(3);         p3.setname("kierownica");         p3.setbrand("fiat");         p3.setprice(100);          product p4 = new product();         p4.setid(4);         p4.setname("akumulator");         p4.setbrand("autosan");         p4.setprice(1700);          product p5 = new product();         p5.setid(5);         p5.setname("zakrętka");         p5.setbrand("maseratii");         p5.setprice(100);          listofproducts.add(p1);         listofproducts.add(p2);         listofproducts.add(p3);         listofproducts.add(p4);         listofproducts.add(p5);     }      @override     public list<product> getlistofproducts() {         return listofproducts;     } } 

i grateful every explaination.

there design guide lines in improving code :

  • separation of responsibilities. productrepositoryimpl should responsible dealing repository of products. not responsibility , should not have code constructing products. construction of products should responsibility of product
  • code reuse. version 1 better version 2 since code construction of product written once, inside addproducts(), rather multiple times in version 2.
  • encapsulation. interface of class part should public. it's implementation should hidden. means id field of product should not directly accesed outside, instead should accesed through interface methods. advantage being if later on need change how ids internally work can because ids come , go few methods in product class. if id field public there may hundreds of places accessing , dealing such change nightmare.

another issue mutability. product class mutable, implied setname method. if absolute requirement of application go ahead. if product not change once defined should rather make product inmutable. inmutability has several advantages, 1 of them being thread safe without needing synchronization. have made product inmutable.

with guide lines in mind approach take :

class product {     private final int idproduct;     private final string name;     private final string brand;     private final double price;      public product(int idproduct, string name, string brand, double price)     {         this.idproduct = idproduct;         this.name = name;         this.brand = brand;         this.price = price;     } }  public class productrepositoryimpl implements productrepository {     private list<product> listofproducts = new arraylist<product>();      public productrepositoryimpl()     {         addproduct(new product(1, "silnik", "ferrari", 1000));         addproduct(new product(2, "sprzęgło", "opel", 500));         addproduct(new product(3, "kierownica", "fiat", 100));         addproduct(new product(4, "panewka", "maluch", 250.00));         addproduct(new product(5, "akumulator", "autosan", 1700.00));         addproduct(new product(6, "zakrętka", "maseratii", 100.00));     }      private void addproduct(product product)     {         listofproducts.add(product);     }      @override     public list<product> getlistofproducts()     {         return listofproducts;     } } 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -