Exposing lists as class properties / attributes
Hi folks.
Hopefully I won't mess up on my first post but here goes.
I'm currently in the process of migrating my VB / VB.NET knowledge of 12 years to Java, using Netbeans as my chosen IDE. Things are starting to sink in but I'm currently stuck with one thing which I've taken from granted in VB but can't seem to achieve in Java.
In VB.NET you can expose generic collections as properties of a class. For example if we have a class called MyProject, you can have a property called 'Components' which is simply a reference to a collection within the class. This allows you to reference the collection's properties and methods directly in the following way ('Contains' is a property of many VB.NET generic collections)...
Code :
MyProject.Components.Contains(MyComponent)
I know that in Java you use the concept of 'getters' and 'setters', but what would be the accepted method of achieving the above functionality, within the standards of the Java language?
Any help appreciated.
Thanks.
Re: Exposing lists as class properties / attributes
I guess I'm not understanding what you're looking for, but can't you just use a List?
Re: Exposing lists as class properties / attributes
How about having a get method that returns a reference to the property.
The way you have coded it looks like how defining the property as public static would work.
Re: Exposing lists as class properties / attributes
Quote:
Originally Posted by
KevinWorkman
I guess I'm not understanding what you're looking for, but can't you just use a List?
Hi Kevin and thanks for the response.
I'm don't think I quite understand your response, but are you saying that the list should just be declared and publicly accessible without a 'getter' and 'setter'? I know in VB.NET that this principle is generally frowned upon, and that all attribute member variables should only be set and retrieved via a Property Get or Property Set method.
Quote:
Originally Posted by Norm
Re: Exposing lists as class properties / attributes
How about having a get method that returns a reference to the property.
The way you have coded it looks like how defining the property as public static would work.
Hi Norm.
Is it valid to have member variables declared directly without implicit methods to get and set them?
Re: Exposing lists as class properties / attributes
Quote:
Is it valid to have member variables declared
yes, but it is not good coding practice. It violates some OOP principles.
Re: Exposing lists as class properties / attributes
That's what I thought! :o
In VB.NET a collection object is a reference type; the object is never physically moved or copied. All you pass around when dealing with collections is a pointer to that collection. This is probably why VB.NET lets you interact with the object directly using the syntax
Code :
MyClass.MyCollectionProperty.MyCollectionPropertyMethod()
I'm assuming then that there is no way to do this in Java without cheating (i.e. setting the collection variable to public and accessing it directly)? If I wanted to be 100% OOP compliant I'd have to do the following (not syntactically correct but hopefully you'll understand)...
MyClass Definition
Code :
private collection myCollection;
private collection getMyCollection() {
return myCollection;
}
private void setMyCollection(collection collectionOject) {
myCollection = collectionObject;
}
Calling Code
Code :
private collection SomeCollection;
private MyClass SomeClass;
public void main() {
// Create new instance of class
SomeClass = new MyClass;
// Copy the object to a local instance variable
SomeCollection = SomeClass.getMyCollection();
// Set some properties and / or do stuff
SomeCollection.setThisProperty = PropertyValue;
SomeCollection.Method();
// Replace the collection in the class instance with the modified one
SomeClass.setMyCollection = SomeCollection;
}
Re: Exposing lists as class properties / attributes
Java coding conventions uses lowercase first letters for variables:
Code :
private MyClass someClass;
A constructor is called like a method(with ()s)
Code :
someClass = new MyClass();
Re: Exposing lists as class properties / attributes
Is the VB code somehow more OOP-compliant than what we've been suggesting?
Re: Exposing lists as class properties / attributes
Quote:
Originally Posted by
KevinWorkman
Is the VB code somehow more OOP-compliant than what we've been suggesting?
Not sure but this may well be the case. Simply referencing a variable from within a class as a public entity is definitely frowned upon, but achieves the same result from a functional perspective.
Code :
Public Class MyClass
' Publicly exposed member variable of MyList class
Public Shared MyList As Generic.List(of String)
End Class
... is fundamentally the same as ...
Code :
Public Class MyClass
' Private member variable in which to store the property value - cannot be seen directly by calling code
Private _myPrivateList As Generic.List(of String)
' Property construct exposing Get and Set functionality
Public Property MyList() As Generic.List(of String)
Get
Return _myPrivateList
End Get
Set(ByVal value As Generic.List(of String))
_myPrivateList = value
End Set
End Property
End Class
However like Norm said, the first example doesn't conform to true OOP methods, although it is perfectly functional so far as the program is concerned. To demonstrate how the property is accessed, lists in VB.NET have a Contains(entity) method which returns TRUE if the list contains the entity supplied (a string for example). In both the above examples the Contains() method would be called by using...
Code :
If MyClass.MyList.Contains(aValue) = True Then ...
If you guys were wanting to write a class that would expose a list of other classes to a calling program (e.g. A folder class containing a list of file clases for each file within that folder), how would you achieve this in the accepted Java fashion? If you wouldn't do it anything like what I'm wanting to achieve, please let me know with an example. :o
Thanks for all the help so far.
Re: Exposing lists as class properties / attributes
In your VB code, is MyClass an instance, or is it simply a class? I don't know how VB works, but you might be asking two different things depending on what exactly MyClass is. Is it a class, or an instance of a class?
Java's List interface also has a contains() method.
Re: Exposing lists as class properties / attributes
By the look of it your VB code exposes the collection as a public piece of data. Ie, calling it myPrivateList is a bit misleading as it isn't, in any real sense, private. Precisely because the two forms you give are functionally equivalent one is neither more nor less "OOP" than the other. Anyone who would frown on one but smile on the other is following a rule, not exercising judgement.
The same functionality would be achieved in Java by making it a public member variable. (The difference between the languages is that VB has a bit of sugar allowing you to write functionality like a method, but invoke it like a field. Java programmers would probably dislike the taste of the sugar: preferring to see from the syntax what manner of thing they are dealing with. But it is a matter of taste.)
In Java collections are a reference type, and are passed about as copies of pointers. The languages don't really differ in that respect.
Rather than worrying about whether this is OOP or whatever, it might be more instructive to consider what could go wrong (in either language)
(1) An instance of MyClass can have its collection data changed to a different collection by another class. And it won't know anything about it. The solution here is to provide a setter that responds to the change, or not have a setter in which case the collection really will be private in the sense that no-one outside the class can alter which collection is being used. (In VB the response would go in the Set part, or there could be no Set functionality.)
(2) An instance of MyClass can have the contents of its collection data altered without knowing anything about it. Ie the caller can add or remove things from the collection once it has a reference to it. The solution here is to provide a getter that returns a copy of the collection, so callers can only monkey about with the copy, not the collection that is part of the MyClass instance. Or you could not provide a getter at all but provide specific methods to allow specific behaviour involving the collection.
This last alternative (no getter or setter) even hides the state of the things in the collection from the caller! The caller asks for something to be done: it doesn't ask to be told what the MyClass instance's data is.