dimanche 5 novembre 2017

Two instances of an object with nested objects compare with dynamic (options/rules/args)

How to differ/compare two instances of an object with nested objects and more IMPORTANTLY with dynamic compare (options/rules).

So basically I don't know what of the object attributes I need to compare. The options/rules vary and are added dynamically by a user through some UI. Of course one could easily create a lot of methods with a lot of if statements. This is not what I'm looking for I have many different objects that needs this functionality.

I've thought of using Reflection, but how would this do with many nested objects? Like obj A has obj B. Obj B has a List of obj C and each of C has obj E?? That would make reflection pretty complicated right?

I've also thought of Chain of Respoibllity, but still a solution is not quite clear to me..

Example

Say I have an object A. This object contains some standard data types: int, bool etc. Also it has the object B which then have some standard data types as well. I now have 2 instances of A which I need to compare. But not just a simple compare of all attributes (CompareTo). Only what the user or some rules (preferably some strings) says that needs to be compared. How do I proceed???

public class A {
    int number;
    boolean bool;
    C other;
    List<B> list;

    public A (..... some args){ ... }

    void dynamicCompare(String [] rules ){
        // compare using args
    }
}

public class B {
    int number;
    boolean bool;
    D other;

    public B (..... some args){ ... }
}





Aucun commentaire:

Enregistrer un commentaire