b4da
b4da

Reputation: 3108

Which design pattern am I using?

I am developing a game. Instead of creating a class for each game entity I created an entity class and a feature class. Entity objects keep location, name, id and a list of entity features. I have bunch of feature classes derived from feature like movefeature, fuelfeature, displayfeature... If I need a moving entity I add movefeature to it. If it is a vehicle and burns fuel, it gets fuelfeature. Entities shown on screen have displayfeature and so on.

This probably is a design pattern and someone invented it before me. My question: is this method a valid pattern and what it is called?

Upvotes: 2

Views: 115

Answers (2)

famousgarkin
famousgarkin

Reputation: 14116

What you describe is really just composition and favoring composition over inheritance, which is usually a good idea.

As for patterns, I'd say you prolly use a lot of them, conceptually at least if not strictly correct implementation-wise. Take a look at the patterns listing and you'll be able to indentify a bunch yourself just by the name and short description. By what you say I'd guess you might be using the following:

  • Decorator, Strategy, Composite - to enable composition of complex entities from standalone parts and features,
  • Factory, Builder - to automate creation of such complex entities,
  • State, Observer, Mediator, Visitor, eventing, messaging - to enable entities and environment interactions,
  • Module - to organize all this code,
  • Singleton - as codebases just tend to have some every so often,
  • and probably something for concurrency

See, patterns are like vegetarian thali, you just end up with mix and match of some, aware of em or not.

Upvotes: 1

M. A. Kishawy
M. A. Kishawy

Reputation: 5079

It can be more than one pattern depending on how you design/implement them. I can see some of these possible patterns in there:

enter image description here

enter image description here

enter image description here

Upvotes: 0

Related Questions