装饰者模式说, 只是“将灵活的附加职责动态地附加到对象上”。
换句话说, 装饰器模式使用合成而不是继承来扩展对象在运行时的功能。
装饰器模式也称为包装器。
装饰图案的优势
- 它提供了比静态继承更大的灵活性。
- 因为可以通过对新类进行编码来进行更改, 所以可以增强对象的可扩展性。
- 它允许你从目标类开发一系列功能, 而不是将所有行为编码到对象中, 从而简化了编码。
装饰图案的用法
它用于:
- 当你想要透明, 动态地向对象添加职责而又不影响其他对象时。
- 当你想向对象添加职责时, 将来可能需要更改。
- 通过子类扩展功能不再可行。
用于装饰器模式的UML:
第1步:创建食物界面。
public interface Food {
public String prepareFood();
public double foodPrice();
}// End of the Food interface.
步骤2:创建一个VegFood类, 该类将实现Food接口并覆盖其所有方法。
public class VegFood implements Food {
public String prepareFood(){
return "Veg Food";
}
public double foodPrice(){
return 50.0;
}
}
第3步:创建一个FoodDecorator抽象类, 该类将实现Food接口并覆盖所有方法, 并且可以装饰更多食物。
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();
}
}
步骤4:创建NonVegFood具体类, 该类将扩展FoodDecorator类并覆盖所有方法。
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;
}
}
第5步:创建一个ChineeseFood具体类, 该类将扩展FoodDecorator类并覆盖所有方法。
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;
}
}
步骤6:创建一个DecoratorPatternCustomer类, 该类将使用Food接口来使用客户想要的食物类型(Decorates)。
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);
}
}
输出量
========= Food Menu ============
1. Vegetarian Food.
2. Non-Vegetarian Food.
3. Chineese Food.
4. Exit
Enter your choice: 1
Veg Food
50.0
========= Food Menu ============
1. Vegetarian Food.
2. Non-Vegetarian Food.
3. Chineese Food.
4. Exit
Enter your choice: 2
Veg Food With Roasted Chiken and Chiken Curry
200.0
========= Food Menu ============
1. Vegetarian Food.
2. Non-Vegetarian Food.
3. Chineese Food.
4. Exit
Enter your choice: 3
Veg Food With Fried Rice and Manchurian
115.0
========= Food Menu ============
1. Vegetarian Food.
2. Non-Vegetarian Food.
3. Chineese Food.
4. Exit
Enter your choice: 4
Other than these no food available
评论前必须登录!
注册