小菜鸟一步步打造图书馆外挂之十四:GetDoWorkFactory工厂的实现
该工厂就是产生该系统在不同情况下的工作对象,实现很简单,就是有一个对ILibraryHelperDoWork接口的引用,再根据不同入口启动和网络连接状况来实例化具体对象,在连接网络之后要调用Ping命令去Ping学校图书馆服务器看看是否可以连接。
创建工程IDoWorkFactory,添加一个类GetDoWorkFactory,代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using LibraryHelper.IDoWork;
using LibraryHelper.DoWork;
using LibraryHelper.ADSLService;
using LibraryHelper.DALService;
using LibraryHelper.HttpService;
namespace LibraryHelper.IDoWorkFactory
{
public class GetDoWorkFactory
{
public static ILibraryHelperDoWork CreateDowork(String startType)
{
ILibraryHelperDoWork doWork = null;
ADSL adsl = new ADSL();
ADSLXMLService adslXMLService = new ADSLXMLService();
//连接
adsl.Connect(adslXMLService.GetConnectionName(), adslXMLService.GetUserName(), adslXMLService.GetUserPassword());
//Ping连接结果,些IP地址为图书馆IP地址
Boolean adslStatu = adsl.ConnnectState("Ping 210.46.107.20");
if (adslStatu)
{
if (startType == "AUTO")
{
doWork = new HttpSendToWarningUI();
}
else if (startType == "Common")
{
doWork = new HttpSendToEditUI();
}
}
else
{
if (startType == "AUTO")
{
doWork = new GetBookFromXMl();
}
else
{
doWork = new DoNothing();
}
}
return doWork;
}
}
}
using System.Collections.Generic;
using System.Text;
using LibraryHelper.IDoWork;
using LibraryHelper.DoWork;
using LibraryHelper.ADSLService;
using LibraryHelper.DALService;
using LibraryHelper.HttpService;
namespace LibraryHelper.IDoWorkFactory
{
public class GetDoWorkFactory
{
public static ILibraryHelperDoWork CreateDowork(String startType)
{
ILibraryHelperDoWork doWork = null;
ADSL adsl = new ADSL();
ADSLXMLService adslXMLService = new ADSLXMLService();
//连接
adsl.Connect(adslXMLService.GetConnectionName(), adslXMLService.GetUserName(), adslXMLService.GetUserPassword());
//Ping连接结果,些IP地址为图书馆IP地址
Boolean adslStatu = adsl.ConnnectState("Ping 210.46.107.20");
if (adslStatu)
{
if (startType == "AUTO")
{
doWork = new HttpSendToWarningUI();
}
else if (startType == "Common")
{
doWork = new HttpSendToEditUI();
}
}
else
{
if (startType == "AUTO")
{
doWork = new GetBookFromXMl();
}
else
{
doWork = new DoNothing();
}
}
return doWork;
}
}
}