枚举实现工厂模式

 1 package com. lee.first1200 ;
 2 
 3 public interface Car {
 4 
 5        public void sayName() ;
 6 }
 7 
 8 package com. lee.first1200 ;
 9 
10 public class AudiCar implements Car {
11 
12        @Override
13        public void sayName() {
14              System.out .println( "Audi");
15        }
16 
17 }
18 package com. lee.first1200 ;
19 
20 public class BenzCar implements Car {
21 
22        @Override
23        public void sayName() {
24              System.out .println( "Benz");
25        }
26       
27 }
28 package com. lee.first1200 ;
29 
30 public enum CarFactory {
31 
32       BENZ {
33 
34              @Override
35              public Car create() {
36                    // TODO Auto-generated method stub
37                    return new BenzCar() ;
38              }
39 
40        },
41       AUDI {
42 
43              @Override
44              public Car create() {
45                    // TODO Auto-generated method stub
46                    return new AudiCar() ;
47              }
48 
49        },
50       BMW {
51 
52              @Override
53              public Car create() {
54                    // TODO Auto-generated method stub
55                    return new BmwCar() ;
56              }
57 
58        };
59 
60        public abstract Car create();
61 
62 }
63 
64 package com. lee.first1200 ;
65 
66 public class CarMain {
67 
68        public static void main(String [] args ) {
69             CarFactory .BMW. create(). sayName();
70 
71             CarFactory .AUDI. create(). sayName();
72 
73             CarFactory .BMW. create(). sayName();
74        }
75 }

 

转载于:https://www.cnblogs.com/IcanFixIt/p/4853721.html

不使用枚举实现策略工厂的方法通常涉及到使用映射(Map)来根据策略名称动态地创建策略实例。以下是实现策略工厂的一个基本步骤: 1. 定义策略接口:首先,定义一个策略接口,所有具体的策略实现都需要实现这个接口。 ```java public interface Strategy { void execute(); } ``` 2. 实现具体策略类:然后,为每种策略创建具体的实现类。 ```java public class ConcreteStrategyA implements Strategy { @Override public void execute() { // 具体实现A } } public class ConcreteStrategyB implements Strategy { @Override public void execute() { // 具体实现B } } ``` 3. 创建策略工厂:接着,实现一个策略工厂类,该类内部可以使用一个映射(Map)来关联策略名称和对应的策略实例。 ```java public class StrategyFactory { private static final Map<String, Strategy> strategies = new HashMap<>(); static { strategies.put("StrategyA", new ConcreteStrategyA()); strategies.put("StrategyB", new ConcreteStrategyB()); // 可以添加更多的策略映射 } public static Strategy getStrategy(String name) { if (name == null || name.isEmpty()) { throw new IllegalArgumentException("Strategy name cannot be null or empty."); } return strategies.get(name); } } ``` 4. 使用工厂获取策略实例:最后,在客户端代码中使用工厂类来获取具体的策略实例并执行策略方法。 ```java public class Client { public static void main(String[] args) { Strategy strategyA = StrategyFactory.getStrategy("StrategyA"); strategyA.execute(); Strategy strategyB = StrategyFactory.getStrategy("StrategyB"); strategyB.execute(); } } ``` 这种方法通过映射动态地关联策略名称和对应的策略实例,避免了枚举的使用,同时保持了策略模式的灵活性和扩展性。策略工厂可以根据实际需要动态地增加或删除策略映射。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值