3

I am fairly new to Design Patterns in programming, and I am trying to learn them by creating recipes and a meal. There can be many recipes but only one meal.

Using Builder:

Create a Recipe, which will contain a name, ingredients, etc.

Using Singleton:

Instantiate my CurrentMeal, which should contain an ArrayList<Recipe>. I also should be able to access a Recipe inside my CurrentMeal.

While I believe I understand Builder, I am not sure I understand Singleton all that well. Am I approaching this problem in an appropriate manner? If not, any suggestions as to an approach to take would be greatly appreciated. Thanks in advance.

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
Yehuda Gutstein
  • 381
  • 10
  • 27

2 Answers2

1

builder pattern:

Intent:

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

Key points:

  1. Builder pattern builds a complex object using simple objects and using a step by step approach
  2. A Builder class builds the final object step by step. This builder is independent of other objects
  3. Replacement to Factory method/Abstract Factory in this scenario : Too Many arguments to pass from client program to the Factory class that can be error prone
  4. Some of the parameters might be optional unlike in Factory which forces to send all parameters

UML diagram:

enter image description here

Builder:

Abstract interface for creating objects (product).

ConcreteBuilder:

Provides implementation for Builder. It is an object able to construct other objects. Constructs and assembles parts to build the objects

Guidelines for Builder design pattern in Java

  1. Make a static nested class called Builder inside the class whose object will be build by Builder
  2. Builder class will have exactly same set of fields as original class
  3. Builder class will expose method for adding ingredients. Each method will return same Builder object. Builder will be enriched with each method call.
  4. Builder.build() method will copy all builder field values into actual class and return object of Item class
  5. Item class (class for which we are creating Builder) should have private constructor to create its object from build() method and prevent outsider to access its constructor.

Refer to this journaldev article for more details.

Singleton pattern:

Intent:

  1. Ensure a class has only one instance, and provide a global point of access to it.
  2. Encapsulated "just-in-time initialization" or "initialization on first use"

singleton should be considered in below scenarios:

  1. Ownership of the single instance cannot be reasonably assigned
  2. Lazy initialization is desirable
  3. Global access is not otherwise provided for

UML diagram:

enter image description here

Have a look at below SE question for more details:

What is an efficient way to implement a singleton pattern in Java?

Community
  • 1
  • 1
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
0

The Singleton pattern allows you to share a single instance of an object. If you would like to share your CurrentMeal property, then you can of course encapsulate it in a Singleton, although I don't really see the point. It's not really what the pattern is designed for. You might use Singleton to implement a logging mechanism, for example.

The Builder pattern is well-suited to your application, as it allows different Recipe implementations to be constructed based on similar properties.

Paul Mooney
  • 1,576
  • 12
  • 28