Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-09-2008, 09:09 PM   PM User | #1
brookey86
New Coder

 
Join Date: Mar 2008
Posts: 34
Thanks: 6
Thanked 0 Times in 0 Posts
brookey86 is an unknown quantity at this point
Is this good coding practice?

I would like to keep an arraylist of all the Member object references created in my program. Thus, I have placed a public static variable inside the Member class itself that gets added to in the constructor every time a new object is created. I have also placed an instance method called "deleteMember()" that will deleted the object reference if needed.

Is it good Java practice to keep this array inside of the Member class itself, and have additions made in the constructor and deletions through an instance method?

The other option would be to put the arraylist inside the main() function and use the arraylist class functions to add and delete when needed.

Code:
public class Member {
	
	private String fullName;
	private int birthYear;
	private double weight;
	private boolean isAdmin;
	
	public static ArrayList<Member> memberList = new ArrayList<Member>(); 
	
	public Member(String fullName, int birthYear, double weight, boolean isAdmin) {
		//super();
		this.fullName = fullName;
		this.birthYear = birthYear;
		this.weight = weight;
		this.isAdmin = isAdmin;
		
		//add the newest object to the memberList arraylist
		memberList.add(this);
	}

	public void deleteMember()
	{
		//delete current object from the memberList arraylist
		memberList.remove(this);
	}
}
brookey86 is offline   Reply With Quote
Old 09-10-2008, 04:51 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Its not wrong practice if thats what you mean. You're only other real option would be to create a collection yourself which has significant overhead compared to using what you do have.
I'd stick with an arraylist within the class you've created. It makes sense for it to be static as well since its not actually a member but a collection of members. What I would change is the usage of anything altering what you have. First, make it protected or private, then use public static mutators for it: public static void addMember(Member member);public static void deleteMember().... Although you certainly don't require it to be, it just seems wrong having something like:
Code:
Member mem1 = new Member(...);
mem1.addMember(new Member(...));
// Something like this would be better:
Member mem1 = new Member(...);
Member.addMember(mem1);
Member.addMember(new Member(...));
Hope that helps you out!
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 09-10-2008, 11:46 AM   PM User | #3
ess
Regular Coder

 
Join Date: Oct 2006
Location: United Kingdom
Posts: 865
Thanks: 7
Thanked 29 Times in 28 Posts
ess will become famous soon enough
Agree with Fou-Lu comment....but I would abstract the member class where it does not hold collections of members....and define another class which would handle how members are stored etc.

Personally, I would go further and implement the Singleton design pattern on the MemberCollection class so that there is only instance that exists...holding all members etc. and ultimately, improving the overall performance of your application

The following are examples of how I would implement this:
Member.java file
PHP Code:
public class Member {
    
    private 
String fullName;
    private 
int birthYear;
    private 
double weight;
    private 
boolean isAdmin;
    
    
//public static ArrayList<Member> memberList = new ArrayList<Member>(); 
    
    
public Member(String fullNameint birthYeardouble weightboolean isAdmin) {
        
//super();
        
this.fullName fullName;
        
this.birthYear birthYear;
        
this.weight weight;
        
this.isAdmin isAdmin;
        
        
//add the newest object to the memberList arraylist
        //memberList.add(this);
    
}
    
    
/*
    public void deleteMember()
    {
        //delete current object from the memberList arraylist
        memberList.remove(this);
    }
    */
    
@Override
    
public String toString() {
        return 
"Name: " this.fullName "\tD.O.B: " this.birthYear "\tIs Admin: " this.isAdmin;
    } 
//-- ends toString

MemberCollection.java
PHP Code:
import java.util.*;

public class 
MemberCollection {
    
    private 
ArrayList<MembermemberList = new ArrayList<Member>(); 
    private static 
MemberCollection instance null;
    
    private 
MemberCollection() {
        
    } 
//-- ends constructor
    
    // you can only have one instance of this class
    // since we've implemented the Singleton design pattern
    
public static MemberCollection getInstance() {
        if(
MemberCollection.instance==null) {
            
MemberCollection.instance = new MemberCollection();
        }
        return 
MemberCollection.instance;
    } 
//-- ends MemberCollection
    
    
public void addMember(final Member m) {
        
this.memberList.add(m);
    } 
//-- ends addMember
    
    
public void delMember(final Member m) {
        
this.memberList.remove(m);
    } 
//-- ends delMember
    
    
@Override
    
public String toString() {
        
StringBuffer buf = new StringBuffer();
        for(
int i=0this.memberList.size();i++) {
            
buf.append(this.memberList.get(i).toString()+"\n");
        } 
//-- ends for loop
        
return buf.toString();
    } 
//-- ends toString
    
//-- ends class definition 
and finally MemberRunner.java to test the application
PHP Code:
public class MemberRunner {
    public static 
void main(String [] args) {
        
Member m1 = new Member("John Smith"197712.25false);
        
Member m2 = new Member("Marco Smith"197010.25false);
        
// get instance an instance of the MemberCollection class 
        
MemberCollection coll MemberCollection.getInstance();
        
coll.addMember(m1);
        
coll.addMember(m2);
        
System.out.println("+++ Collection of Members +++");
        
System.out.println(coll);
        
// to prove that you can only have one instance
        // and therefore improving performance
        // let's create another pointer to the already 
        // existing instance of the MemberCollection class
        
MemberCollection second MemberCollection.getInstance();
        
// print it out and examin the results
        
System.out.println("--- Declaring another pointer ---");
        
System.out.println(second);
    } 
//-- ends class method main
//-- ends class definition 
If you wish to read about the Singleton design pattern, please visit this page http://www.javabeginner.com/singleton.htm

Cheers
~E
ess is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:01 AM.


Advertisement
Log in to turn off these ads.