Skip to content
Advertisement

Bridge pattern use and structural design patterns

I have a project where I am asked to include at least one structural design pattern, and I chose the a desktop app for food delivery, my thoughts where that I have an abstract class FoodElement that derives VeganFoodElement and a NormalFoodElement. Now for each category I can either choose a dish or a drink, can I separate them this way?

public abstract class FoodElement {

     private String elementName;
     private float elementPrice;

     public abstract void showFoodElement();
     public abstract float getPrice();

}

public class NormalFoodElement extends FoodElement{

    private TypeElementRepas elementNormal;

    @Override
    public void afficherElementRepas() {}

    @Override
    public float getPrix() {}
}

public interface TypeFoodElement {

    void showAttributes();
}

public class Drink implements TypeFoodElement {

    private int sugarQuantity=0;

    @Override
    public void showAttributes() {}

    @Override
    public void addSugar() {
       this.sugarQuantity += 1;
    }

}

public class Dish implements TypeFoodElement {

    @Override
    public void showAttributes() { }

}




Advertisement

Answer

I recommend that you choose the Decorator pattern. This pattern helps you to use some elements to make new elements. For Example new Seafood(new BasicFood()) .

You can read more here

And here an example

public interface Food {  
    public String prepareFood();  
    public double foodPrice();  
}// End of the Food interface.  

public class VegFood implements Food {  
    public String prepareFood(){  
         return "Veg Food";  
    }  
  
        public double foodPrice(){  
        return 50.0;  
    }  
}  

public abstract class FoodDecorator implements Food{  
    private Food newFood;  
    public FoodDecorator(Food newFood)  {  
        this.newFood=newFood;  
    }  
    @Override  
    public String prepareFood(){  
        return newFood.prepareFood();   
    }  
    public double foodPrice(){  
        return newFood.foodPrice();  
    }  
}  


public class NonVegFood extends FoodDecorator{    
    public NonVegFood(Food newFood) {  
        super(newFood);  
    }  
    public String prepareFood(){  
        return super.prepareFood() +" With Roasted Chiken and Chiken Curry  ";   
    }  
    public double foodPrice()   {  
        return super.foodPrice()+150.0;  
    }  
}  


public class ChineeseFood extends FoodDecorator{  
  public ChineeseFood(Food newFood)    {  
        super(newFood);  
  }  
    public String prepareFood(){  
        return super.prepareFood() +" With Fried Rice and Manchurian  ";   
    }  
    public double foodPrice()   {  
        return super.foodPrice()+65.0;  
        }  
}  



import java.io.BufferedReader;  
import java.io.IOException;  
import java.io.InputStreamReader;  
public class DecoratorPatternCustomer {  
    private static int  choice;  
    public static void main(String args[]) throws NumberFormatException, IOException    {  
       do{        
        System.out.print("========= Food Menu ============ n");  
        System.out.print("            1. Vegetarian Food.   n");  
        System.out.print("            2. Non-Vegetarian Food.n");  
        System.out.print("            3. Chineese Food.         n");  
        System.out.print("            4. Exit                        n");  
        System.out.print("Enter your choice: ");  
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));   
        choice=Integer.parseInt(br.readLine());  
        switch (choice) {  
        case 1:{   
                 VegFood vf=new VegFood();  
              System.out.println(vf.prepareFood());  
              System.out.println( vf.foodPrice());  
            }  
            break;  
              
                case 2:{  
                Food f1=new NonVegFood((Food) new VegFood());  
                    System.out.println(f1.prepareFood());  
                System.out.println( f1.foodPrice());  
        }  
            break;    
     case 3:{  
             Food f2=new ChineeseFood((Food) new VegFood());  
                     System.out.println(f2.prepareFood());  
                    System.out.println( f2.foodPrice());  
              }  
            break;    
              
         default:{    
            System.out.println("Other than these no food available");  
        }         
    return;  
     }//end of switch  
          
}while(choice!=4);  
    }  
}  
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement