Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

[Java] [Singleton Factory] The singleton pattern by itself is useful but it is r

ID: 3879753 • Letter: #

Question

[Java] [Singleton Factory]

The singleton pattern by itself is useful but it is really powerful is in combining it with other patterns. Let's assume that we design an application for the production of different types of chocolate bars (Wonka Bar, Oompaloompa Bar, etc.). Each type of chocolate bar has a unique ID. This unique ID is printed on the inside of the chocolate bar wrapper. Customers can check this ID online on and win a visit to the chocolate factory. Chocolate bars are being mass produced using multiple threads and a problem occurs when the factory is instantiated twice in 2 different threads as it is possible to have 2 overlapping ids for 2 different bars of the same type, which can lead to problems (such as customers using the chocolate factory). If the factory is implemented as a singleton we can avoid this problem.

Implement a “singleton factory” (from incomplete code) to create different types of chocolate bars with a unique ID. (Note different types of chocolate bars can have the same ID). Use different threads to produce the bars to check the correctness of you Singleton factory implementation.

incomplete code: drive.google.com/drive/folders/1X7JU7z4z1tfp8Ogy8U5SB5LO5JpNjeFs?usp=sharing

Explanation / Answer

Below new classes are created :

1.  

public class OompalampaBar extends Bar {

   public OompalampaBar(int identifier) {

       id = identifier;

}

  

}

2.

public class OompalampaBarFactory extends Bar {

private int counter=0;

  

   private static OompalampaBarFactory uniqueInstance; // = new WonkaBarFactory();

  

   private OompalampaBarFactory() {

   }

  

   public synchronized Bar create(int id) {

       Bar bar = new OompalampaBar(counter++);

       System.out.println(id+ " creates new Wonka bar created with id:" + counter);

       return bar;

   }

  

  

   public static synchronized OompalampaBarFactory getInstance() {

       if (uniqueInstance==null) {

           uniqueInstance= new OompalampaBarFactory();

       }

       return uniqueInstance;

   }

  

}

3.

public class OompalampaBarProducer extends Thread {

   Factory oompala;

   public int count=0; // each producer creates 50 bars

   public int id;

  

   public OompalampaBarProducer(int identifier) {

       id=identifier;

       System.out.println("creating new Bar Producer with ID:"+id);

   }

  

   public void run()

{    

       while (count<50) {

           try {

               sleep(100);

           } catch (InterruptedException e) {}

           count++;

           //System.out.println(id+":running");

           OompalampaBarFactory.getInstance().create(id);

       }

   }

  

}

and below are the changes required to make:

4.

public class SingletonfactoryTest {

   public static void main(String[] args) {

       WonkaBarProducer t1 = new WonkaBarProducer(1);

       WonkaBarProducer t2 = new WonkaBarProducer(2);

       OompalampaBarProducer t3 = new OompalampaBarProducer(3);

       OompalampaBarProducer t4 = new OompalampaBarProducer(4);

       t1.start();

       t2.start();

       t3.start();

       t4.start();

       //t1.interrupt();

       //t1.interrupt();

   }

}

here when you run the test class after above changes, in output you will get 100 wonka bars created by 2 threads but each thread has unique id for choclate bar. Also, 100 Oompaloompa bars created by 2 threads but again each thread has unique id for choclate bar, while wonka bar and oompoolampa bar has same unique id. Basically your partial implemented code is already working you just need to add new classes for new chocolate type.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote