具有抽象参数的抽象工厂? [英] Abstract factory with abstract parameters?

查看:116
本文介绍了具有抽象参数的抽象工厂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用抽象工厂设计一个好的实体创建系统(按照 http://www.dofactory .com/Patterns/PatternAbstract.aspx ),但是在实例特定参数方面,我很挣扎.

例如: 我有两个抽象工厂,一个用于创建弹丸,另一个用于创建板条箱

现在工厂可以是每种类型的一个实例,并从列表中传递一个抽象参数集(在基类中它将共享材质,大小等),类型特定的参数将是弹丸的速度,并且板条箱的耐用性.

但是,我一直在苦苦挣扎的是,最终,当我有了这个抽象工厂方法并用诸如字符串"BulletProjectile"和"WeakCrate"之类的参数调用时,我需要提供实例特定的参数,更重要的是它们对于不同的工厂具有不同的类型-对于弹丸,它们将具有位置和速度,而条板箱将仅具有位置. 更糟糕的情况是,用户或播放器正在创建板条箱或类似的对象,并且能够定义其尺寸.我该如何处理?

解决方案

几个选项:

重新考虑您的用法

如果抽象工厂将工厂的用户与确切类型的产生方式分开,则它很有用.抽象工厂对其产生的内容没有任何限制,只是它是抽象的.它可以返回非抽象类型,也可以返回不是继承层次结构最基本的抽象类型.

如果使用工厂的代码已经可以获取不同的数据集来调用工厂,那么使用工厂的代码已经对其中的类型有所了解.

以下是一些可供考虑的选项:

  • 提供多种抽象工厂类型,每种类型都有一个Create方法,例如GrenadeFactoryBulletFactory
  • 在单个抽象工厂类型上提供多种方法,例如CreateBulletCreateGrenade
  • 停止使用抽象工厂.如果您真的不需要抽象的构造,而只需要抽象的类型,那么这是一个很好的选择.

请记住,您仍然可以将派生类型(Bullet)传递给采用基本类型(例如EntityProjectile)的方法.

双重发送

如果您真的不愿意将抽象工厂与抽象参数结合起来,那么您可能需要研究双重调度,或者访客模式.这里的关键是,您正在尝试使两种不同的虚拟方法相互结合,并基于这两种派生类型获得行为的唯一组合.

这将要求您为参数创建基本类型和派生类型,因此,如果不创建从基本Parameters类型派生的自定义参数结构,就无法传递简单类型(如int,string等). /p>

实现访问者模式还需要大量额外的代码.

RTTI

您可以使用 C ++运行时类型信息功能.

使用dynamic_cast,可以将基本类型转换为派生类型.您可以在工厂实现中执行此操作,以将基本参数类型转换为特定的参数类型.

就像两次调度一样,这还需要您为参数创建类型层次结构,但是需要更少的代码将它们缝合在一起(不需要访问者模式).

不过,此选项会将您的工厂实现与参数结构实现紧密耦合.

财产包

您还可以使用string-> some type词典(例如,string-> boost::any).这称为财产袋.但是,它会失去很多编译时类型的安全性,因为您基本上是在按字符串值查找所有内容.我真的不推荐.

I'm trying to design a good entity creation system with an abstract factory (as per http://www.dofactory.com/Patterns/PatternAbstract.aspx) but I'm struggling when it comes to instance specific parameters.

For example: I have two abstract factories, one for creating a projectile, and one for creating a crate

Now the factory can be either be one instance for each type, which is passed an abstract parameter set from a list (which in the base class would shared material, size etc), type specific parameters would be velocity for a projectile and durability for a crate.

But what I'm struggling with is, in the end when I have this abstract factory method which I call with parameters like a string "BulletProjectile", and "WeakCrate", I need to provide instance specific parameters, and more importantly they are of different types for different factories - for projectiles they would have position and velocity, and crate would just have position. A worse scenario is when the user or player is creating a crate or similar object, and is able to define its dimensions. How would I handle this?

解决方案

A couple options:

Rethink your usage

An abstract factory is useful if it separates the user of the factory from how the exact type is produced. The abstract factory doesn't have any restrictions on what it produces, just that it is abstract. It can return a non-abstract type, or an abstract type that isn't at the very base of your inheritance hierarchy.

If code that uses the factory already can get different sets of data to call the factory with, then the code using the factory already has some knowledge of the type that comes out of it.

Here are some options to think about:

  • Provide multiple abstract factory types, with one Create method each, such as a GrenadeFactory and a BulletFactory
  • Provide multiple methods on a single abstract factory type, such as CreateBullet and CreateGrenade
  • Stop using abstract factories. This is a good option if you don't really need abstract construction, and just need abstract types.

Remember that you can still pass a derived type (Bullet) to a method taking a base type (say, Entity or Projectile).

Double dispatch

If you're really dead set on combining abstract factories with abstract parameters, then you may want to look into double dispatch, or the Visitor Pattern. The key here is that you're trying to get two different virtual methods to be combined with each other, and get a unique combination of behavior based on those two derived types.

This would require you to create base and derived types for your parameters, so you couldn't pass simple types (like int, string, etc) without creating a custom parameter structure that derived from a base Parameters type.

It also requires a lot of extra code to implement the Visitor pattern.

RTTI

You could use the C++ Run-Time Type Information feature.

Using dynamic_cast, you can cast a base type to a derived type. You could do this in the factory implementation to cast your base parameter type to your specific parameter type.

Like double-dispatch, this would also require you to create a type hierarchy for parameters, but would require less code to stitch them together (wouldn't require the visitor pattern).

This option would tightly couple your factory implementation to a parameter structure implementation, though.

Property bag

You can also use a string -> some type dictionary (string -> boost::any, for example). This is called a property bag. It loses you a lot of compile time type safety, though, because you're basically looking everything up by string value. I don't really recommend it.

这篇关于具有抽象参数的抽象工厂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆