繁体   English   中英

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

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

}




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

你可以在这里阅读更多

这里有一个例子

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM