1.写一个ICustomerDao的接口,CustomerDao继承这个接口。
2.写一个DaoFactory这个类,里面有一个getCustomerDao()这个方法,返回一个ICustomerDao这个接口。
3.LoginAction这个类中:
ICustomerDao cdao = DaoFactory.getCustomerDao();//面向接口编程。
具体的见Prj10_1_bank
面向接口:
public class Action
{
public void execute()
{
ICustomerDao cdao = DaoFactory.getCustomerDao();
cdao.login();
}
}
public class DaoFactory
{
public static ICustomer getCustomerDao()
{
return new CustomerDao();
}
}
public interface ICustomerDao()
{
public abstract boolean login();
}
public class CustomerDao implements ICustomerDao
{
pbulic boolean login(){/*代码*/}
}
Spring 接口:
public class Action
{
public void execute()
{
ICustomerDao cdao = DaoFactory.getBean("类名");
cdao.login();
}
}
public class BeanFactory
{
public static Object getBean(String className)
{
return Class.forName(className).newInstance();
}
}
public interface ICustomerDao()
{
public abstract boolean login();
}
public class CustomerDao implements ICustomerDao
{
pbulic boolean login(){/*代码*/}
}