You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Ashish Kulkarni <ku...@yahoo.com> on 2005/01/25 22:57:09 UTC

[OT] a design question

Hi
Suppose i have a class, which required 3 parameters to
do some process, out of 3 parameters 2 are standard
and only one changes depending upon the process.
I dont want to hard code those 2 values since these
can be change (this is not a web application so i
cannot use session)
What i want to do is create one instance of this class
with the constant parameters setup, 
So a sort of Singleton pattern, 
This class will have no knowledge to read those 2
parameters, and have to be supplied by some other
class

How will be design this class, 

my class example
public class GetData
{
  private String parm1, parm2;
  public GetData(String parm1, String parm2)
   {
     this.parm1 = parm1;
     this.parm2 = parm2;
   }

  public void doSomeThing(String parm3)
   {
        // do some logic here
   }


}

ashish


		
__________________________________ 
Do you Yahoo!? 
All your favorites on one personal page � Try My Yahoo!
http://my.yahoo.com 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [OT] a design question.. have a look at this class

Posted by Ashish Kulkarni <ku...@yahoo.com>.
Hi
My problem is that the class which will need this
helper class does not know the values of param1 and
param2, and so cannot create the Instance as if it is
a java bean, these param1 and param2 values are only
know by the initialization class.
this is what i have come up with
public class GetData
{
  private String parm1,parm2;
  private boolean isInit = false;
  private static GetData instance = new GetData();
  
   private GetData()
     {
     }
   public void setInit(String one, String two)
    {
    if(!isInit)
     {
      this.parm1 = one; 
      this.parm2 = two;
      isInit = true; 
      }
    }
   public static GetData getInstance()
    {
            return instnace;
    }

   public String workString(String three)
   {
        return parm1 + parm2 + three;
   }
  
}
Ashish
--- Eddie Bush <ea...@gmail.com> wrote:

> I tend to follow the JavaBeans conventions:
> 
> public class GetData {
>   private String param1;
>   private String param2;
> 
>   public GetData() {
>   }
> 
>   public GetData(String param1, String param2) {
>     setParam1(param1);
>     setParam2(param2);
>   }
> 
>   public String getParam1() {
>     return param1;
>   }
> 
>   public void setParam1(String param1) {
>     this.param1 = param1;
>   }
> 
>   public String getParam2() {
>     return param2;
>   }
> 
>   public void setParam2(String param2) {
>     this.param2 = param2;
>   }
> }
> 
> Note that the singleton pattern has nothing to do
> with the way you
> pass parameters.  An object being a singleton means
> that there may
> never be more than one object of that type, and that
> you're guaranteed
> there never will be more than one object of that
> type.  See GoF.
> 
> Note that, to follow JavaBean conventions, if you
> specify a
> constructor that has parameters, you'll have to also
> specify one that
> does not.  The no-args constructor allows the class
> to be dynamically
> instantiated.  Following the JavaBean convention for
> properties, each
> property should have its own get/set method.
> 
> I personally like this approach better because I can
> create them
> dynamically and set their properties dynamically.  I
> get a lot of
> milage out of beans by doing this.
> 
> I didn't really see a question?  Is your question
> how to best write
> the class?  Well, you've got my 2 cents.
> 
> Good Luck,
> 
> Eddie
> 
> On Tue, 25 Jan 2005 13:57:09 -0800 (PST), Ashish
> Kulkarni
> <ku...@yahoo.com> wrote:
> > Hi
> > Suppose i have a class, which required 3
> parameters to
> > do some process, out of 3 parameters 2 are
> standard
> > and only one changes depending upon the process.
> > I dont want to hard code those 2 values since
> these
> > can be change (this is not a web application so i
> > cannot use session)
> > What i want to do is create one instance of this
> class
> > with the constant parameters setup,
> > So a sort of Singleton pattern,
> > This class will have no knowledge to read those 2
> > parameters, and have to be supplied by some other
> > class
> > 
> > How will be design this class,
> > 
> > my class example
> > public class GetData
> > {
> >  private String parm1, parm2;
> >  public GetData(String parm1, String parm2)
> >   {
> >     this.parm1 = parm1;
> >     this.parm2 = parm2;
> >   }
> > 
> >  public void doSomeThing(String parm3)
> >   {
> >        // do some logic here
> >   }
> > 
> > }
> > 
> > ashish
> 
> -- 
> Eddie Bush
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> user-unsubscribe@struts.apache.org
> For additional commands, e-mail:
> user-help@struts.apache.org
> 
> 



		
__________________________________ 
Do you Yahoo!? 
Yahoo! Mail - now with 250MB free storage. Learn more.
http://info.mail.yahoo.com/mail_250

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [OT] a design question

Posted by Eddie Bush <ea...@gmail.com>.
I tend to follow the JavaBeans conventions:

public class GetData {
  private String param1;
  private String param2;

  public GetData() {
  }

  public GetData(String param1, String param2) {
    setParam1(param1);
    setParam2(param2);
  }

  public String getParam1() {
    return param1;
  }

  public void setParam1(String param1) {
    this.param1 = param1;
  }

  public String getParam2() {
    return param2;
  }

  public void setParam2(String param2) {
    this.param2 = param2;
  }
}

Note that the singleton pattern has nothing to do with the way you
pass parameters.  An object being a singleton means that there may
never be more than one object of that type, and that you're guaranteed
there never will be more than one object of that type.  See GoF.

Note that, to follow JavaBean conventions, if you specify a
constructor that has parameters, you'll have to also specify one that
does not.  The no-args constructor allows the class to be dynamically
instantiated.  Following the JavaBean convention for properties, each
property should have its own get/set method.

I personally like this approach better because I can create them
dynamically and set their properties dynamically.  I get a lot of
milage out of beans by doing this.

I didn't really see a question?  Is your question how to best write
the class?  Well, you've got my 2 cents.

Good Luck,

Eddie

On Tue, 25 Jan 2005 13:57:09 -0800 (PST), Ashish Kulkarni
<ku...@yahoo.com> wrote:
> Hi
> Suppose i have a class, which required 3 parameters to
> do some process, out of 3 parameters 2 are standard
> and only one changes depending upon the process.
> I dont want to hard code those 2 values since these
> can be change (this is not a web application so i
> cannot use session)
> What i want to do is create one instance of this class
> with the constant parameters setup,
> So a sort of Singleton pattern,
> This class will have no knowledge to read those 2
> parameters, and have to be supplied by some other
> class
> 
> How will be design this class,
> 
> my class example
> public class GetData
> {
>  private String parm1, parm2;
>  public GetData(String parm1, String parm2)
>   {
>     this.parm1 = parm1;
>     this.parm2 = parm2;
>   }
> 
>  public void doSomeThing(String parm3)
>   {
>        // do some logic here
>   }
> 
> }
> 
> ashish

-- 
Eddie Bush

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org