0

I'm still studying OOP designs, so what's the best way to achieve an inventory for a simple flash game ? It seems that more than one design pattern could deliver some kind of an invetory but I would lose the flexibility if I try to adapt it somehow without a good knowledge about the subject.

For money to buy what is available in an inventory I thought of Singleton. If there's enough cash earned while playing the game, then one can buy new skills.

Maybe decorator pattern could list many thumbnails as buttons, and clicking on it applies new features and skills to the character.

I'd like to read standard advices on solving this problem, because I feel I'm on the wrong way. Thanks.

Michael J. Barber
  • 22,744
  • 8
  • 61
  • 84
Matt
  • 43
  • 1
  • 1
  • 4
  • multi or singleplayer? Because on singleplayer game you could easily get away with a gold counter and a list/hashmap of the skills. – Daniel Iankov Aug 23 '11 at 21:57
  • Daniel Iankov, it's going to be singleplayer. – Matt Aug 23 '11 at 22:07
  • So no transactions thank god. Then a singleton that returns all available skills in a hashmap is good solution. You then remove/disable/highlight the ones the player already have and you create a small view to visualise them and just bubble some events back when you get a click. – Daniel Iankov Aug 23 '11 at 22:22

1 Answers1

2

Stay away from singleton if possible

Singleton has its uses, however I believe it's overused in a lot of cases.

The biggest problem with a singleton is that you're using Global State, which is generally regarded as a bad thing as when complexity in your software grows it can cause you to have unintended side effects.

Object composition might be a better way

For games you might want to take a look at using Object Composition rather than traditional OOD Modelling.

A software component is a software element that conforms to a component model and can be independently deployed and composed without modification according to a composition standard.

A component model defines specific interaction and composition standards. A component model implementation is the dedicated set of executable software elements required to support the execution of components that conform to the model.

A software component infrastructure is a set of interacting software components designed to ensure that a software system or subsystem constructed using those components and interfaces will satisfy clearly defined performance specifications.

Reading over the material in the first link should give you some excellent ideas on how to model your inventory system and have it extendable in a nice way.

Community
  • 1
  • 1
Justin Shield
  • 2,360
  • 14
  • 12
  • +1 I have been designing a component based game engine and so far it is working out really well and giving me a lot of flexibility. Also, in cases where a singleton might be used, I think the Service Locator pattern is better. See this http://martinfowler.com/articles/injection.html#UsingAServiceLocator – Allan Aug 23 '11 at 23:22
  • @Allan - I'd also avoid using a service locator as well. The service locator pattern is generally recognised as an Anti-Pattern. Even though it was coined by Martin Fowler. It's better to use parameter based Dependency Injection as opposed to a service locator. [is-it-a-code-smell-if-an-object-knows-a-lot-of-its-owner/100549#100549](http://programmers.stackexchange.com/questions/100537/is-it-a-code-smell-if-an-object-knows-a-lot-of-its-owner/100549#100549) – Justin Shield Aug 24 '11 at 00:25
  • Yeah I have read that anti-pattern article too. Certainly some are against it and some for it. I find it useful, particularly in game development http://gameprogrammingpatterns.com/service-locator.html – Allan Aug 24 '11 at 01:15