在web開發中咱們經常使用mvc模式進行web應用的開發java
當應用進入service 層的時候咱們根據不一樣的業務多邏輯進行處理web
當有數據進入controller的時候spring
public class VirtualController {
private static Service service = CommonFactory.getFactory().getInstance(Service.class);
public static void main(String[] args) {
User nuser= new User();
nuser.setName("李某");
service.insert(nuser);
}
}
首先是實體類sql
public class User { public String getName() { return name; } public void setName(String name) { this.name = name; } private String name; }
因此到咱們定義一個service 的接口 處理全部用戶的業務tomcat
//也就是至關於子工廠
public interface Service {
public void insert(User user);
public void deldte(User user);
}
//子工廠的實現
public class UserService implements Service{
private Dao dao = CommonFactory.getFactory().getInstance(Dao.class);
@Override
public void insert(User user) {
System.out.println("普通用戶業務");
dao.insert(user);
}
@Override
public void deldte(User user) {
}
}
public class VipService implements Service{
private Dao dao = CommonFactory.getFactory().getInstance(Dao.class);
@Override
public void insert(User user) {
System.out.println("Vip用戶業務");
}
@Override
public void deldte(User user) {
}
}
Dao層springboot
public interface Dao {
public void insert(User user);
public void deldte(User user);
}
public class MysqlDao implements Dao{
@Override
public void insert(User user) {
System.out.println("Mysql 插入用戶信息 "+ user.getName());
}
@Override
public void deldte(User user) {
}
}
public class OrcalDao implements Dao{
@Override
public void insert(User user) {
System.out.println("Mysql 插入用戶信息 "+ user.getName());
}
@Override
public void deldte(User user) {
}
}
工廠的實現mvc
public class CommonFactory {
private Properties properties=new Properties();
private CommonFactory(){};
private static CommonFactory factroy= new CommonFactory();
public static CommonFactory getFactory(){
return factroy;
}
public <T> T getInstance(Class<T> clazz) {
try {
properties.load(new FileInputStream("C:\\Users\\Administrator.TEDU-20180130RP\\IdeaProjects\\springboot\\hello\\resource\\config.properties"));
String _className=clazz.getSimpleName();
String className=properties.getProperty(_className);
return (T) Class.forName(className).newInstance();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
這樣咱們就能夠經過修改配置文件進行對不一樣業務的處理,也好像是tomcat 以及jdbc的原理ide