There is a class Rules, which has in itself some set of rules. Several cards, a pair of primitives, etc.
There is an interface / abstract class CellMatrix .
There are several implementations of it, which in the constructor accept the Rules class for initialization:

 class CellMatrixArray implements CellMatrix{ CellMatrixArray(Rules r){} } class MapMatrixArray implements CellMatrix{ MapMatrixArray(Rules r){} } 

There is a main class that contains this CellMatrix . Implementation, of course, is unknown.

 class PlayerField{ PlayerField(Rules r){} private CellMatrix matrix; //назначается сеттером или конструктором. } 

But, apparently, this class also accepts Rules for initialization. I really do not like it, that I have to poke this initializer several times into different classes, especially classes connected by aggregation. I got such a stupid initialization scheme:

alt text

I cannot create CellMatrix objects inside PlayerField - I need to provide for the possibility of inheritance / implementation change. I'm thinking, maybe an abstract factory to create something like this:

 interface CellMatrixFactory{ void setRules(Rules r); CellMatrix createMatrix(); } 

And send this factory to PlayerField. Those. I’ll get rid of sending Rules to several places, sending it to PlayerField once, calling factory setRules and creating CellMatrix.

    2 answers 2

    There is not enough data for a full answer.

    How will PlayerField use CellMatrix?

    What is and how CellMatrix and Rules are related (more precisely, what does the rule set within CellMatrix affect where this logic is implemented)?

    Does CellMatrix perform any actions or just contain data to process?

    Why it is impossible to have the CellMatrix.getRules () method, which will return the PlayerField rules that are relevant to this CellMatrix object? Then you do not need to fence gardens with patterns.

    Is CellMatrix an integral part of PlayerField or is it only used by the latter to process some kind of data inside CellMatrix?

    In general, so that you can advise something, give more information.

      Most likely you will have to use the Strategy pattern in conjunction with the Factory method

      • one
        If it’s easy to give links to the templates described in your answer - Barmaley
      • All the necessary material you will find in this book is read and perceived very easily twirpx.com/file/1023157 - Andrei Bordak