I have got a problem and a solution, but I am not really satisfied with it:
My problem:
I have got several classes and several “properties”. These properties are not described by a method or a logical value. They are defined by those classes, which own a property (common features).
Now there should be formed groups of classes with the same properties. These groups can be interpreted as sets.
The properties of sub-groups (subsets) do not necessarily have to do with the properties of the parent group (superset).
In addition, the compiler can check for these groups on type-correctness.
Previous solution:
For each group, a (possibly empty) interface is created. All classes with a common property implement this interface. The group and the interface is so for all classes with this property.
If we now pass a parameter to a method that should only take classes with a certain property, then the parameter type is the corresponding interface.
Description of an example:
All classes implement an interface with a method m1.
For each property, an interface is created, which is implemented by all classes that meet the appropriate property. Properties can for example be side effects that occur when running this method.
The interface is therefore a set of classes with a corresponding property. It represents the property so to speak.
Furthermore, there is another method m2, which receives as a parameter an object of a certain group of property. In m2, m1 from the given object is called. Thus we know about the side effects, which occur when running m1, or a property, which is met. It is not possible to pass m2 an object with no corresponding property/side effect, because the compiler checks for type correctness.
Code-Example:
(Side effects are presented through an output)
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);
// m2(c_3); // Is not passed by the compiler (wrong type)
}
}
interface Combinable {
public void m1();
}
interface Property_12 extends Combinable {}
interface Property_13 extends Combinable {}
interface Property_23 extends Combinable {}
interface Property_123 extends Combinable {}
class Class_1 implements Property_12, Property_13, Property_123 {
@Override
public void m1() {
System.out.println("Property (1,2)");
System.out.println("Property (1,3)");
System.out.println("Property (1,2,3)");
}
}
class Class_2 implements Combinable, Property_12, Property_23, Property_123 {
@Override
public void m1() {
System.out.println("Property (1,2)");
System.out.println("Property (2,3)");
System.out.println("Property (1,2,3)");
}
}
class Class_3 implements Combinable, Property_13, Property_23, Property_123 {
@Override
public void m1() {
System.out.println("Property (1,3)");
System.out.println("Property (2,3)");
System.out.println("Property (1,2,3)");
}
}
Why I am not satisfied with this solution?
My concern is not to a specific case, but a general problem.
The number of interfaces that you create quickly shoots in the air:
- possible combinations = power set of the set of all classes
- |P(C)| = 2^|C|
If the interfaces/groups are set up as in this case, according to a specific system,
there must be a way to solve the whole thing more elegant than to write an interface for each group. The effort to create 2^|C| interfaces is already enormous and it should be possible to reduce it at least.
If you now want to summarize several properties, you are once again facing the same problem. Thus you would have to create any combination of interfaces for an interface:
- |P(P(C))| = 2^2^|C|
If you handle the huge amount of writing, you can after all pass objects with multiple properties.
I hope my wording is fairly clear and there will be one or the other interesting proposal.
Thank you for your efforts!
There are always 2^|C| interfaces. Maybe I can use a code-generator to creat these interfaces. What do you think of this idea?
C = The set of all classes that are to be grouped.
It is important that every class has something in common with one, two, …, and |C|-1 others, so that each possible “combination of classes” (=grouping) exists.
Furthermore, no set may appear twice. It follows that the set of all groupings is the power set. My properties are these groupings.
You can ignore the one-element sets (and the empty set), since it is already the class itself. Thus 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 --| |
|____________________________|
A concrete example:
|-- PolarBear --|
| |--> White --|
| Swan -------| |--> Animal
Mammal <--| |_________ |
| | |--> Fly ----|
| |-- Bat --------| |
|___________________________________|
Code:
public class AnimalsWithProperties {
// It can be passed only white mammal.
public static void whiteAnimal(White w) {
w.shout();
}
// All animal can be passed
public static void animalShout(Animal a) {
a.shout();
}
public static void main(String[] args) {
PolarBear e = new PolarBear();
Swan s = new Swan();
Bat f = new Bat();
whiteAnimal(e);
whiteAnimal(s);
// whiteAnimal(f); // Error
animalShout(e);
animalShout(s);
animalShout(f);
}
}
interface HasAnimalProperty {
public void shout();
}
interface White extends Animal {}
interface Fly extends Animal {}
interface Mammal extends Animal {}
interface Animal extends HasAnimalProperty {}
class PolarBear implements White, Mammal {
@Override
public void shout() {
System.out.println("PolarBear: I am a white Mammal.");
}
}
class Swan implements White, Fly {
@Override
public void shout() {
System.out.println("Swan: I am white and can fly.");
}
}
class Bat implements Fly, Mammal {
@Override
public void shout() {
System.out.println("Bat: I am a flying mammal.");
}
}
If you want to treat these properties as interfaces, I would strongly consider looking at finding a way to automatically generate the classes and/or interfaces.
I would personally take a look at Clojure’s methods for generating interfaces and classes, which would quite literally allow you to pass a list of strings to generate the interfaces for. You could then easily package the resulting set of classes into a jar and use it for the rest of your project, which could be written in Java (or Clojure for that manner). Automatically generating the power set of possibly classes would also be relatively simple.
Another possibility which fits with the above is using a byte code generator to generate the interfaces and classes, though I think it would be simpler to use the above.