
角色
- /**
- * 抽象产品:定义一个咖啡抽象类
- */
- public abstract class Coffee {
-
- //定义一个抽象获取咖啡名字的方法
- public abstract String getName();
-
- }
- /**
- * 具体产品:咖啡具体实现类
- */
- public class AmericanCoffee extends Coffee {
-
- @Override
- public String getName() {
- return "美式咖啡";
- }
-
- }
- /**
- * 具体产品:拿铁咖啡实现类
- */
- public class LatteCoffee extends Coffee {
-
- @Override
- public String getName() {
- return "拿铁咖啡";
- }
-
- }
- /**
- * 具体工厂:咖啡工厂
- */
- public class CoffeeFactory {
-
- /**
- * 获取咖啡的方法,按照不同类型,创建不同的咖啡
- *
- * @param type 类型
- * @return 返回咖啡具体实现类
- */
- public static Coffee getCoffee(String type) {
- Coffee coffee = null;
- if (type.equals("american")) {
- coffee = new AmericanCoffee();
- } else if (type.equals("latte")) {
- coffee = new LatteCoffee();
- } else {
- throw new RuntimeException("没有这种咖啡");
- }
- return coffee;
- }
-
- }
- /**
- * 提供者
- */
- public class CoffeeStore {
-
- public Coffee orderCoffee(String type) {
- return CoffeeFactory.getCoffee(type);
- }
-
- }
- /**
- * 测试类
- */
- public class Test {
-
- public static void main(String[] args) {
- //实例化咖啡Store
- CoffeeStore coffeeStore = new CoffeeStore();
- //获取拿铁咖啡
- Coffee latte = coffeeStore.orderCoffee("latte");
- //打印名称
- System.out.println(latte.getName());
- }
-
- }

角色
- /**
- * 抽象产品:定义一个咖啡抽象类
- */
- public abstract class Coffee {
-
- //定义一个抽象获取咖啡名字的方法
- public abstract String getName();
-
- }
- /**
- * 具体产品:咖啡具体实现类
- */
- public class AmericanCoffee extends Coffee {
-
- @Override
- public String getName() {
- return "美式咖啡";
- }
-
- }
- /**
- * 具体产品:拿铁咖啡实现类
- */
- public class LatteCoffee extends Coffee {
-
- @Override
- public String getName() {
- return "拿铁咖啡";
- }
-
- }
- /**
- * 抽象工厂
- */
- public interface CoffeeFactory {
-
- //声明一个创建咖啡的方法
- Coffee createCoffee();
-
- }
- /**
- * 具体工厂:美式咖啡工厂
- */
- public class AmericanCoffeeFactory implements CoffeeFactory {
-
- @Override
- public Coffee createCoffee() {
- return new AmericanCoffee();
- }
-
- }
- /**
- * 具体工厂:拿铁咖啡工厂
- */
- public class LatteCoffeeFactory implements CoffeeFactory {
-
- @Override
- public Coffee createCoffee() {
- return new LatteCoffee();
- }
-
- }
- /**
- * 提供者:咖啡Store
- */
- public class CoffeeStore {
-
- //咖啡工厂
- private CoffeeFactory factory;
-
- public void setFactory(CoffeeFactory factory) {
- this.factory = factory;
- }
-
- //创建咖啡,由子类工厂决定
- public Coffee orderCoffee() {
- return factory.createCoffee();
- }
-
- }
- /**
- * 测试类
- */
- public class Test {
-
- public static void main(String[] args) {
- //声明工厂为美式咖啡生产工厂
- CoffeeFactory coffeeFactory = new AmericanCoffeeFactory();
- //创建咖啡Store
- CoffeeStore coffeeStore = new CoffeeStore();
- //设置工厂
- coffeeStore.setFactory(coffeeFactory);
- //调用生产方法
- Coffee coffee = coffeeFactory.createCoffee();
- //打印咖啡名称
- System.out.println(coffee.getName());
- }
-
- }

角色
- /**
- * 抽象产品:定义一个咖啡抽象类
- */
- public abstract class Coffee {
-
- //定义一个抽象获取咖啡名字的方法
- public abstract String getName();
-
- }
- /**
- * 具体产品:咖啡具体实现类
- */
- public class AmericanCoffee extends Coffee {
-
- @Override
- public String getName() {
- return "美式咖啡";
- }
-
- }
- /**
- * 具体产品:拿铁咖啡实现类
- */
- public class LatteCoffee extends Coffee {
-
- @Override
- public String getName() {
- return "拿铁咖啡";
- }
-
- }
- /**
- * 定义甜点抽象类
- */
- public abstract class Dessert {
-
- //定义show方法
- public abstract String show();
-
- }
- /**
- * 具体产品
- */
- public class Musi extends Dessert {
-
- @Override
- public String show() {
- return "抹茶慕斯";
- }
-
- }
- /**
- * 具体产品
- */
- public class Tiramisu extends Dessert {
-
- @Override
- public String show() {
- return "提拉米苏";
- }
-
- }
- /**
- * 定义抽象工厂
- */
- public interface Factory {
-
- //创建咖啡
- Coffee createCoffee();
-
- //创建甜点
- Dessert createDessert();
-
- }
- /**
- * 具体工厂
- */
- public class AmericanFactory implements Factory {
-
- @Override
- public Coffee createCoffee() {
- return new AmericanCoffee();
- }
-
- @Override
- public Dessert createDessert() {
- return new Musi();
- }
-
- }
- /**
- * 具体工厂
- */
- public class ItalyFactory implements Factory {
-
- @Override
- public Coffee createCoffee() {
- return new LatteCoffee();
- }
-
- @Override
- public Dessert createDessert() {
- return new Tiramisu();
- }
-
- }
- //测试类
- public class Test {
-
- public static void main(String[] args) {
- //创建美式工厂
- Factory factory = new AmericanFactory();
- //创建咖啡
- Coffee coffee = factory.createCoffee();
- //输出姓名
- System.out.println(coffee.getName());
- }
-
- }