Java设计模式(13) MVC模式
2016-12-31 00:00
190 查看
/** * 设计模式(13) * MVC模式 * * 这个模式是大家最熟悉的模式了,各大语言的框架基本都有MVC设计的 * * 这个模式将数据与显示分离, 使用控制器来操作他们 * 设计分层, 更利于代码的开发和扩展 * */ // Model class PetStoreDataBase { private int petCount = 0; public int getPetCount() { return petCount; } public void addPet(int n) { petCount+=n; } public void subPet(int n) { petCount-=n; } } // View class PetStoreView { void show(int n) { System.out.printf("店里一共有[%d]只宠物\n",n); } } // Controller class PetStoreController { private PetStoreDataBase petStoreDataBase; private PetStoreView petStoreView; public PetStoreController() { petStoreDataBase = new PetStoreDataBase(); petStoreView = new PetStoreView(); } public void addPet(int n) { petStoreDataBase.addPet(n); } public void subPet(int n) { petStoreDataBase.subPet(n); } public void showPet() { petStoreView.show(petStoreDataBase.getPetCount()); } } // 测试 // Test MVC Pattern public class main { public static void main(String[] argv) { PetStoreController petStoreController = new PetStoreController(); // 显示当前 petStoreController.showPet(); // 宠物店进货10只 petStoreController.addPet(10); // 当即卖出2只 petStoreController.subPet(2); // 显示当前 petStoreController.showPet(); } } // 输出 /* 店里一共有[0]只宠物 店里一共有[8]只宠物 */
相关文章推荐
- java 设计模式学习笔记(13) - 适配器模式
- JAVA设计模式(13):行为型-责任链模式(Responsibility)
- java行为设计模式——MVC模式
- Java设计模式(13)之策略模式
- java设计模式13——策略模式(Strategy)
- Java设计模式13——迭代器模式
- 23种设计模式(13)java观察者模式
- JAVA-13-IO流之字符流;设计模式讲解
- Java设计模式(13)模板模式(Template模式)
- JAVA设计模式(13):行为型-责任链模式(Responsibility)
- 重学java23种设计模式(13)责任链模式
- JAVA设计模式-13-适配器模式
- 使用观察者模式实现Java应用程序设计MVC模式
- 设计模式:MVC模式 (java)
- Java常用的设计模式13:常用设计模式之桥接模式(结构型模式)
- java学习笔记-设计模式13(享元模式)
- 走穿java23种设计模式--13模板方法模式详解
- Java设计模式(13)——结构型模式之桥梁模式(Bridge)
- Java设计模式(13)行为型:策略模式
- JAVA设计模式(13) —<行为型>策略模式(strategy)