简单工厂模式
简单工厂模式是最基本的工厂模式,我们只需要一个工厂类,它根据客户端的请求返回一个具体的对象实例。简单工厂模式通常只适用于创建单一的对象类型。
工厂方法模式
工厂方法模式是一种扩展简单工厂模式的模式,它定义了一个工厂方法接口,用于创建对象,这些对象由子类决定实例化哪一个类。工厂方法模式通常用于创建不同的对象类型,每个对象类型对应一个工厂方法。
抽象工厂模式
抽象工厂模式是一种创建一组相关或依赖对象的工厂模式。在抽象工厂模式中,客户端请求一个工厂,并从工厂中获取一个产品族中的一个对象。在抽象工厂模式中,工厂不仅仅是一个单独的类,而是一个由多个工厂组成的层次结。
工厂模式的优点
-
将对象创建过程与客户端代码分离,减少重复代码,提高代码的可维护性和可读性。
-
隐藏实现细节,避免客户端代码对具体实现的依赖,提高代码的可扩展性。
-
降低了耦合性,客户端只需要知道需要什么对象,而不需要知道对象的创建细节。
工厂模式的缺点
-
增加了代码的复杂度,需要创建更多的类。
-
工厂模式通常需要使用额外的代码来实现,这增加了开发时间和成本。
简单工厂模式应用:
首先我们创建一个道具的基类
public class Item : MonoBehaviour
{
protected string name;
public virtual void use()
{
Debug.Log("use" + name);
}
}
然后,可以定义几个具体的道具类,如 HealthPotion,SpeedPotion,它们都是 Item 的子类,并且实现了自己特定的 use 方法。
public class HealthPotion : Item
{
public HealthPotion()
{
name = "yao";
}
public override void use()
{
//base.use();
Debug.Log("yao001");
}
}
public class SpeedPotion : Item
{
public SpeedPotion()
{
name = "speed";
}
public override void use()
{
base.use();
Debug.Log("speed001");
}
}
定义一个简单工厂类 ItemFactory,该类包含一个用于创建道具的静态方法 CreateItem,根据传入的参数不同,创建不同的道具实例并返回。
public class IteamFactory : MonoBehaviour
{
public static Item CreatItem(string itemname)
{
Item item = null;
if (itemname == "health")
{
item = new HealthPotion();
}
else if (itemname == "speed")
{
item = new SpeedPotion();
}
return item;
}
}
最后是具体使用
public class use : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Item health = IteamFactory.CreatItem("health");
HealthPotion healthPotion = new HealthPotion();
healthPotion.use();
Item speed = IteamFactory.CreatItem("speed");
SpeedPotion speedPotion = new SpeedPotion();
speedPotion.use();
}
}
工厂模式应用:
首先我们定义一个工厂接口 ItemFactory,该接口包含一个用于创建道具的抽象方法 CreateItem,具体的工厂类实现该方法来创建具体的道具实例。
public interface IItemFactory
{
Item CreatItem();
}
创建具体的工厂类,如 HealthPotionFactory、SpeedPotionFactory,它们实现了 ItemFactory 接口,并且实现了自己特定的 CreateItem 方法来创建具体的道具实例
public class HealthPotionFactory : IItemFactory
{
public Item CreatItem()
{
return new HealthPotion();
}
}
public class SpeedPotionFactory : IItemFactory
{
public Item CreatItem()
{
return new SpeedPotion();
}
}
最后,我们可以在游戏中使用具体的工厂类来创建道具实例并使用它们。
void Start()
{
IItemFactory health = new HealthPotionFactory();
health .use();
IItemFactory speed = new SpeedPotionFactory();
speed .use();
}
抽象工厂模式:
通过抽象工厂实现Sqlserver和Access服务器并存的程序,根据客户端选择使用哪个服务
定义User表格,声明两个参数id和名字。
public class User : MonoBehaviour
{
private int _id;
public int ID
{
get { return _id; }
set { _id = value; }
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
}
IUser接口,用于客户端访问,解除与具体数据库访问的耦合。
public interface IUser
{
void Insert(User user);
User GetUser(int id);
}
Sqlserver User类,用于访问SQL Server的User。
public class SqlserverUser : IUser
{
public User GetUser(int id)
{
Debug.Log("在Sql server 中得到User表一条记录");
return null;
}
public void Insert(User user)
{
Debug.Log("在Sql server 中给User表增加一条记录");
}
}
Access User类,用于访间Access的User。
public class AccessUser : IUser
{
public User GetUser(int id)
{
Debug.Log("在Access 中得到User表一条记录");
return null;
}
public void Insert(User user)
{
Debug.Log("在Access 中给User表增加一条记录");
}
}
IFactory接口,定义一个创建访问User表对象的抽象的工厂接口。
public interface IFactory
{
IUser CreateUser();
IDepatment CreatDepartment();
}
Sql Server Factory类,实现IFactory接口,实例化Sql server User。
public class SqlserveFactory : IFactory
{
public IDepatment CreatDepartment()
{
return new SqlserverDepartment();
}
public IUser CreateUser()
{
return new SqlserverUser();
}
}
Access Factory类,实现I Factory接口,实例化Access User。
public class AccessFactory : IFactory
{
public IDepatment CreatDepartment()
{
return new AccessDepartment();
}
public IUser CreateUser()
{
return new AccessUser();
}
}
客户端代码:
public class Client : MonoBehaviour
{
private void Start()
{
User user = new User();
IFactory factory = new SqlserveFactory();
IUser iuser = factory.CreateUser();
iuser.Insert(user);
iuser.GetUser(1);
}
}
当增加新的表时,需要新建新表接口,将Sql和access分别实现接口,再去Ifactory新增实现方法。
同时客户端根据服务器不同,实例化相应方法,调用即可。
在此项目中,工厂部分显得比较臃肿了,去除I Factory、Sql server Factory和Access Factory三个工厂类,取而代之Data Access类,用一个简单工厂模式来实现,则可以更方便。