Do I have a possibility to use Flyweight Pattern for this scenario

So I have a order management system CustomerOrder will have multiple food items and a Customer
22 Replies
JavaBot
JavaBot10mo ago
This post has been reserved for your question.
Hey @Rag...JN 🌌 🦡 👽 💰! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Rag...JN 🌌 🦡 👽 💰 🐊
public class CustomerOrder {
private Customer customer;
private List<AddedFoodItemDAO> orders;
public class CustomerOrder {
private Customer customer;
private List<AddedFoodItemDAO> orders;
public class AddedFoodItemDAO {
private String foodItemName;
private double price;
private int qty;
private CustomizedOptions customizedOptions;
public class AddedFoodItemDAO {
private String foodItemName;
private double price;
private int qty;
private CustomizedOptions customizedOptions;
Now the CustomizedOptions have multiple sub classes according to the food type so if the food name is Pizza, it will have CustomizedPizzaOptions, if the food name is Burger, it will have CustomizedBurgerOptions
public class CustomizedOptions {
private Map<String,Object> keysValue;

public Map<String, Object> getKeysValue() {
return keysValue;
}

CustomizedOptions(){
keysValue = new HashMap<>();
}
}
public class CustomizedOptions {
private Map<String,Object> keysValue;

public Map<String, Object> getKeysValue() {
return keysValue;
}

CustomizedOptions(){
keysValue = new HashMap<>();
}
}
Sub classes
public class CustomizedPizzaOptions extends CustomizedOptions {

private String crust;
private String size;
private String toppings1;
private String toppings2;
private int slices;
private String cheeseOptions;
private String description;
public class CustomizedPizzaOptions extends CustomizedOptions {

private String crust;
private String size;
private String toppings1;
private String toppings2;
private int slices;
private String cheeseOptions;
private String description;
public class CustomizedBurgerOptions extends CustomizedOptions{
private String cheeseOptions;
private String heatLevel;
private String specialIngredients;
private String vegetable1;
private String vegetable2;
public class CustomizedBurgerOptions extends CustomizedOptions{
private String cheeseOptions;
private String heatLevel;
private String specialIngredients;
private String vegetable1;
private String vegetable2;
Usage and building of an order
CustomizedOptions customizationOptions = new CustomizedPizzaOptions(crustType, size, topping1, topping2, slices, cheeseOptions, specialNote);
valueChanged.valueChanged(customizationOptions);
CustomizedOptions customizationOptions = new CustomizedPizzaOptions(crustType, size, topping1, topping2, slices, cheeseOptions, specialNote);
valueChanged.valueChanged(customizationOptions);
Once the CustomizedOptions is created here I added the values to the map too, ignore the UI implementation. the point is I added key and value pairs to the map here. Which is useful when you want to show details dynamically.
if (customizedOptions instanceof CustomizedPizzaOptions customizedPizzaOptions) {
panelList.add(new CustomizationLabel("crust", customizedPizzaOptions.getCrust()));
panelList.add(new CustomizationLabel("size", customizedPizzaOptions.getSize()));
panelList.add(new CustomizationLabel("topping1", customizedPizzaOptions.getToppings1()));
panelList.add(new CustomizationLabel("topping2", customizedPizzaOptions.getToppings2()));
panelList.add(new CustomizationLabel("slices", Integer.toString(customizedPizzaOptions.getSlices())));
panelList.add(new CustomizationLabel("cheese options", customizedPizzaOptions.getCheeseOptions()));
panelList.add(new CustomizationLabel("description", customizedPizzaOptions.getDescription()));
}

orderScrollPaneJPanel.setLayout(new GridLayout(panelList.size(), 1));
for (CustomizationLabel panel : panelList) {
orderScrollPaneJPanel.add(panel);
customizedOptions.getKeysValue().put(panel.getjLabel1().getText(), panel.getjLabel2().getText());
}
if (customizedOptions instanceof CustomizedPizzaOptions customizedPizzaOptions) {
panelList.add(new CustomizationLabel("crust", customizedPizzaOptions.getCrust()));
panelList.add(new CustomizationLabel("size", customizedPizzaOptions.getSize()));
panelList.add(new CustomizationLabel("topping1", customizedPizzaOptions.getToppings1()));
panelList.add(new CustomizationLabel("topping2", customizedPizzaOptions.getToppings2()));
panelList.add(new CustomizationLabel("slices", Integer.toString(customizedPizzaOptions.getSlices())));
panelList.add(new CustomizationLabel("cheese options", customizedPizzaOptions.getCheeseOptions()));
panelList.add(new CustomizationLabel("description", customizedPizzaOptions.getDescription()));
}

orderScrollPaneJPanel.setLayout(new GridLayout(panelList.size(), 1));
for (CustomizationLabel panel : panelList) {
orderScrollPaneJPanel.add(panel);
customizedOptions.getKeysValue().put(panel.getjLabel1().getText(), panel.getjLabel2().getText());
}
public CustomerOrder createOrder() {
List<AddedFoodItemDAO> addedFoodItemDAOS = new LinkedList<>();
for (Component component : myJPanel.getComponents()) {

if (component instanceof AddedFoodItem) {
AddedFoodItem foodItem = (AddedFoodItem) component;
if (foodItem.getCustomizedOptions() == null) {
return null;
}
addedFoodItemDAOS.add(new AddedFoodItemDAO(
foodItem.getFoodItemName(),foodItem.getPrice(),foodItem.getQty(),foodItem.getCustomizedOptions()));

}
}

return new CustomerOrder.Builder().orders(addedFoodItemDAOS).customer(customer).build();
}
public CustomerOrder createOrder() {
List<AddedFoodItemDAO> addedFoodItemDAOS = new LinkedList<>();
for (Component component : myJPanel.getComponents()) {

if (component instanceof AddedFoodItem) {
AddedFoodItem foodItem = (AddedFoodItem) component;
if (foodItem.getCustomizedOptions() == null) {
return null;
}
addedFoodItemDAOS.add(new AddedFoodItemDAO(
foodItem.getFoodItemName(),foodItem.getPrice(),foodItem.getQty(),foodItem.getCustomizedOptions()));

}
}

return new CustomerOrder.Builder().orders(addedFoodItemDAOS).customer(customer).build();
}
JavaBot
JavaBot10mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
dan1st
dan1st10mo ago
What specifically do you want to use the flyway pattern for here? and why?
Rag...JN 🌌 🦡 👽 💰 🐊
it goes with a Java Swing application Second question for the assignment is to apply Flyweight pattern for the application When choosing customized food options I revealed my solution in the general chat
dan1st
dan1st10mo ago
? I mean, you might be able to use it for the CustomizedOptions if you assume that the same CustomizedOptions typically occur multiple times
Rag...JN 🌌 🦡 👽 💰 🐊
yah when you open the same panel again and again
dan1st
dan1st10mo ago
then use it for the CustomizedOptions
Rag...JN 🌌 🦡 👽 💰 🐊
I shared the solution in the general, my search is not working
dan1st
dan1st10mo ago
you mean you already solved it?
john
john10mo ago
I guess I figured out the Interpreter Pattern for my application
dan1st
dan1st10mo ago
probably that thing but if you solved it, I don't need to help you I guess
Rag...JN 🌌 🦡 👽 💰 🐊
this is interpreter yah it went inactive so I didn't close it
john
john10mo ago
========== I implemetned the Flyweight pattern So I created a Factory
public class CustomizedOptionsFactory {
private static Map<String, CustomizedOptions> optionsCache = new HashMap<>();


public static CustomizedOptions createCustomizedOptions(String optionName){

if(!optionsCache.containsKey(optionName)){
optionName = optionName.toUpperCase().replace(" ","_");
if(FoodItemList.PIZZA.name().equals(optionName)){
optionsCache.put(optionName,new CustomizedPizzaOptions());
}
if(FoodItemList.PASTA.name().equals(optionName)){
optionsCache.put(optionName,new CustomizedPastaOptions());
}
if(FoodItemList.SALAD.name().equals(optionName)){
optionsCache.put(optionName,new CustomizedSaladOptions());
}
if(FoodItemList.BURGER.name().equals(optionName)){
optionsCache.put(optionName,new CustomizedBurgerOptions());
}if(FoodItemList.ICE_CREAM.name().equals(optionName)){
optionsCache.put(optionName,new CustomizedIceCreamOptions());
}
}
return optionsCache.get(optionName);
}
}
public class CustomizedOptionsFactory {
private static Map<String, CustomizedOptions> optionsCache = new HashMap<>();


public static CustomizedOptions createCustomizedOptions(String optionName){

if(!optionsCache.containsKey(optionName)){
optionName = optionName.toUpperCase().replace(" ","_");
if(FoodItemList.PIZZA.name().equals(optionName)){
optionsCache.put(optionName,new CustomizedPizzaOptions());
}
if(FoodItemList.PASTA.name().equals(optionName)){
optionsCache.put(optionName,new CustomizedPastaOptions());
}
if(FoodItemList.SALAD.name().equals(optionName)){
optionsCache.put(optionName,new CustomizedSaladOptions());
}
if(FoodItemList.BURGER.name().equals(optionName)){
optionsCache.put(optionName,new CustomizedBurgerOptions());
}if(FoodItemList.ICE_CREAM.name().equals(optionName)){
optionsCache.put(optionName,new CustomizedIceCreamOptions());
}
}
return optionsCache.get(optionName);
}
}
dan1st
dan1st10mo ago
it's fine, I just saw the thing in #help-notifications lol
Rag...JN 🌌 🦡 👽 💰 🐊
thank you for being kind enough to looking into the thread
JavaBot
JavaBot10mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
JavaBot
JavaBot10mo ago
Post Closed
This post has been closed by <@910524060771971103>.
Want results from more Discord servers?
Add your server