标签:break oid 输出 tor ace creat interface bsp 代码
//设计模式之简单工厂模式 //创建一个食物类 interface Food { void eat(); } //我今天想吃薯条或汉堡 class Hamburger implements Food { @Override public void eat() { System.out.println("我吃汉堡"); } } class FrenchFries implements Food { @Override public void eat() { System.out.println("我吃薯条"); } } //我到速食店了,要开始点餐了 class FoodFactory { static Food createFood(String name) { Food food = null; switch (name) { case "汉堡": food = new Hamburger(); break; case "薯条": food = new FrenchFries(); break; } return food; } } public class Demo3 { public static void main(String[] args) { //我全部都要 Food hamburger = FoodFactory.createFood("汉堡"); hamburger.eat(); Food frenchFries = FoodFactory.createFood("薯条"); frenchFries.eat(); } } 输出: 我吃汉堡 我吃薯条
//创建一个食物类 interface Food { void eat(); } //我今天想吃薯条或汉堡 class Hamburger implements Food { @Override public void eat() { System.out.println("我要吃奥尔良烤鸡腿堡"); } } class FrenchFries implements Food { @Override public void eat() { System.out.println("我要吃皱纹炸薯条"); } } class HamburgerFactory { static Hamburger createHamburger() { return new Hamburger(); } } class FrenchFriesFactory { static FrenchFries createFrenchFries() { return new FrenchFries(); } } //我到速食店了,要开始点餐了 //class FoodFactory { // static Food createFood(String name) { // Food food = null; // switch (name) { // case "汉堡": // food = new Hamburger(); // break; // case "薯条": // food = new FrenchFries(); // break; // } // return food; // } //} public class Demo3 { public static void main(String[] args) { //我全部都要 // Food hamburger = FoodFactory.createFood("汉堡"); // hamburger.eat(); // Food frenchFries = FoodFactory.createFood("薯条"); // frenchFries.eat(); Hamburger hamburger = HamburgerFactory.createHamburger(); hamburger.eat(); FrenchFries frenchFries = FrenchFriesFactory.createFrenchFries(); frenchFries.eat(); } } 输出: 我要吃奥尔良烤鸡腿堡 我要吃皱纹炸薯条
标签:break oid 输出 tor ace creat interface bsp 代码
原文地址:https://www.cnblogs.com/lzh110/p/9462028.html