桥梁模式使用和结构设计模式

Bridge pattern use and structural design patterns

我有一个项目,我被要求至少包含一个结构设计模式,我选择了一个用于送餐的桌面应用程序,我的想法是我有一个抽象的 class FoodElement 派生 VeganFoodElement和一个 NormalFoodElement。现在每个类别我可以选择一道菜或一杯饮料,我可以这样分开吗?

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() { }

}




我建议你选择Decorator模式。此模式可帮助您使用一些元素来制作新元素。例如 new Seafood(new BasicFood()) .

你可以阅读更多here

这里有一个例子

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);  
    }  
}