java设计模式(3)--工厂模式及其实例
一.工厂模式,以数据库的业务逻辑为简单例子,里面有很多不合理的地方,目的是为了阐述对工厂模式的理解。
1.声明接口BaseDAO
/**
?**<p>工厂接口</p>
?**作者:???
?**Blong:http://www.blogjava.net/1504
?**E-mail:yyk1504@163.com
?**QQ:251218333
?**<p>2008-12-16 下午02:08:15</p>
*/
package cn.com.model.factory;
import java.sql.Connection;
public interface BaseDAO
{
??? /**数据库的插入方法*/
??? public void save();?
??? /**数据库的更新方法*/
??? public void update();
??? /**数据库的删除方法*/
??? public void delete();
}
2.MYSQL具体实现MysqlDAO
*<p>Mysql数据库实现业务逻辑类</p>
?*作者:???
?*Blong:http://www.blogjava.net/1504
?*E-mail:yyk1504@163.com
?*QQ:251218333
?*<p>2008 2008-12-16 </p>
*/
package cn.com.model.factory;
import java.sql.Connection;
import java.sql.DriverManager;
public class MysqlDAO implements BaseDAO
{
??? public void delete()
??? {
??????? /**
???????? *<p>此方法的功能介绍</p>
???????? *delete
???????? *MysqlDao
???????? *
???????? *Author:ASUS_1
???????? <p>2008 2008-12-16 下午02:16:27</p>
??????? */
??????? System.out.println("Mysql在此处实现删除....");
??? }
??? public void save()
??? {
??????? /**
???????? *<p>此方法的功能介绍</p>
???????? *save
???????? *MysqlDao
???????? *
???????? *Author:ASUS_1
???????? <p>2008 2008-12-16 下午02:16:27</p>
??????? */
??????? System.out.println("Mysql在此处实现保存....");
??? }
??? public void update()
??? {
??????? /**
???????? *<p>此方法的功能介绍</p>
???????? *update
???????? *MysqlDao
???????? *
???????? *Author:ASUS_1
???????? <p>2008 2008-12-16 下午02:16:27</p>
??????? */
??????? System.out.println("Mysql在此处实现更新....");
??? }
???
}
3.SQL2000具体实现类
/**
?*<p>MsSql2000数据库实现业务逻辑类</p>
?*作者:???
?*Blong:http://www.blogjava.net/1504
?*E-mail:yyk1504@163.com
?*QQ:251218333
?*<p>2008 2008-12-16 </p>
*/
package cn.com.model.factory;
import java.sql.Connection;
import java.sql.DriverManager;
public class MsSql2000DAO implements BaseDAO
{
??? public void delete()
??? {
??????? /**
???????? *<p>此方法的功能介绍</p>
???????? *delete
???????? *MsSql2000DAO
???????? *
???????? *Author:ASUS_1
???????? <p>2008 2008-12-16 下午02:21:04</p>
??????? */
??????? System.out.println("MsSql2000在此处实现删除....");
??? }
??? public void save()
??? {
??????? /**
???????? *<p>此方法的功能介绍</p>
???????? *save
???????? *MsSql2000DAO
???????? *
???????? *Author:ASUS_1
???????? <p>2008 2008-12-16 下午02:21:04</p>
??????? */
??????? System.out.println("MsSql2000在此处实现保存....");
??? }
??? public void update()
??? {
??????? /**
???????? *<p>此方法的功能介绍</p>
???????? *update
???????? *MsSql2000DAO
???????? *
???????? *Author:ASUS_1
???????? <p>2008 2008-12-16 下午02:21:04</p>
??????? */
??????? System.out.println("MsSql2000在此处实现更新....");
??? }
??? }
4.SQL2005具体实现类
/**
?*<p>MsSql2005数据库实现业务逻辑类</p>
?*作者:???
?*Blong:http://www.blogjava.net/1504
?*E-mail:yyk1504@163.com
?*QQ:251218333
?*<p>2008 2008-12-16 </p>
*/
package cn.com.model.factory;
import java.sql.Connection;
import java.sql.DriverManager;
public class MsSql2005DAO implements BaseDAO
{
??? public void delete()
??? {
??????? /**
???????? *<p>此方法的功能介绍</p>
???????? *delete
???????? *MsSql2005DAO
???????? *
???????? *Author:ASUS_1
???????? <p>2008 2008-12-16 下午02:23:38</p>
??????? */
??????? System.out.println("MsSql2005在此处实现删除....");
??? }
??? public void save() {
??????? /**
???????? *<p>此方法的功能介绍</p>
???????? *save
???????? *MsSql2005DAO
???????? *
???????? *Author:ASUS_1
???????? <p>2008 2008-12-16 下午02:23:38</p>
??????? */
??????? System.out.println("MsSql2005在此处实现保存....");
??? }
??? public void update()
??? {
??????? /**
???????? *<p>此方法的功能介绍</p>
???????? *update
???????? *MsSql2005DAO
???????? *
???????? *Author:ASUS_1
???????? <p>2008 2008-12-16 下午02:23:38</p>
??????? */
??????? System.out.println("MsSql2005在此处实现更新....");
??? }
???
}
5.ORACLE具体实现类
/**
?**<p>Oracle数据库实现业务逻辑类</p>
?**作者:???
?**Blong:http://www.blogjava.net/1504
?**E-mail:yyk1504@163.com
?**QQ:251218333
?**<p>2008-12-16 下午02:11:59</p>
*/
package cn.com.model.factory;
import java.sql.Connection;
import java.sql.DriverManager;
public class OracleDAO implements BaseDAO
{
??? public void delete()
??? {
??????? /**
???????? *<p>此方法的功能介绍</p>
???????? *delete
???????? *OracleDao
???????? *
???????? *Author:ASUS_1
???????? *<p>2008 2008-12-16 下午02:12:59</p>
??????? */
??????? System.out.println("Oracle在此处实现删除....");
??? }
??? public void save()
??? {
??????? /**
???????? *<p>此方法的功能介绍</p>
???????? *save
???????? *OracleDao
???????? *
???????? *Author:ASUS_1
???????? *<p>2008 2008-12-16 下午02:12:59</p>
??????? */
??????? System.out.println("Oracle在此处实现保存....");
??? }
??? public void update()
??? {
??????? /**
???????? *<p>此方法的功能介绍</p>
???????? *update
???????? *OracleDao
???????? *
???????? *Author:ASUS_1
???????? *<p>2008 2008-12-16 下午02:12:59</p>
??????? */
??????? System.out.println("Oracle在此处实现更新....");
??? }
?
}
6.工厂类
/**
?*<p>数据库业务工厂</p>
?*作者:???
?*Blong:http://www.blogjava.net/1504
?*E-mail:yyk1504@163.com
?*QQ:251218333
?*<p>2008-12-16 </p>
*/
package cn.com.model.factory;
import cn.com.model.factory.*;
public class DaoFactory
{
???? public BaseDAO createFactory(String type)
???? {??
???????????????? if ("Mysql".equals(type))
???????????????? {??
???????????????????? return new MysqlDAO();??
???????????????? }
???????????????? else
???????????????? if ("MsSql2000".equals(type))
???????????????? {??
???????????????????? return new MsSql2000DAO();??
???????????????? }
???????????????? else?
???????????????? if ("MsSql2005".equals(type))
???????????????? {??
???????????????????? return new MsSql2005DAO();??
???????????????? }
???????????????? else?
???????????????? if ("Oracle".equals(type))
???????????????? {??
???????????????????? return new OracleDAO();??
???????????????? }
???????????????????? return null;??
???????????? }??
??????????
???????????? public static void main(String[] args)
???????????? {??
???????????????? DaoFactory creator = new DaoFactory();??
???????????????? BaseDAO mysqlDAO=creator.createFactory("Mysql");?
???????????????? mysqlDAO.save();
???????????????? mysqlDAO.update();
???????????????? mysqlDAO.delete();
???????????????
???????????????? System.out.println("---------------------------------");
???????????????
???????????????? BaseDAO msSql2000DAO=creator.createProduct("MsSql2000");?
???????????????? msSql2000DAO.save();
???????????????? msSql2000DAO.update();
???????????????? msSql2000DAO.delete();
???????????????
???????????????? System.out.println("---------------------------------");
???????????????
???????????????? BaseDAO msSql2005DAO=creator.createProduct("MsSql2005");?
???????????????? msSql2005DAO.save();
???????????????? msSql2005DAO.update();
???????????????? msSql2005DAO.delete();
???????????????
???????????????? System.out.println("---------------------------------");
???????????????
???????????????? BaseDAO oracleDAO=creator.createProduct("Oracle");?
???????????????? oracleDAO.save();
???????????????? oracleDAO.update();
???????????????? oracleDAO.delete();
???????????? }???
}