以供参考,
这种格式或多或少是索尼HMD格式的变体,这是一种高效的低级别格式,直接由PlayStation 1 GPU使用,代价是不友好地读取/解析等.(实际上,在实际的PlayStation 1上,很少会发出系统库调用来加载它,就是这样)
长话短说,
下面的代码片段可能更好地解释了内容和初步方法,
我们将针对的
/// <summary>
/// a graphics primitive.
/// </summary>
public interface IPrimitive
{
bool IsPolygon { get; }
bool IsSprite { get; }
}
/// <summary>
/// a polygon, i.e. triangle, quad.
/// </summary>
public interface IPolygon : IPrimitive
{
void GetMesh();
}
/// <summary>
/// a sprite, (internal representation has different meaning than a polygon).
/// </summary>
public interface ISprite : IPrimitive
{
void GetMesh();
}游戏数据中的
/// <summary>
/// a packet representing graphical data : a polygon or a sprite.
/// </summary>
public class GraphicsPacket : IPolygon, ISprite
{
public GraphicsPacket(Stream stream)
{
// read packet ...
// there will be a flag indicating if it's a sprite
// and therefore should be treated differently
}
public bool IsSprite { get; }
public bool IsPolygon => !IsSprite;
void IPolygon.GetMesh()
{
if (IsSprite)
throw new InvalidOperationException("Instance is not a polygon");
}
void ISprite.GetMesh()
{
if (IsPolygon)
throw new InvalidOperationException("Instance is not a sprite");
}
}整个逻辑的
public class Test
{
public void BuildPrimitive([NotNull] IPrimitive primitive)
{
if (primitive == null)
throw new ArgumentNullException(nameof(primitive));
// obviously here, 'as' cast would always be true for all cases
var polygon = primitive.IsPolygon;
if (polygon)
{
// do something with it
((IPolygon)polygon).GetMesh();
return;
}
var sprite = primitive.IsSprite;
if (sprite)
{
// do something with it
((ISprite)sprite).GetMesh();
return;
}
throw new NotSupportedException(nameof(primitive));
}
}根据图形包的内容,我的处理方法是好的吗?
即
bool检查和抛出强烈主张在这种情况下考虑分开关注,
希望这对你来说是有意义的,否则让我知道如何改进这个问题。
发布于 2017-05-18 19:50:07
我相信你只需要一个接口,类为多边形和雪碧实现。基于我所看到的,我将为GraphicsPacket创建一个接口,并创建实现其方法的Polygon和Sprite类。
然后,在整个代码中,您只需编写它就可以使用GraphicsPacket对象,您不需要多次决定它是多边形还是雪碧。
public interface IGraphicsPacket
{
public GraphicsPacket(Stream stream);
public GetMesh()
}
public class Polygon : IGraphicsPacket
{
public GraphicsPacket(Stream stream)
{
//Polygon implementation
}
public GetMesh()
{
//Polygon implementation
}
}
public class Sprite : IGraphicsPacket
{
public GraphicsPacket(Stream stream)
{
//Sprite implementation
}
public GetMesh()
{
//Sprite implementation
}
}public class Test
{
public void BuildPrimitive([NotNull] IGraphicsPacket primitive)
{
if (primitive == null)
throw new ArgumentNullException(nameof(primitive));
primitive.GetMesh();
return;
}
}发布于 2017-06-17 23:51:58
。
这是一个很好的例子,在这个例子中,某人应该像jdobrzen在另一个答案中建议的那样使用类层次结构。
避免代码重复的方法是在基类或助手类中共享实现。
public class PrimitiveBase
{
/* contains common code, probably with protected */
}
public class Polygone : PrimitiveBase, IPolygone { ... }然后,您还应该有一个工厂方法,它将从流创建适当的对象。一种方法是,您的所有接口都来自IPrimitive,并有一个工厂。
类似于:
public class PrimitiveFactory
{
IPrimitive CreateFromStream(Stream stream)
{
// - Read primitive type from stream
// - Reset stream position (if necessary)
// - Create appropriate primitive (might be a switch statement)
// - If data loading is separate from constructor, then load data
}
} 如果需要,可以使用DI容器或序列化框架.
https://softwareengineering.stackexchange.com/questions/349231
复制相似问题