java - Can a custom class know the name of the object that called it? -


is there anyway, when calling method through object (instance) method know instance (object) called it?

here's example (pseudo code) of mean:

pseudo code example

public class customclass{   public void mymethod(){       if (calling method object1){      //do here      }          else {          //do else          }           }//end of method   }//end of class 

and in class:

public someotherclass{  customclass = object1;  public void someothermethod(){  object1 = new customclass();  object1.mymethod();    //this call 1st condition calling object object1, if other object name, call 2nd condition.      }//end of method  }//end of class 

possible work-around

the way i've found method take argument, 'int' , check value of int , perform whichever part of 'if else' statement relates (or 'switch' statement if using 'int' value) seems messy way of doing it.

what need strategy pattern

public abstract class customclass {     public abstract void mymethod(); }  public class impl1 extends customclass {     @override     public void mymethod() {         //     } }  public class impl2 extends customclass {     @override     public void mymethod() {         // else     } } 

use way

public static void main(string[] args) {     customclass myobject = new impl1();     // or customclass myobject = new impl2(); } 


comment says need perhaps template method pattern

public abstract class customclass {     public void mymethod(){ // template method         // common things         thedifferentthings();     }      public abstract void thedifferentthings(); }  public class impl1 extends customclass {     @override     public void thedifferentthings() {         //     } }  public class impl2 extends customclass {      @override     public void thedifferentthings() {         // else     } } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -