I have a problem and solution, but I'm not very happy with this:
My problem:
I have several classes and several "properties". These properties are not described by a method or a boolean. They are defined by those classes that have a property (common features).
Now the groups of classes with the same properties should be formed. These groups can be interpreted as sets. The properties of subgroups (subsets) are not necessarily related to the properties of the parent group (superset).
In addition, the compiler can check these groups for correctness by type.
Previous solution:
An (possibly empty) interface is created for each group. All classes with a common property implement this interface. Group and interface for all classes with this property. If you now pass the parameter to a method that should only accept classes with a specific property, then the parameter type is the appropriate interface.
Description of the example:
All classes implement the interface using the m1 method.
An interface is created for each property, which is implemented by all classes that correspond to the corresponding property. Properties can be, for example, side effects that occur when you start this method.
Thus, an interface is a collection of classes with the corresponding property. It is a property, so to speak.
In addition, there is another method m2, which receives as an parameter the object of a certain group of properties. In m2, m1 is called from the given object. Thus, we are aware of the side effects that occur when you start m1 or the property that is running. It is not possible to pass an m2 object without the corresponding property / side effect, because the compiler checks the type is correct.
Example Code:
(Side effects are presented through exit)
public class Example { public static void m2(Property_12 p_12) { p_12.m1(); } public static void main() { Class_1 c_1 = new Class_1(); Class_2 c_2 = new Class_2(); Class_3 c_3 = new Class_3(); m2(c_1); m2(c_2);
Why am I not satisfied with this decision?
My concern is not a specific case, but a general problem. The number of created interfaces is growing rapidly in the air:
- possible combinations = set power of a set of all classes
- | P (C) | = 2 ^ | C |
If the interfaces / groups are configured, as in this case, according to a specific system, there should be a way to solve everything more elegant than writing an interface for each group. Efforts to Create 2 ^ | C | the interfaces are already huge, and if possible you can reduce it at least.
If now you want to summarize several properties, you are again faced with the same problem. Thus, you will need to create any combination of interfaces for the interface:
- | P (P (C)) | = 2 ^ 2 ^ | C |
If you process a huge number of letters, you can transfer objects with several properties.
I hope that my wording is clear enough, and there will be one or the other interesting proposal.
Thanks for your efforts!
There is always 2 ^ | C | interfaces. Perhaps I can use a code generator to create these interfaces. What do you think of this idea?
C = The set of all classes that should be grouped.
It is important that each class has something in common with one, two, ... and | C | -1 others, so that every possible "combination of classes" (= grouping) exists. In addition, no set can appear twice. It follows that the set of all groups is a set of degrees. My properties are groupings.
You can ignore singleton sets (and empty sets), since this is the class itself. So the formula is actually:
|P(C) \ ({ {x} : x in C } add "empty set")| = |P(C)| - |C| - 1 = 2^|C| - |C| - 1
Example:
| C | = 2 => | P (C) | - | C | - 1 = 2 ^ | C | - | C | - 1 = 2 ^ 2 - 2 - 1 = 4 - 2 - 1 = 1 (= groupings / interfaces)
Class_1 --| |--> P_12 Class_2 --|
Example:
| C | = 3 => | P (C) | - | C | - 1 = 2 ^ | C | - | C | - 1 = 2 ^ 3 - 3 - 1 = 8 - 3 - 1 = 4 (= groupings / interfaces)
|-- Class_1 --| | |-- P_12 --| | Class_2 --| |-- P_123 P_13 --| |______ | | | |-- P_23 --| | |-- Class_3 --| | |____________________________|
Specific example:
|-- PolarBear --| | |--> White --| | Swan -------| |--> Animal Mammal <--| |_________ | | | |--> Fly ----| | |-- Bat --------| | |___________________________________|
Code:
public class AnimalsWithProperties {