我正在写餐厅管理作业,它必须遵循MVC模式。我的Menu
模型有两部分:饮料菜单和食物菜单。
食物菜单分为三种不同类型的菜单:早餐、午餐和晚餐菜单。关于饮料菜单,它还分为两个不同的菜单:酒精和软饮料菜单。
每个菜单都有相同的CRUD操作,因此,我编写了一个父类,并从中扩展了五个模型,因此,只需要为父类编写一个方法,并更改方法的参数,它们将以相同的方式工作。
然而,我不知道我的结构是否一直遵循MVC结构?我应该在MVC结构的视图中编写异常处理,还是应该在控制器中编写?感谢您的支持。
我的晚餐模型:
public class DailyMenu implements Serializable {
private List<MenuItem> menuItemList;
public DailyMenu(List<MenuItem> menuItemList){
this.menuItemList = menuItemList;
}
public DailyMenu(){
this.menuItemList = new ArrayList<>();
}
public List<MenuItem> getMenuItemList() {
return menuItemList;
}
public void setMenuItemList(List<MenuItem> menuItemList) {
this.menuItemList = menuItemList;
}
}
菜单视图:
public class MenuView {
private Scanner scanner = new Scanner(System.in);
private DailyMenuServices dailyMenuServices = new DailyMenuServicesImpl();
public DailyMenu getMenuTypes(Menu menu){;
menu();
int option = Integer.parseInt(scanner.nextLine());
MenuTypes menuTypes = MenuTypes.get(option-1);
switch (menuTypes){
case FOODMENU -> {return getFoodMenuTypes(menu.getFoodMenu());}
case DRINKMENU -> {return getDrinkMenuTypes(menu.getDrinkMenu());}
default -> {return null;}
}
}
public DailyMenu getFoodMenuTypes(FoodMenu foodMenu){
foodMenu();
int option = Integer.parseInt(scanner.nextLine());
FoodMenuTypes foodMenuTypes = FoodMenuTypes.get(option-1);
switch (foodMenuTypes){
case BREAKFASTMENU -> {return foodMenu.getBreakfastMenu();}
case LUNCHMENU -> {return foodMenu.getLunchMenu();}
case DINNERMENU -> {return foodMenu.getDinnerMenu();}
default -> {return null;}
}
}
public DailyMenu getDrinkMenuTypes(DrinkMenu drinkMenu){
drinkMenu();
int option = Integer.parseInt(scanner.nextLine());
DrinkMenuTypes drinkMenuTypes = DrinkMenuTypes.get(option-1);
switch (drinkMenuTypes){
case ALCOHOL -> { return drinkMenu.getAlcohol();}
case SOFTDRINK -> {return drinkMenu.getSoftDrinks();}
default -> {return null;}
}
}
public MenuItem createMenuItem(){
MenuItem menuItem = new MenuItem();
System.out.print("n Insert food name: ");
menuItem.setNames(scanner.nextLine());
System.out.print(" Insert food description: ");
menuItem.setDescription(scanner.nextLine());
System.out.print(" Insert food image: ");
menuItem.setImage(scanner.nextLine());
System.out.print(" Insert food price: ");
menuItem.setPrice(Float.parseFloat(scanner.nextLine()));
return menuItem;
}
public String getMenuItemName(){
System.out.print("Insert Menu item you want to change: ");
return scanner.nextLine();
}
public static void menu(){
System.out.println("n 1. Food Menu");
System.out.println(" 2. Drink Menu");
System.out.print(" Choose your option:");
}
public static void drinkMenu(){
System.out.println("1. Alcohol Menu");
System.out.println("2. Soft drink Menu");
System.out.print("Choose which menu you want to make change:");
}
public static void foodMenu(){
System.out.println("n1. Breakfast Menu");
System.out.println("2. Lunch Menu");
System.out.println("3. Dinner Menu");
System.out.print("nChoose which menu you want to add:");
}
}
我的菜单服务:
public class DailyMenuServicesImpl implements DailyMenuServices {
@Override
public void addMenuItemsToMenu(DailyMenu dailyMenu,MenuItem menuItem) {
List<MenuItem> menuItemList = dailyMenu.getMenuItemList();
menuItemList.add(menuItem);
}
@Override
public void updateMenu(DailyMenu dailyMenu,MenuItem updateMenuItem,String itemName) {
dailyMenu.getMenuItemList().forEach((MenuItem menuItem)->{
if(menuItem.getNames().equals(itemName)){
dailyMenu.getMenuItemList().set(dailyMenu.getMenuItemList().indexOf(menuItem),updateMenuItem);
}
});
}
@Override
public void deleteMenu(DailyMenu dailyMenu,String itemName) {
dailyMenu.getMenuItemList().removeIf(menuItem ->
menuItem.getNames().equals(itemName));
}
}
我的主要方法:
public class Main {
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
int option;
Menu menu = new Menu();
Bill bill = new Bill();
while (true){
System.out.println("n1.Menu management");
System.out.println("2.Bill management");
System.out.print("Please choose which types Management you want to work with:");
option = Integer.parseInt(scanner.nextLine());
ManagementTypes types = ManagementTypes.get(option-1);
switch (types){
case MENU -> menuMain(menu);
case BILL -> billMain(bill,menu);
default -> {}
}
}
}
public static void billMain(Bill bill, Menu menu) throws IOException{
int option = 0;
BillServices billServices = new BillServicesImpl();
BillPrinter billPrinter = new BillPrinterImpl();
BillFileHandlingServices billFileHandlingServices = new BillFileHandlingServicesImpl();
MenuView menuView = new MenuView();
BillView billView = new BillView();
MenuPrinter menuPrinter = new MenuPrinterImpl();
try {
while (option != 7){
System.out.println("nBill management");
System.out.println("1. Create Bill ");
System.out.println("2. Update Bill ");
System.out.println("3. Delete Bill");
System.out.println("4. Show Bill");
System.out.println("5. Export bill");
System.out.println("6. Import bill");
System.out.println("7. To escape");
System.out.print("nChoose your option:");
option = Integer.parseInt(scanner.nextLine());
ManagementActions actions = ManagementActions.get(option-1);
switch (actions){
case CREATE -> {
menuPrinter.printMenu(menu);
DailyMenu dailyMenu = menuView.getMenuTypes(menu);
menuPrinter.printMenu(dailyMenu);
BillItems billItems = billView.createBillItem(dailyMenu);
billServices.addBill(bill,billItems);
}
case UPDATE -> {
billPrinter.printBill(bill);
DailyMenu dailyMenu = menuView.getMenuTypes(menu);
BillItems billItems = billView.createBillItem(dailyMenu);
String itemName = billView.getBillItemName();
billServices.updateBill(bill,billItems,itemName);
}
case DELETE -> {
billPrinter.printBill(bill);
String itemName = billView.getBillItemName();
billServices.deleteBillItems(bill,itemName);
}
case SHOW -> billPrinter.printBill(bill);
case EXPORT -> billFileHandlingServices.exportBill(bill);
case IMPORT -> {
Bill importBill = billFileHandlingServices.importBill();
billPrinter.printBill(importBill);
}
case BREAK -> {}
}
}
}catch (NullPointerException | IllegalStateException | InputMismatchException | NumberFormatException exception){
System.out.println(exception.getMessage());
}
}
public static void menuMain(Menu menu) throws IOException{
int option = 0;
DailyMenuServices dailyMenuServices = new DailyMenuServicesImpl();
MenuView menuView = new MenuView();
MenuPrinter menuPrinter = new MenuPrinterImpl();
MenuFileHandlingServices menuFileHandlingServices = new MenuFileHandlingServicesImpl();
try {
while (option != 7) {
System.out.println("nMenu management");
System.out.println("1. Create Menu Items");
System.out.println("2. Update Menu Items");
System.out.println("3. Delete Menu Items");
System.out.println("4. Show Menu Items");
System.out.println("5. Export Menu");
System.out.println("6. Import Menu");
System.out.println("7. To escape");
System.out.print("nChoose your option:");
option = Integer.parseInt(scanner.nextLine());
ManagementActions actions = ManagementActions.get(option - 1);
switch (actions) {
case CREATE -> {
MenuItem menuItem = menuView.createMenuItem();
DailyMenu dailyMenu = menuView.getMenuTypes(menu);
dailyMenuServices.addMenuItemsToMenu(dailyMenu,menuItem);
}
case UPDATE -> {
menuPrinter.printMenu(menu);
MenuItem menuItem = menuView.createMenuItem();
DailyMenu dailyMenu = menuView.getMenuTypes(menu);
menuPrinter.printMenu(dailyMenu);
String itemName = menuView.getMenuItemName();
dailyMenuServices.updateMenu(dailyMenu,menuItem,itemName);
}
case DELETE -> {
menuPrinter.printMenu(menu);
DailyMenu dailyMenu = menuView.getMenuTypes(menu);
menuPrinter.printMenu(dailyMenu);
String itemName = menuView.getMenuItemName();
dailyMenuServices.deleteMenu(dailyMenu,itemName);
}
case SHOW -> menuPrinter.printMenu(menu);
case EXPORT -> menuFileHandlingServices.exportMenu(menu);
case IMPORT -> {
Menu importMenu = menuFileHandlingServices.importMenu();
menuPrinter.printMenu(importMenu);
}
case BREAK -> {}
}
}
}catch (NullPointerException | IllegalStateException | NumberFormatException exception){
System.out.println(exception.getMessage());
}
}
}
编辑:
主要类别:
public static void menuMain(Menu menu) throws IOException{
int option = 0;
MenuControllers menuControllers = new MenuControllers();
MenuPrinter menuPrinter = MenuPrinterImpl.getInstance();
try {
while (option != 7) {
menu();
option = Integer.parseInt(scanner.nextLine());
ManagementActions actions = ManagementActions.get(option - 1);
switch (actions) {
case CREATE -> menuControllers.add(menu);
case UPDATE ->menuControllers.update(menu);
case DELETE -> menuControllers.delete(menu);
case SHOW -> menuControllers.showMenu(menu);
case EXPORT -> menuControllers.exportFile(menu);
case IMPORT -> {
Menu importMenu = menuControllers.importFile();
menuControllers.showMenu(importMenu);
}
case BREAK -> {}
}
}
}catch (NullPointerException | IllegalStateException | NumberFormatException exception){
System.out.println(exception.getMessage());
}
}
public static void menu(){
System.out.println("nMenu management");
System.out.println("1. Create Menu Items");
System.out.println("2. Update Menu Items");
System.out.println("3. Delete Menu Items");
System.out.println("4. Show Menu Items");
System.out.println("5. Export Menu");
System.out.println("6. Import Menu");
System.out.println("7. To escape");
System.out.print("nChoose your option:");
}
我的菜单控制器:
public class MenuControllers {
private final MenuView view;
private final DailyMenuServices dailyMenuServices;
private final MenuFileHandlingServices menuFileHandlingServices ;
public MenuControllers(){
this.view = MenuView.getInstance();
this.dailyMenuServices = DailyMenuServicesImpl.getInstance();
this.menuFileHandlingServices = MenuFileHandlingServicesImpl.getInstance();
}
public void add(Menu menu){
MenuItem menuItem = view.createMenuItem();
int option = view.getMenuTypes();
MenuTypes menuTypes = MenuTypes.get(option-1);
switch (menuTypes){
case FOODMENU -> addToFoodMenu(menu.getFoodMenu(),menuItem);
case DRINKMENU -> addToDrinkMenu(menu.getDrinkMenu(),menuItem);
}
}
public void addToDrinkMenu(DrinkMenu drinkMenu, MenuItem menuItem){
int option = view.getDrinkMenuTypes();
DrinkMenuTypes drinkMenuTypes = DrinkMenuTypes.get(option-1);
switch (drinkMenuTypes){
case ALCOHOL -> dailyMenuServices.addMenuItemsToMenu(drinkMenu.getAlcohol(),menuItem);
case SOFTDRINK -> dailyMenuServices.addMenuItemsToMenu(drinkMenu.getSoftDrinks(),menuItem);
}
}
public void addToFoodMenu(FoodMenu foodMenu, MenuItem menuItem){
int option = view.getFoodMenuTypes();
FoodMenuTypes foodMenuTypes = FoodMenuTypes.get(option-1);
switch (foodMenuTypes){
case BREAKFASTMENU -> dailyMenuServices.addMenuItemsToMenu(foodMenu.getBreakfastMenu(),menuItem);
case LUNCHMENU -> dailyMenuServices.addMenuItemsToMenu(foodMenu.getLunchMenu(),menuItem);
case DINNERMENU -> dailyMenuServices.addMenuItemsToMenu(foodMenu.getDinnerMenu(),menuItem);
}
}
public void update(Menu menu){
view.printMenu(menu);
String foodName = view.getMenuItemName();
MenuItem menuItem = view.createMenuItem();
int option = view.getMenuTypes();
MenuTypes menuTypes = MenuTypes.get(option-1);
switch (menuTypes){
case FOODMENU -> updateToFoodMenu(menu.getFoodMenu(),menuItem,foodName);
case DRINKMENU -> updateToDrinkMenu(menu.getDrinkMenu(),menuItem,foodName);
}
}
public void updateToDrinkMenu(DrinkMenu drinkMenu, MenuItem menuItem,String foodName){
int option = view.getDrinkMenuTypes();
DrinkMenuTypes drinkMenuTypes = DrinkMenuTypes.get(option-1);
switch (drinkMenuTypes){
case ALCOHOL -> dailyMenuServices.updateMenu(drinkMenu.getAlcohol(),menuItem,foodName);
case SOFTDRINK -> dailyMenuServices.updateMenu(drinkMenu.getSoftDrinks(),menuItem,foodName);
}
}
public void updateToFoodMenu(FoodMenu foodMenu, MenuItem menuItem,String foodName){
int option = view.getFoodMenuTypes();
FoodMenuTypes foodMenuTypes = FoodMenuTypes.get(option-1);
switch (foodMenuTypes){
case BREAKFASTMENU -> dailyMenuServices.updateMenu(foodMenu.getBreakfastMenu(),menuItem,foodName);
case LUNCHMENU -> dailyMenuServices.updateMenu(foodMenu.getLunchMenu(),menuItem,foodName);
case DINNERMENU -> dailyMenuServices.updateMenu(foodMenu.getDinnerMenu(),menuItem,foodName);
}
}
public void delete(Menu menu){
view.printMenu(menu);
String foodName = view.getMenuItemName();
int option = view.getMenuTypes();
MenuTypes menuTypes = MenuTypes.get(option-1);
switch (menuTypes){
case FOODMENU -> deleteToFoodMenu(menu.getFoodMenu(),foodName);
case DRINKMENU -> deleteToDrinkMenu(menu.getDrinkMenu(),foodName);
}
}
public void deleteToDrinkMenu(DrinkMenu drinkMenu,String foodName){
int option = view.getDrinkMenuTypes();
DrinkMenuTypes drinkMenuTypes = DrinkMenuTypes.get(option-1);
switch (drinkMenuTypes){
case ALCOHOL -> dailyMenuServices.deleteMenu(drinkMenu.getAlcohol(),foodName);
case SOFTDRINK -> dailyMenuServices.deleteMenu(drinkMenu.getSoftDrinks(),foodName);
}
}
public void deleteToFoodMenu(FoodMenu foodMenu,String foodName){
int option = view.getFoodMenuTypes();
FoodMenuTypes foodMenuTypes = FoodMenuTypes.get(option-1);
switch (foodMenuTypes){
case BREAKFASTMENU -> dailyMenuServices.deleteMenu(foodMenu.getBreakfastMenu(),foodName);
case LUNCHMENU -> dailyMenuServices.deleteMenu(foodMenu.getLunchMenu(),foodName);
case DINNERMENU -> dailyMenuServices.deleteMenu(foodMenu.getDinnerMenu(),foodName);
}
}
public void showMenu(Menu menu){
view.printMenu(menu);
}
public void exportFile(Menu menu) throws IOException {
menuFileHandlingServices.exportMenu(menu);
}
public Menu importFile() throws IOException {
return menuFileHandlingServices.importMenu();
}
}
我的菜单视图:
public class MenuView {
private Scanner scanner = new Scanner(System.in);
private final MenuPrinter menuPrinter = MenuPrinterImpl.getInstance();
private MenuView(){ }
public static MenuView getInstance(){
return MenuViewHelper.menuView;
}
public static class MenuViewHelper{
private static final MenuView menuView = new MenuView();
}
public int getMenuTypes(){
menu();
return Integer.parseInt(scanner.nextLine());
}
public int getFoodMenuTypes(){
foodMenu();
return Integer.parseInt(scanner.nextLine());
}
public int getDrinkMenuTypes(){
drinkMenu();
return Integer.parseInt(scanner.nextLine());
}
public MenuItem createMenuItem(){
MenuItem menuItem = new MenuItem();
System.out.print("n Insert food name: ");
menuItem.setNames(scanner.nextLine());
System.out.print(" Insert food description: ");
menuItem.setDescription(scanner.nextLine());
System.out.print(" Insert food image: ");
menuItem.setImage(scanner.nextLine());
System.out.print(" Insert food price: ");
menuItem.setPrice(Float.parseFloat(scanner.nextLine()));
return menuItem;
}
public String getMenuItemName(){
System.out.print("Insert Menu item you want to change: ");
return scanner.nextLine();
}
public void printMenu(Menu menu){
menuPrinter.printMenu(menu);
}
public static void menu(){
System.out.println("n 1. Food Menu");
System.out.println(" 2. Drink Menu");
System.out.print(" Choose your option:");
}
public static void drinkMenu(){
System.out.println("1. Alcohol Menu");
System.out.println("2. Soft drink Menu");
System.out.print("Choose which menu you want to use:");
}
public static void foodMenu(){
System.out.println("n1. Breakfast Menu");
System.out.println("2. Lunch Menu");
System.out.println("3. Dinner Menu");
System.out.print("nChoose which menu you want to use:");
}
}
我的新菜单服务:
public class DailyMenuServicesImpl implements DailyMenuServices {
private DailyMenuServicesImpl(){}
public static DailyMenuServicesImpl getInstance(){
return DailyMenuServicesImplHelper.dailyMenuServicesImpl;
}
private static class DailyMenuServicesImplHelper{
private static final DailyMenuServicesImpl dailyMenuServicesImpl = new DailyMenuServicesImpl();
}
@Override
public void addMenuItemsToMenu(DailyMenu dailyMenu,MenuItem menuItem) {
List<MenuItem> menuItemList = dailyMenu.getMenuItemList();
menuItemList.add(menuItem);
}
@Override
public void updateMenu(DailyMenu dailyMenu,MenuItem updateMenuItem,String itemName) {
dailyMenu.getMenuItemList().stream()
.filter(menuItem -> menuItem.getNames().equals(itemName))
.findFirst()
.ifPresentOrElse(menuItem -> {
menuItem.setNames(updateMenuItem.getNames());
menuItem.setPrice(updateMenuItem.getPrice());
menuItem.setDescription(updateMenuItem.getDescription());
menuItem.setImage(updateMenuItem.getImage());
},()->{
throw new NullPointerException("Wrong menu Item name !!!");
});
}
@Override
public void deleteMenu(DailyMenu dailyMenu,String itemName) {
dailyMenu.getMenuItemList().removeIf(menuItem ->
menuItem.getNames().equals(itemName));
}
}
MVC(Mmodel-Vview-C控制器)是一种将数据建模、视图渲染和操作控制分离开来的模式。三者的任务是什么:
模型:它负责准确地表示数据view:负责与用户的交互
- 存在UI时的UI渲染或CLI情况下的控制台消息
- 事件处理
- 输入解析控制器:它负责理解用户命令的含义并调用正确的行为(操作)
因此,为了确定您的代码是否符合MVC模式,我们需要阅读您的模型、视图和控制器代码,看看它们在多大程度上满足了上述要求。
从纯MVC模式的角度来看,您的Model已经满足了标准,但您的view内部有一些业务逻辑,这些逻辑更适合控制器。让我们看几个例子:
public void updateMenuTypes(Menu menu){
menu();
int option = Integer.parseInt(scanner.nextLine());
MenuTypes menuTypes = MenuTypes.get(option-1);
System.out.println("Insert Menu item you want to change: ");
String itemName = scanner.nextLine();
MenuItem menuItem = createMenuItem();
switch (menuTypes){
case FOODMENU -> updateFoodMenuTypes(menu.getFoodMenu(),menuItem,itemName);
case DRINKMENU -> updateDrinkMenuTypes(menu.getDrinkMenu(),menuItem,itemName);
}
}
public void updateFoodMenuTypes(FoodMenu foodMenu,MenuItem menuItem,String itemName){
foodMenu();
int option = Integer.parseInt(scanner.nextLine());
FoodMenuTypes foodMenuTypes = FoodMenuTypes.get(option-1);
switch (foodMenuTypes){
case BREAKFASTMENU -> dailyMenuServices.updateMenu(foodMenu.getBreakfastMenu(),menuItem,itemName);
case LUNCHMENU -> dailyMenuServices.updateMenu(foodMenu.getLunchMenu(),menuItem,itemName);
case DINNERMENU -> dailyMenuServices.updateMenu(foodMenu.getDinnerMenu(),menuItem,itemName);
}
}
public void updateDrinkMenuTypes(DrinkMenu drinkMenu,MenuItem menuItem, String itemName){
drinkMenu();
int option = Integer.parseInt(scanner.nextLine());
DrinkMenuTypes drinkMenuTypes = DrinkMenuTypes.get(option-1);
switch (drinkMenuTypes){
case ALCOHOL -> dailyMenuServices.updateMenu(drinkMenu.getAlcohol(),menuItem,itemName);
case SOFTDRINK -> dailyMenuServices.updateMenu(drinkMenu.getSoftDrinks(),menuItem,itemName);
}
}
重复出现的模式如下:
- 调用菜单方法(分别为
menu
、foodmenu
或drinkmenu
) - 菜单方法向用户询问问题
- 你的观点决定了该怎么称呼
上面的前两个是View的有效部分,因为它们处理UI(也就是说,它们向用户提问并获得输入),但决定调用什么的逻辑更适合Controller部分。例如,此部分:
FoodMenuTypes foodMenuTypes = FoodMenuTypes.get(option-1);
switch (foodMenuTypes){
case BREAKFASTMENU -> dailyMenuServices.updateMenu(foodMenu.getBreakfastMenu(),menuItem,itemName);
case LUNCHMENU -> dailyMenuServices.updateMenu(foodMenu.getLunchMenu(),menuItem,itemName);
case DINNERMENU -> dailyMenuServices.updateMenu(foodMenu.getDinnerMenu(),menuItem,itemName);
}
是处理菜单更新的业务逻辑,您应该将此逻辑移动到控制器方法中。其思想是将业务逻辑与视图呈现分离。想象一下,在某个时候,您需要为这个项目编写一个Swing UI,并且需要完全重写与您的视图相关的任何内容。如果在视图代码中您有业务逻辑,那么您将很难做到这一点。如果您的视图代码与控制器代码正确分离,那么在这种情况下,您只需要查看控制器如何从视图接收输入,并重写视图(且仅重写视图),使其符合控制器的期望。
您还可以在控制器中混淆视图和控制器,即在
@Override
public void updateMenu(DailyMenu dailyMenu,MenuItem updateMenuItem,String itemName) {
menuPrinter.printMenu(dailyMenu);
dailyMenu.getMenuItemList().forEach((MenuItem menuItem)->{
if(menuItem.getNames().equals(itemName)){
dailyMenu.getMenuItemList().set(dailyMenu.getMenuItemList().indexOf(menuItem),updateMenuItem);
}
});
}
对menuPrinter.printMenu(dailyMenu);
的调用显然是视图渲染,如果您想应用MVC模式,最好在控制器完成工作后在视图内进行调用。在控制器完成工作之前最好不要显示主菜单(除非你有充分的理由),因为你的命令可能会失败,但如果用户被要求执行下一步操作,它会表明它是成功的。因此,您可以如下重构此方法:
@Override
public DailyMenu updateMenu(DailyMenu dailyMenu,MenuItem updateMenuItem,String itemName) {
dailyMenu.getMenuItemList().forEach((MenuItem menuItem)->{
if(menuItem.getNames().equals(itemName)){
dailyMenu.getMenuItemList().set(dailyMenu.getMenuItemList().indexOf(menuItem),updateMenuItem);
}
});
return dailyMenu;
}
然后你的视图会调用一些方法,然后打印菜单。
编辑
让我们考虑一下这个开关案例,它是菜单的一部分
switch (foodMenuTypes){
case BREAKFASTMENU -> {return foodMenu.getBreakfastMenu();}
case LUNCHMENU -> {return foodMenu.getLunchMenu();}
case DINNERMENU -> {return foodMenu.getDinnerMenu();}
}
这个逻辑属于哪里?这是景色的一部分吗?还是控制器?
让我们这样想:如果我们要实现一个图形用户界面,那么我们希望这个开关案例在哪里?答案很简单:我们希望它在控制器中,因为这样我们就可以轻松地实现新的UI,它将具有一些getDinnerMenu
和其他类似的方法,负责以图形方式显示菜单。
另一方面,如果我们把这个开关案例留在视图中,那么,在我们重新实现用户界面的场景中,我们将不得不逐行浏览视图代码,并找到我们需要保留的内容。然而,如果在完全重新实现视图时,我们有部分视图需要保留,那么我们的架构设计就会出错。那么,我们如何将这个逻辑转移到控制器中呢?答案一点也不难:
- 我们创建了一个控制器方法
- 哪个会有这个开关盒
- 返回视图方法的结果
- 取决于在视图中选择的值
原因:即使在视图中选择了值,随后视图必须解析该选择,但决定加载什么模板是一个逻辑问题,逻辑问题被视为在控制器中得到了回答。
所以,你会有一个控制器的方法如下:
public SomeViewClass doMyStuff(int input) {
switch (input) {
case SOMEVALUE1: return someView.getSomeViewClass1();
case SOMEVALUE2: return someView.getSomeViewClass2();
case SOMEVALUE3: return someView.getSomeViewClass3();
//...
default: return null;
}
}
从视图中,只需调用doMyStuff
并传递选项。上面的命名是任意的,你当然应该更能代表你的实体。