根据类型调用不同的子类

需求:根据用户选择的不同,调用不同的底层实现

 

public interface FatherService {

    void speak();

    void doSth();
}

public abstract class FatherServiceImpl implements FatherService {

    public void speak() {
        //speak
    }

    public abstract void doSth();
}


@Service("boyService")
public class BoyServiceImpl extends FatherServiceImpl {

    public void doSth() {
        //doSth
    }
}

@Service("girlService")
public class GirlServiceImpl extends FatherServiceImpl {

    public void doSth() {
        //doSth
    }
}

public class OutterInterfaceImpl implements OutterInterface {

	FatherSerivce fatherSerivce;

	public doSth(int type) {
		if(type = 1) {
			fatherSerivce = boyService;
		} else {
			fatherSerivce = girlService;
		}
		fatherSerivce.doSth();
	}
}

 

你可能感兴趣的:(SpringMVC,java)