我有一个用Unity编写的游戏客户端和一个用Azure Functions编写的服务器。我在Azure函数解决方案中有一个类库,带有"域"。将在客户端和服务器之间共享的类。我将类库构建为DLL并将其放入Unity的Plugins文件夹中,所以我可以在游戏客户端中使用共享代码。
这工作得很好,但我遇到了一种情况,我需要游戏客户端具有特殊属性,我不希望服务器在其解决方案中拥有,因为它只会增加复杂性和依赖性(例如对Unity游戏引擎的依赖性,例如在服务器中没有业务)。
制作一个可以在游戏客户端增强但在服务器端保持基本的类的最干净的方法是什么?
选项1:
使用继承
public class DomainGun
{
public int BulletCount;
public string Name;
public DomainBullets Bullets;
}
public class DomainBullets
{
public string Property;
}
public class GameClientBullet: DomainBullets
{
BulletRendere Renderer;
}
这失败了,因为现在我必须从DomainBullets向下投射到gameclientbullet,以便在客户端上使用DomainGun进行操作时到达渲染器。
我可以用泛型来做。
public class DomainGun
{
public int BulletCount;
public string Name;
public DomainBullets Bullets;
}
public class DomainBullets<T>
{
public string Property;
public T Renderer;
}
//now I can do this on the server:
public interface IBulletRenderer
{
//placeholder that has nothing in it
}
//and this on the client..
public class BulletRenderer
{
UnityRenderingThing UnityDependentThing;
}
我认为这将工作-允许我在一个地方共享属性,然后让客户端使其更专业化,但是以这种方式使用泛型与空接口感觉像一个代码气味。
我认为有一个更聪明的方法来做这个接口,但我现在空格。谢谢大家的建议。
使用装饰器设计模式。把基类放在服务器上,把装饰器放在客户端。
https://www.dofactory.com/net/decorator-design-pattern