-1

I have to model a 15-puzzle game board in Ruby. As a representation, I chose to use a multidimensional array (as a classic 2x2 matrix) with rows and columns.

My question is: is it better to subclass Array, in order to store all the relevant informations inside self or is it better to create a new class, and internally use an instance variable to keep track of the multidim array?

The API is meant to be the same in both cases, my question is about the implementation.

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
whatyouhide
  • 15,897
  • 9
  • 57
  • 71
  • It is not clear what you mean by "store all the relevant informations [information] inside `self`". – sawa Oct 13 '13 at 16:09
  • 1
    @sawa `self` being the instance of the subclass of Array. – Andrew Marshall Oct 13 '13 at 16:11
  • @AndrewMarshall I understand that, but how is the OP saying to store information inside the array instance? As its elements? That is definitely a code smell. – sawa Oct 13 '13 at 16:14
  • In addition, it is not clear how a 2x2 matrix is relevant to a 15-puzzle. – sawa Oct 13 '13 at 16:15

2 Answers2

2

Your class will not be an array, so don't subclass it. It might be implemented with an array, but it isn't one.

Subclassing would violate LSP (Liskov Substitution Principle).

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
0

If You explore more on different OOD patterns, You will discover that one of the most common advices is to favour composition over inheritance. Inheritance might be fine for highly a hierarchical data structure. But in most cases You will just introduce extra dependencies.

Edgars Jekabsons
  • 2,833
  • 15
  • 20