[Solved] Concept/Design question: Alternatives to switch/conditional statements and Enums

Bob Jones Asks: Concept/Design question: Alternatives to switch/conditional statements and Enums
I am practicing design patterns and OO concepts such as inheritance in java and I’m writing an application that represents a vending machine.

I have two questions focused on ideal structure and design patterns:

  1. What is an alternative way to decide which treat from the vending machine to manufacture based on the passed name parameter of the treat object(see below there are three treat type classes derived from a treat superclass). I am currently using switch statements and conditional statements based on treat name.
  2. What is the best way to represent a list of potential variables for “brand name” of the treat. The brand names can be used by a salty treat or a sweet treat, but not by the healthy treat.

So I have a vending machine class that has a “treat” class that represents a superclass for all treats held within the machine.

The treat class has three types of objects that inherit from treat: “salty”, “sweet”, “healthy.”

Each of these types of objects will then have specific methods (like getIngredients()) and member variables (name, price) that are unique to those objects.

But I’d like to have a class that is merely a list of the brand names that can be a member variable of the salty and sweet treat objects. Healthy is different because it won’t have a brand — it will just be fruit.

For example, the Nabisco brand could be a member variable of two of the three treat types: “Oreos” (sweet), “Wheat thins” (salty).

Should I maybe use a command pattern to determine which treat to generate based on the name? I’d just like to avoid switch/conditionals if possible and use the best design patterns for each task. I’ve watched videos and have been reading tutorials on how to use patterns but I’m still unsure in these two cases which is best to use. I also can’t find a good example on stack exchange that deals with my issue there.

Should I try to use a strategy pattern to store this list of brands that has no other function but is simply an attribute stored within an object? Something like BrandStrategy –> assignBrand() –should brand even be stored in it’s own class? It seems like it should since two subclasses would use it.

Diagram of Program:

                                 FactoryMethod (Creates treat based on treat "name")
                                      Treat    (Abstract class)
                                   /    |    

                              Sweet   Salty   Healthy (Concrete classes: inherit from treat)
                                ^       ^
                                |       |       
                               Brand class (member variable of sweet & salty)

I have been reading about design patterns and best practices and I’ve been trying to avoid switch statements and conditional statements to generate specific objects and I’ve been reading that poor use of enums can create brittle code. I originally had “brand” stored as an enum but I’m realizing now that is a bad way to do it since that value is likely to change (basing this statement off of this post I researched).

Here are some of the stack exchange posts and tutorials I have read:

  1. What is the best way to structure a group of variables that have a group of variables that have a group of variables and so on?
  2. Refactoring Switch Statements and is there any real use for Switch Statements at all?
  3. Use Enum’s with caution – planetgeek.ch
  4. Switch statements are bad?
  5. Design Patterns in Java – Javatpoint
  6. Alternative to enum in design pattern

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.