Skip to content

Commit f0e3671

Browse files
Changed the ProductCatalog's usage to follow the Singleton pattern.
By refactoring the class ProductCatalog as an implementation of the Singleton pattern, we're able to avoid any issues coming from the instantiation of the list of products, which is now made the first time someone uses any method from the class.
1 parent e5d3c48 commit f0e3671

File tree

10 files changed

+55
-26
lines changed

10 files changed

+55
-26
lines changed

src/main/java/coreapi/Menu.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ public List<Product> getProductsInMenu()
6666
*/
6767
public int getProductQuantity(int productId)
6868
{
69-
Product productToCheck = ProductCatalog.getProduct(productId);
69+
Product productToCheck = ProductCatalog.Instance().getProduct(productId);
7070
int productQuantity;
7171

7272
if (productsMenu.containsKey(productToCheck))
7373
{
74-
productQuantity = productsMenu.get(ProductCatalog.getProduct(productId)).intValue();
74+
productQuantity = productsMenu.get(ProductCatalog.Instance().getProduct(productId)).intValue();
7575
}
7676
else
7777
{

src/main/java/coreapi/OrderImpl.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public Map<Product,Integer> getBasket()
6464
*/
6565
public boolean containsProduct(int id)
6666
{
67-
return basket.containsKey(ProductCatalog.getProduct(id));
67+
return basket.containsKey(ProductCatalog.Instance().getProduct(id));
6868
}
6969

7070
/**
@@ -76,7 +76,7 @@ public int checkProductQuantity(int id)
7676

7777
if (this.containsProduct(id))
7878
{
79-
quantity = basket.get(ProductCatalog.getProduct(id)).intValue();
79+
quantity = basket.get(ProductCatalog.Instance().getProduct(id)).intValue();
8080
}
8181
else
8282
{
@@ -94,7 +94,7 @@ public void addProduct(int newProductId, int quantity)
9494
{
9595
if (quantity > 0)
9696
{
97-
Product prod = ProductCatalog.getProduct(newProductId);
97+
Product prod = ProductCatalog.Instance().getProduct(newProductId);
9898
if (this.containsProduct(newProductId))
9999
{
100100
int actualQuantity = basket.get(prod).intValue();
@@ -109,14 +109,14 @@ public void addProduct(int newProductId, int quantity)
109109

110110
public void removeProduct(int productId)
111111
{
112-
basket.remove(ProductCatalog.getProduct(productId));
112+
basket.remove(ProductCatalog.Instance().getProduct(productId));
113113
}
114114

115115
public void removeProduct(int productId, int quantity)
116116
{
117117
if (quantity > 0)
118118
{
119-
Product prod = ProductCatalog.getProduct(productId);
119+
Product prod = ProductCatalog.Instance().getProduct(productId);
120120
if (basket.get(prod).intValue() <= quantity)
121121
{
122122
this.removeProduct(productId);

src/main/java/coreapi/OrderService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public OrderService() {}
1717
*/
1818
public void addProductToOrder(Cafeteria coffe, OrderImpl ord, int productId, int q)
1919
{
20-
Product prod = ProductCatalog.getProduct(productId);
20+
Product prod = ProductCatalog.Instance().getProduct(productId);
2121
if(ord.containsProduct(productId))
2222
{
2323
throw new RuntimeException("This product is already in your basket, you can modify the quantity of it if you wish.");
@@ -45,7 +45,7 @@ public void addProductToOrder(Cafeteria coffe, OrderImpl ord, int productId, int
4545
*/
4646
public void modifyProductQuantity(Cafeteria coffe, OrderImpl ord, int productId, int q)
4747
{
48-
Product prod = ProductCatalog.getProduct(productId);
48+
Product prod = ProductCatalog.Instance().getProduct(productId);
4949

5050
if(coffe.productStock.containsKey(prod) && ord.containsProduct(productId))
5151
{

src/main/java/coreapi/ProductCatalog.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
1+
/**
2+
* The <code>ProductCatalog</code> class works as a placeholder for product
3+
* data representation, as it will come later pre-established as a file.
4+
* @author Borja
5+
* @version 0.2
6+
* @see Product
7+
*/
8+
19
package coreapi;
210

311

412
public class ProductCatalog {
513

614
private static Product[] products = new Product[10];
15+
private static ProductCatalog uniqueInstance;
716

8-
public static void createProducts()
17+
/**
18+
* Returns the unique existing instance of the <code>ProductCatalog</code> class,
19+
* through which the user is able to call its methods.
20+
* @return the only instance of the class.
21+
*/
22+
public static ProductCatalog Instance()
23+
{
24+
if (uniqueInstance == null)
25+
{
26+
uniqueInstance = new ProductCatalog();
27+
}
28+
return uniqueInstance;
29+
}
30+
/**
31+
* This private constructor initializes the list of products that serves as a
32+
* temporary placeholder for product information.
33+
*/
34+
private ProductCatalog()
935
{
1036
products[0] = new ProductImpl(0, (float) 1.2, "Patatas fritas");
1137
products[1] = new ProductImpl(1, (float) 1.7, "Bacon-queso-huevo");
@@ -18,8 +44,12 @@ public static void createProducts()
1844
products[8] = new ProductImpl(8, (float) 0.75, "Donut de chocolate");
1945
products[9] = new ProductImpl(9, (float) 1.4, "Sándwich de roquefort");
2046
}
21-
22-
public static Product getProduct(int id)
47+
/**
48+
* Returns the corresponding reference to a certain product, determined by its identifier.
49+
* @param id the product identifier number to check for.
50+
* @return the product corresponding to the id passed as a parameter.
51+
*/
52+
public Product getProduct(int id)
2353
{
2454
if (id <= 9 && id >= 0)
2555
{

src/test/java/coreapiTest/MenuTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
public class MenuTest
1616
{
1717
private Menu myMenu;
18-
private Product product1 = ProductCatalog.getProduct(0);
19-
private Product product2 = ProductCatalog.getProduct(1);
18+
private Product product1 = ProductCatalog.Instance().getProduct(0);
19+
private Product product2 = ProductCatalog.Instance().getProduct(1);
2020

2121
@Before
2222
public void setUp()

src/test/java/coreapiTest/OrderImplTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class OrderImplTest {
1414

1515
private OrderImpl myOrder;
1616

17-
private Product product1 = ProductCatalog.getProduct(0);
18-
private Product product2 = ProductCatalog.getProduct(1);
17+
private Product product1 = ProductCatalog.Instance().getProduct(0);
18+
private Product product2 = ProductCatalog.Instance().getProduct(1);
1919

2020
@Before
2121
public void setUp()

src/test/java/coreapiTest/OrderServiceTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ protected void setUp()
3535
ordSer = new OrderService();
3636

3737
// We introduce products from the catalog to the cafeteria with a certain stock
38-
Product1 = ProductCatalog.getProduct(0);
39-
Product2 = ProductCatalog.getProduct(1);
40-
Product3 = ProductCatalog.getProduct(2);
38+
Product1 = ProductCatalog.Instance().getProduct(0);
39+
Product2 = ProductCatalog.Instance().getProduct(1);
40+
Product3 = ProductCatalog.Instance().getProduct(2);
4141
coffe.ProductRegister(Product1, 10);
4242
coffe.ProductRegister(Product2, 8);
4343
coffe.ProductRegister(Product3, 30);

src/test/java/coreapiTest/ProductImplTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public class ProductImplTest
1818
@Before
1919
public void setUp()
2020
{
21-
Product1 = (ProductImpl) ProductCatalog.getProduct(0);
22-
Product2 = (ProductImpl) ProductCatalog.getProduct(1);
23-
Product3 = (ProductImpl) ProductCatalog.getProduct(2);
21+
Product1 = (ProductImpl) ProductCatalog.Instance().getProduct(0);
22+
Product2 = (ProductImpl) ProductCatalog.Instance().getProduct(1);
23+
Product3 = (ProductImpl) ProductCatalog.Instance().getProduct(2);
2424
}
2525

2626
@After

src/test/java/coreapiTest/ProductTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public class ProductTest
1616
@Before
1717
public void setUp()
1818
{
19-
Product1 = ProductCatalog.getProduct(3);
20-
Product2 = ProductCatalog.getProduct(4);
21-
Product3 = ProductCatalog.getProduct(5);
19+
Product1 = ProductCatalog.Instance().getProduct(3);
20+
Product2 = ProductCatalog.Instance().getProduct(4);
21+
Product3 = ProductCatalog.Instance().getProduct(5);
2222
}
2323

2424
@After

src/test/java/coreapiTest/TestRunner.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ public class TestRunner
1111
{
1212
public static void main(String[] args)
1313
{
14-
ProductCatalog.createProducts();
1514
Result result = JUnitCore.runClasses(CoffeeShopTestSuite.class);
1615
for(Failure failure : result.getFailures())
1716
{

0 commit comments

Comments
 (0)