jeudi 12 novembre 2020

how to call a method from same object in different packages in java?

I have different packages like com.pack01 , com.pac02 so on.. for one of my object A , i need to this object everytime to create something based on version parameter.

E.g If I have version "01" then it should create that object from com.pack01 ..If I have version "02" then it should create that object from com.pack02so on and so forth.

Once i get this object , i will call one create() method from it.

I am thinking of using a factory pattern here but confused how do i get particular object of the type from it as i cannot return an Object everytime because i need to perform further operation of create() on it ?

Is there any other way i can achieve the expected functionality ?

Current sample code :

A a = new  A();
a.create();

Expected code:

if(version.equals("01"))
com.pack01.A a = new com.pack01.A();
a.create();

I need to do it in a better way as per the versions .can anybody suggest a better practice as i am new design patterns in java?

Aucun commentaire:

Enregistrer un commentaire