Spring反射+策略模式替代switch/if-else

2019-01-16  本文已影响0人  那我懂你意思了_de16

如果switch/if-else分支不固定,或将来有可能改,可以采用反射+策略模式来替换switch/if-else语句。例如动物,随着种类变化,我们需要不断增加switch分支,这样我们就可以使反射+策略模式,使之更加灵活。

if-else方式:

Animal animal = new Animal();
if("dog".equals(type)) {
    animal = new Dog();
} if else("cat".equals(type)) {
    animal = new Cat();
}
animal.eat();

switch方式:

Animal animal = new Animal();
switch (type) {
    case "dog":
        animal  = new Dog();
        break;
    case "cat":
        animal  = new Cat();
        break;
}
animal.eat();

Spring反射+策略模式:

Animal接口(抽象策略角色):

public interface Animal {

    void eat();
}

Cat实现Animal接口(具体实现策略):

@Component
public class Cat implements Animal {

    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }
}

Dog实现Animal接口(具体实现策略):

@Component
public class Dog implements Animal {

    @Override
    public void eat() {
        System.out.println("狗啃骨头");
    }
}

AnimalFactory类(环境角色):

@Service("animalFactory")
public class AnimalFactory implements ApplicationContextAware, InitializingBean {

    private Map<String, Animal> animalMap;

    private ApplicationContext applicationContext;

    @Override
    public void afterPropertiesSet() throws Exception {
        Map<String, Animal> animalMap = this.applicationContext.getBeansOfType(Animal.class);

        this.animalMap = animalMap;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public Animal getAnimal(String beanName) {
        return this.animalMap.get(beanName);
    }
}
存储bean

测试类:

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

    @Autowired
    private AnimalFactory animalFactory;

    @Test
    public void test() {
        Animal animal = animalFactory.getAnimal("dog");
//        Animal animal = animalFactory.getAnimal("cat");
        animal.eat();
    }

}

输出:

狗啃骨头


以后需求变更,只要增加实现类即可。

上一篇下一篇

猜你喜欢

热点阅读