factory method problem

Started by
1 comment, last by LorenzoGatti 14 years ago
I created 2 model loaders, an MD2 and a RAW Faces model loader. The MD2 model loader can do animations. The RAW model loader can't do animations. I use a factory method to get an instance of the model, for example:

model = modelFactory.LoadModel("test.md2");
or
model = modelFactory.LoadModel("test.raw");
I can then access the methods that apply for both types, such as Draw(); However, if the model is in MD2 format I want to use the Play() (play animation) method. But I'm talking to an interface and can't access it. I want that, if the model is an MD2 I can use the Play() method, but if it's an RAW, the method shouldn't be available. How can I access the method Play()? Do I use the right pattern?
Advertisement
Your factory method is creating objects that implement an interface. Therefore, if you want to take objects from that factory and use methods outside of that interface, you are using the wrong factory method.

Either:
- extend the interface to provide the method you want (eg. make Play() exist for RAW, but do nothing)
- create a new interface (eg. AnimatedModel) with the extra method, and a new factory method that only creates AnimatedModels. (This would fail if passed "test.raw".)
Maybe you could implement the animation control methods in the RAW model class to display the same still pose at all times, with only one (animated) model interface.

The factory would return a possibly animated model object and you should be calling its animation control methods to treat it uniformly with other models and in the most general way.

Tomorrow, when you improve your assets, you might just replace the still RAW model with a MD2 animated one and it would be animated without additional interface juggling effort.

Can you explain the animation-related methods and metadata of your model objects in more detail?

Omae Wa Mou Shindeiru

This topic is closed to new replies.

Advertisement