João Marques
João Marques

Reputation: 141

How can I know when a instance is created?

So I made a simple program that allows you to create instances of a ton of classes. Now I'm responsible to send the instances created to a server. I really like the classes constructors so I really didn't want to alter them. How could I listen to this program so that I could know what classes were recently created, i was thinking in using reflection and maybe threads?

Here a shorter example of what i want to accomplish:

public class MainApplicaton{

     public static void main(String []args){
        ConnectServer.listenToCreatedInstances().
        new Vase();
        new Dog();
        new House();
     }
}

package stuff.components;
public class Human{
    public Human(){

    }
}

package stuff.components;
public class Dog{
    public Dog(){

    }
}

package stuff.components;
public class House{
    public House(){

    }
}

Now my listener thread:

   public enum ConnectServer {
        Server;

        public void listenTocreatedIntances(){
            //Something happens here
            Class c ..
            System.out.println("A instance of "+c.getName());
        }
    }

Upvotes: 0

Views: 338

Answers (2)

João Marques
João Marques

Reputation: 141

Another way to answer this question would be to with the FactoryPattern. Every time we register an object we notify the singleton.

public class MainApplicaton{

     public static void main(String []args){
        Vase vase = new Vase();
        Dog dog = new Dog();
        House house = new House();
        ConnectServer.listenToCreatedInstances(vase);
        ConnectServer.listenToCreatedInstances(dog);
        ConnectServer.listenToCreatedInstances(house);
     }
}

In this Singleton, we would receive the element created and apply the desired behavior.

public enum ConnectServer {
        Server;

        public void listenTocreatedIntances(Component component){
            //Something happens here
            System.out.println("A instance of "+component.getClass().getName());
        }
    }

Upvotes: 0

Joni
Joni

Reputation: 111239

A relatively easy way to achieve this is by introducing a common parent class, and using the parent class constructor to generate events. Subclass constructors always call the parent class constructor.

package stuff.components;
class Component {
    public Component() {
        ConnectServer.Server.onInstanceCreated(this.getClass());
    }
}

class Human extends Component {
    public Human(){
        // implicit call to Component constructor
    }
}

class Dog extends Component{
    public Dog(){
        // implicit call to Component constructor    
    }
}

If you want to get events from instance creation without any code modifications whatsoever, you'll have to look further than Java. Your options include:

  • aspect oriented programming, for example aspectj: it lets you inject code when specific things happens in a program (for example constructor calls)
  • inject code into the subclass constructors at run time using a byte code manipulation library such as ASM

Upvotes: 1

Related Questions