954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to seperate tasks of one class

Hi
I have a class which has alot of tasks related to multiple classes.
the tasks are add, modify ,delete, write on a file and read array from a file.all theses methods operates on objects of other classes as a member variables.
In other words, I have to perform a group of functions on multiple objects ,actually the same functions must be performed on all objects (which are from different classes).
so how can I seperate the tasks?
thank you in advance.

creative_m
Light Poster
28 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

what exactly do you mean?
your explanation makes no sense, it's like saying: I'm writing a method add() or multiply() which has to be able to take a String as well as a Double, and has to be able to be applied to as well a String instance as a Double instance.

do you mean 'this must be able to take parameters of all kinds, as long as they're numerical'?
if so, I think generics is what you mean, but maybe you can try to explain it a bit more clear before I'm saying that's definitely the way to go.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

Or would overloading methods do it.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 
class Controller {
	static ArrayList<Semester> semesterList = new ArrayList<Semester> ();
	static ArrayList<Student>  studentList = new ArrayList<Student> ();
	static ArrayList<AcademicStaff> academicStaffList = new ArrayList<AcademicStaff> ();
	static ArrayList<Enrollment> enrolllementList = new ArrayList<Enrollment> ();
	static ArrayList<Course>  courseList = new ArrayList<Course> ();
	static ArrayList<Object> objects;
//then my methods
//----------------- AcademicStaff methods ------------------------

add()
delete()
modify()
//------------------ semester methods ---------

delete(semester){}
//------------------ Student methods ---------

modify(Student s){}
delete (student){}

//------------------ Enrollment methods ---------

modify (Enrollment e){}


//------------------ Course methods ---------

delete (course) {}

//------------------ statistics methods ---------
 static double CourseStatistics (Course course){}

static HashMap<Student, Double> StudentStatistics (Semester semester){}

////-------------------read and write methods----------------------------------
writeStudent(){} //write arrayList of students in a file
writeCourse(){}
writeObject(){}
readObject(){}
//...
///
///----------------------sort methods----------------------------------------
sortSemesters()
sortStudents(){}

//etc

}

I should write alot of methods in this class (mentioned above) ,so I want to "splite" this class into pieces to enhance this code so it becomes reusable.

creative_m
Light Poster
28 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

Why are those arraylists in the same class? How are they related? Are they independent and could be move to their own class with their own editing methods.
Make an abstract class or interface that they extend or implement.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

besides, why do you think methods that in the same class are not reusable?

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

actually, the arraylists can be moved to another class but what about the methods

creative_m
Light Poster
28 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

just try and rationalize the why. what do you want to achieve by moving them to other classes? every method in each class is reusable, just the scope within can be adjusted by using access modifiers

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 
besides, why do you think methods that in the same class are not reusable?


my class (now) is not reusable because of those non associated method,but the methods themselves are reusable

creative_m
Light Poster
28 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

Maybe those method definitions belong in the class they apply to, ie in Student there is a delete(Student s) method, in Course there's delete(Course c) etc etc. (These should probably be just named delete() defined as instance methods.)
In a typical controller class you will call those methods, but not define them.
If there's significant duplication between the methods in all those classes then consider inheriting them from a common superclass, or using a low-level utility class or classes that they all call on for common functions (eg database updates)

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
(These should probably be just named delete() defined as instance methods.)


and the arrayLists should be public and known to every instance of the classes?

creative_m
Light Poster
28 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: