Service
package com.ylz.consumer.web.routeable;
public interface Service {
/**
* 订购
* @param request 订购请求参数
* @return
*/
public String order(String request);
}
Routeable
package com.ylz.consumer.web.routeable;
import java.util.HashMap;
import java.util.Map;
public class Routeable implements Service{
static Map<String ,Service > serviceMap = new HashMap<String, Service>();
static{
Service aService = new AService();
Service bService = new BService();
Service cService = new CService();
serviceMap.put("aService", aService);
serviceMap.put("bService", bService);
serviceMap.put("cService", cService);
}
@Override
public String order(String request) {
// TODO Auto-generated method stub
Service service = serviceMap.get(request);
service.order(request);
return null;
}
public static void main(String[] args) {
Routeable r = new Routeable();
String service = "";
// TODO Auto-generated method stub
System.out.println("---------------select AService---------------");
service = "aService";
r.order(service);
System.out.println("---------------select AService---------------");
System.out.println("---------------select BService---------------");
service = "bService";
r.order(service);
System.out.println("---------------select BService---------------");
System.out.println("---------------select CService---------------");
service = "cService";
r.order(service);
System.out.println("---------------select CService---------------");
}
}
AbstracService
package com.ylz.consumer.web.routeable;
abstract public class AbstracService implements Service{
@Override
public String order(String request) {
// TODO Auto-generated method stub
System.out.println("all Services need to go in this");
process(request);
return null;
}
//所有继承的子类 必须实现 抽象方法
abstract public void process(String request);
}
AService
package com.ylz.consumer.web.routeable;
public class AService extends AbstracService{
//与其他继承 AbstracService 的 自定义的方法
@Override
public void process(String request) {
// TODO Auto-generated method stub
System.out.println("我是:"+request);
}
}
BService
package com.ylz.consumer.web.routeable;
public class BService extends AbstracService{
//与其他继承 AbstracService 的 自定义的方法
@Override
public void process(String request) {
// TODO Auto-generated method stub
System.out.println("我是:"+request);
}
}
CService
package com.ylz.consumer.web.routeable;
public class CService extends AbstracService{
//与其他继承 AbstracService 的 自定义的方法
@Override
public void process(String request) {
// TODO Auto-generated method stub
System.out.println("我是:"+request);
}
}