This discussion is archived
2 Replies Latest reply: Nov 3, 2012 1:27 PM by mrsAngelo RSS

relations between classes

mrsAngelo Newbie
Currently Being Moderated
Hi,

recently I had problem using relations between classes in a case
where panels (implemented with classes) was nested many times in other panes
and needed also have collaboration with other classes that had to elaborate data
inside component implemented in those panels.

It was a case of relations M:N and was not trivial to find in general way to get
connection in a such kind of dependencies between classes.

Looking in Internet I found some good articles to explain the problem...
----
look
Some Patterns for Relationships. James Noble. MRI, School of MPCE,. Macquarie University, Sydney. kjx@mri.mq.edu.au. September 2, 1996. Abstract ...
----
But I didn't find nothing of practical for manage my problem.

Only had some ideas that drived me to build a list where classes was registered
at the moment of them instantiation, and was afterward possible to get
the objects using the name of the classes.

This is the code of my solution
import java.util.ArrayList;

public final class ManagerForTheConnections {

    static private ArrayList aList;

    static void addInstanceToTheList(Object obj_) {
        if (aList == null){
            aList = new ArrayList<Object>();
        }
        aList.add(obj_);
        System.out.println("Size arrayList = " + aList.size());
    } // addInstanceToTheList()

    /**
     *
     * @param className
     * @return
     */
    static Object getInstanceFromTheList(String className) {
//
        Object oggetto = null;
        int intems = aList.size();
        for (int index = 0; index < intems; index++) {
            Object oggetto_ = aList.get(index);
            //---
            Class cla =oggetto_.getClass();
            String nomeDellaClasse= cla.getSimpleName();
            //---
            if (className.compareToIgnoreCase(nomeDellaClasse) == 0) {
                oggetto = oggetto_;
                break;
            }
        }
        return oggetto;
    } // getInstanceFromTheList()
    //
} // class ManagerForTheConnections

// interface that class that have to be registered have to implemrnt
interface ListOfRegisteredClasses_INTERFACE {
    public void registerTo_ConnectionsManager(Object thisClassIntance);
} // serve solo per la conv
below is a possible use of this class:
public class Main {

    public static void main(String[] args) {
        TestOne testOne = new TestOne();
        // -----
        TestTwo testTwo = new TestTwo
        // -----
        TestTree testTree = new TestTree();
        // -----
        TestFor testFor = new TestFor();
        // -----. 
        // -----


        TestTree instOfClass = 
        (TestTree) ManagerForTheConnections.getInstanceFromTheList("TestTree");
        instOfClass.methodXtestOne();

    }
}


/**
 * 
 * @author Owner
 */
class TestOne implements ListOfRegisteredClasses_INTERFACE {

    public TestOne() {
        ManagerForTheConnections.addInstanceToTheList(this);
    }

    void methodXtestOne() {
        JOptionPane.showMessageDialog(null, "methodXtestOne");
    }

    public void registerTo_ConnectionsManager(Object this_) {
    }
}

/**
 * 
 * @author Owner
 */
class TestTwo implements ListOfRegisteredClasses_INTERFACE {

    public TestTwo() {
        ManagerForTheConnections.addInstanceToTheList(this);
    }

    void methodXtestOne() {
        JOptionPane.showMessageDialog(null, "methodXtestTwo");
    }

    public void registerTo_ConnectionsManager(Object this_) {
    }
}

/**
 * 
 * @author Owner
 */
class TestTree implements ListOfRegisteredClasses_INTERFACE {

    public TestTree() {
        ManagerForTheConnections.addInstanceToTheList(this);
    }

    void methodXtestOne() {
        JOptionPane.showMessageDialog(null, "methodXtestTree");
    }

    public void registerTo_ConnectionsManager(Object this_) {
    }
}

//

/**
 * 
 * @author Owner
 */
class TestFor implements ListOfRegisteredClasses_INTERFACE {
    public TestFor() {
        ManagerForTheConnections.addInstanceToTheList(this);
    }

    void methodXtestOne() {
        JOptionPane.showMessageDialog(null, "methodXtestFor");
    }

    public void registerTo_ConnectionsManager(Object this_) {
    }
}
This solution is anyhow out of my experience and I should like to know if it is
some other way to treat better this problem and also a judgment about this solution.

Thank you

regards

Angelo Moreschini

Edited by: mrsAngelo on Nov 2, 2012 8:25 AM

Edited by: mrsAngelo on Nov 2, 2012 8:29 AM

Edited by: mrsAngelo on Nov 2, 2012 8:31 AM
  • 1. Re: relations between classes
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    One other solution of course is to create a architecture/design which doesn't have complex many to many couplings.

    Another variation is one in which one identifies several specific and limited (needed) couplings and then creates specific pipelines to support those. Somewhat similar to what you have but one which specifically embodies the limitation. So several pipelines rather than one.
  • 2. Re: relations between classes
    mrsAngelo Newbie
    Currently Being Moderated
    thank you for your answer jshell,


    I read in the article of James Noble:
    Relationships between ob jects are almost as important to designs as objects 
    themselves  
    . . . .
    
    Unfortunately  most  object oriented programming  languages  do not  support 
    relationships well ....
    this time is not easy to change the design to cohere to the logic of program.

    I am happy, anyhow, to know that what I thought is an acceptable pattern. . .

    Regards good week end

    Angelo

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points