The Artima Developer Community
Sponsored Link

Design Forum
static or non-static

10 replies on 1 page. Most recent reply: Aug 12, 2003 3:22 AM by Vincent O'Sullivan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 10 replies on 1 page
prabhakar

Posts: 1
Nickname: pprabhala
Registered: Jul, 2003

static or non-static Posted: Jul 7, 2003 9:41 AM
Reply to this message Reply
Advertisement
hi guys,

I have a question.

Having a class with all static methods is a good design ?? or class with public constructor and methods is a good design? when to use static? when not to use static?

Could somebody enlighten me on this.

Thanks for the help.


Shashidhar

Posts: 1
Nickname: pachange
Registered: Jul, 2003

Re: static or non-static Posted: Jul 13, 2003 5:19 AM
Reply to this message Reply
Hi Guys ,

Having a class with all static methods is NOT a good design . A class with private attributes and public methods to access those varibales is a good design.

Regards,
Shashidhar Pachange
> hi guys,
>
> I have a question.
>
> Having a class with all static methods is a good design ??
> or class with public constructor and methods is a good
> design? when to use static? when not to use static?
>
> Could somebody enlighten me on this.
>
> Thanks for the help.

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: static or non-static Posted: Jul 15, 2003 4:43 AM
Reply to this message Reply
> Having a class with all static methods is NOT a good
> design . A class with private attributes and public
> methods to access those varibales is a good design.

These are sweeping generalisations that are essentially meaningless.

There are cases where function classes (classes with only static methods) are a perfectly good solution to a problem. Java's Math class is the most often quoted example. That being said, the number of situations where this arises is low. Their presence in a design indicates that the designer is thinking of classes as things that operate on data in a procedural manner, rather than classes as things that represent data and encapsulate the 'ability' of the data to be manipulated in an object oriented manner.

Equally, the existence of private attributes and public methods in a class does not lead one to conclude that the class is well designed. Indeed, it is not difficult to find articles on the Internet that describe why the traditional "private variable plus public getter and setter" design is fundamentally flawed.

Vince.

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: static or non-static Posted: Jul 17, 2003 2:44 AM
Reply to this message Reply
> Indeed, it is not difficult to
> find articles on the Internet that describe why the
> traditional "private variable plus public getter and
> setter" design is fundamentally flawed.

I take it back. It is difficult. I'm sure Bertrand "Eiffel" Meyer wrote such an article, but I can't find it. Does anyone else know of the item I'm referring to?

Vince.

Adam McIntosh

Posts: 2
Nickname: adammc
Registered: Jul, 2003

Re: static or non-static Posted: Jul 18, 2003 5:31 AM
Reply to this message Reply
> > Indeed, it is not difficult to
> > find articles on the Internet that describe why the
> > traditional "private variable plus public getter and
> > setter" design is fundamentally flawed.
>
> I take it back. It is difficult. I'm sure
> Bertrand "Eiffel" Meyer wrote such an article, but I can't
> find it. Does anyone else know of the item I'm referring
> to?
>
> Vince.

Perhaps you can explain the gist of why it's fundamentally flawed ?

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: static or non-static Posted: Jul 28, 2003 2:56 AM
Reply to this message Reply
I'm not sure I'm eloquent enough to put it over very convincingly but, as I recall, the argument goes something along the lines of:

Given a Money object; it might contain an amount field:
class Money1
{
    public int amount;
}
We could then use it as follows:
        Money1 m11 = new Money1();
        Money1 m12 = new Money1();
        Money1 m13 = new Money1();
 
        m11.amount = 3;
        m12.amount = 5;
        m13.amount = m11.amount + m12.amount;

Most comentators simply advise that the amount variable should be private and should be accessed via a matching pair of public getAmount and setAmount methods:
class Money2
{
    private int amount;
    public int  getAmount()             { return amount;      }
    public void setAmount(int newAmount){ amount = newAmount; }
}
This is a minimal solution which is better than nothing, but not much. To add two Money objects, we might write the following code:
        m21.setAmount(3);
        m22.setAmount(5);
        m23.setAmount(m21.getAmount() + m22.getAmount());
Unfortunately, this is procedural not object-oriented thinking. The manipulation of the Money objects is still taking place outside the object. We have encapsulated the variable but then immediately re-exposed it (and its int implementation) via the accessor methods without really achieving anything useful. The manipulation of the money is taking place as integer mathmatics outside the Money object.
What is really required is that the concepts required for manipulating money are encapsulated inside the Money object and that the amount variable and how it is implemented internally becomes further hidden. Thus the raw accessors become package protected and the public methods completely hide the internal implementation:
class Money3
{
    private int amount;
    
    // Raw data accessors shouldn't be public.
    int  getAmount()             { return amount;      }
    void setAmount(int newAmount){ amount = newAmount; }
 
    public Money3(int initialAmount) { amount = initialAmount; }
    
    /** This method (and possibly others) replaces the public getAmount. */
    public String toString()
    {
        // Improve this code to properly format output..
        return new Integer(amount).toString();
    }
    
    /** This method (and possibly others) replaces the public setAmount. */
    public void add(Money3 moreMoney)
    {
        amount += moreMoney.getAmount();
    }
}
Other than the constructor (and this too could be improved), all information relating to the internal storage and manipulation within the Money object is now hidden from the user. The user of the Money object now requests the required services from the object rather than performing those services (e.g. adding money amounts) for the object.
        Money3 m31 = new Money3(3);
        Money3 m32 = new Money3(5);
        Money3 m33 = new Money3(0);
 
        m33.add(m31);
        m33.add(m32);

Vince.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: static or non-static Posted: Jul 28, 2003 8:53 AM
Reply to this message Reply
Looks like you can add a negative amount and come up with some "funny" money.

Perhaps a private amount controller method could be used to prevent the loss of your money with a boolean return on whether a transaction succeeds.

I am with you on the class design.

Some things surely could be best modelled with all static methods. Scientific formulas fit in this category. The class just does the math and any conversions.

Why have to go through the bother to construct a class so you can use a method that just needs some arguments as input. It would work like the functions and subroutines of old languages like Fortran.

Basically Money is just Math with some specific kinds of units based on having at least one of the minimum amount.
It could all be done statically.

But I like the idea of having a basic Money object which can be extended into all the varieties of the world's money types and monetary systems and how they actually work in real life.

Then there are stocks and bonds which can extend these extended Money objects and implement all kinds of real world financial market business functions.

Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Re: static or non-static Posted: Jul 28, 2003 10:30 AM
Reply to this message Reply
I wrote a bit about his issue here that may help:

http://www.artima.com/objectdesign/object9.html

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: static or non-static Posted: Jul 28, 2003 11:45 PM
Reply to this message Reply
> Looks like you can add a negative amount and come up with
> some "funny" money.

I have a bank account for which such an object would be ideal!

V.

David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: static or non-static Posted: Jul 31, 2003 6:45 AM
Reply to this message Reply
"Indeed, it is not difficult to find articles on the Internet that describe why the traditional "private variable plus public getter and setter" design is fundamentally flawed."

A point worth making is that such a design is to encourage encapsulation - allowing the implementation of the method to change, but the interface to remain constant.

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: static or non-static Posted: Aug 12, 2003 3:22 AM
Reply to this message Reply
>> "Indeed, it is not difficult to find articles on the
>> Internet that describe why the traditional "private
>>variable plus public getter and setter" design is
>> fundamentally flawed."
>
> A point worth making is that such a design is to encourage
> encapsulation - allowing the implementation of the method
> to change, but the interface to remain constant.

This is partly true. If the internal representation was an integer with corresponding get and set methods - it would be necessary to change the interface get and set methods, if the internal representation to a long. This could be done legally by deprecating the old integer based methods and replacing them with new long based method. Better though would be to avoid getters and setters that are closely tied to, and expose, the internal representations and replace them with methods that reflect the object's behaviour.

Vince.

Flat View: This topic has 10 replies on 1 page
Topic: what's problem if a user perform multi roles at the same time after login? Previous Topic   Next Topic Topic: Framework for Application to Detect Database Failure

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use