I'm learning about the ECB pattern. I've understood the concept of this pattern but i'm not sure about its implementation. I'm going to write a simple example:
I supposed to need a software which manages the customers of a shop. The customers are stored on a generic database.
Accoding to the ECB pattern i need to have the following classes
1)Customer which represents the entity, with its attributes (name,surname,birthDate etc..)
2)CustomerWindow which represents the boundary, with some labels, textfields, buttons and a table to show customers
3)CustomerController which represents the logic with some methods (insert,delete etc...)
I should add also a CustomerDAO (implementing an interface, but my question is not about DAO) which manages the access to the database.
I would like to know the way this classes interact each other.
For example, supposing the insertion of a new customer, i suppose they interact like this:
1)The CustomerWindow "collects" the data written inside the textFields (name,surname ecc) and calls the method insert(String ....) of the CustomerController.
2)The CustomerController check if all data are ok (for example empty fields or format error). If they are ok, create a new Customer with that data and calls the method insert(Customer c) of the CustomerDAO.
3)The CustomerDao provide to insert the customer into the database
Obviously some of this operations could throw some exceptions but i think it's not important for this example, supposing inserted data are all valid.
Is this the way the ECB pattern works? If not, how it works?
I have a last question:
Some of this classes should be static or i need to declare an instance of each of them? For example i think the CustomerController and the Customer DAO can be static. The CustomeWindows calls the CustomerController.insert(...) method which eventually calls the CustomerDAO.insert(...) method (so i don't need to create a new CustomerController() or a new CustomerDAO(). Is it right?
I hope my english is pretty understandable. Please tell me if i've not been clear about something. Thank you all ;)
P.s. if you prefer i can write a code example
I am working on a Java application and came across a general implementation/meta question and wanted to reach out for suggestions.
I am looking to associate a Java object with a quantity. The java object is complex. In my case, it is a serializable object that represents JSON data from a 3rd party API. I am looking to associate a quantity with this complex Java object.
As this may be something that is easier to understand with an example, here is one. Say I have a Car class that is used to represent a car. It contains all the details of what make a car a car and is a general form that can be used to communicate over an API. Say I am making an inventory app for a dealership. The dealership would want to know how many of each Car they have. Hence the need for the association.
Ideas
There are some ways I can think of the do this.
Class it out
One idea would be to create classes that capture this association. One could have an InventoryEntry class that contains a Car and a quantity. Your dealerships inventory would then consist of a List of InventoryEntry objects.
Arrays
One can also implement this association via an Array mechanism. This can be done by creating an ArrayList<Car> for the cars and an ArrayList<Integer> for the quantity. The index for each list would be used to associate the two.
Would you recommend one of these method or some other implementation?
Using ArrayList makes it a little bit easier to start out, but if you are going to maintaining and extending this application, creating a custom class will save you a lot of time in the long run. The reason is that it would be difficult to change the ArrayList class. Yes, you could subclass the arraylist class, and override the methods that you need to, but that is making more work for yourself.
For the basic scenario that you gave, creating a CarInventory class could be extended for new behavior. The new class could just wrap a basic ArrayList or HashMap implementation, but being able to extend your application for long term maintainability is important.
It is not easy to explain my issue.
JPA creates some complex objects for calculations, which are stored in a database.
We decided to set the results in a working copy of this objects.
This means for each object model we created a seperated working copy model file with the same fields but some other LocalDates values and new result fields.
When the calculation was starting the working copies are instantiated.
This approach is not the best i think.
I think of the prototype pattern to clone the object.
There i come to the problem how to add the new fields. How?
Instantion costs and ist creates lots of additionals model class files.
I only think of put the result field in the calculation data models as transient fields.
Maybe inner class or local class?
I also tried to use an interface as data bucket.
But thats not the realy purpose of interfaces and also it works only with many curious trick.
For Unit Tests and user input i think it is the best to use the builder pattern and then tell JPA to store the parent object, or not?
Sorry but my answer was to long for a comment :(
There is big complex object relationship with Lists and Sets One To Many etc. relationship. When i set the result i a new class i cant determine the right object e.g. in a list. So we bild the same structurefor these result and seperated these classes in a package. Maybe it is possible to dont build the structure a second time with also references to the "basic classes". It should be sufficient to reference to each basic class a result class. It would only a little bit more navigation to get values from deeper classes. For a similiar use case there must be a best practise, or? Interfaces or sth. I very dislike the many classes for the result. Is it not possible to clone and add classmember to it for the result or to logical group easier or something like this?
It could be a solution for somebody:
http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fjdt%2Fcore%2FIWorkingCopy.html
Here you will work with the Eclipse API and create IWorkingCopies.
For the described task toooo much.
I have Many-to-Many associated entities information with me.
I would like to show the user the list of "students" and if user chooses an student, show his teachers.
Conversely, user may opt to see list of teachers and he/she can select a teacher to see all the students that teacher is teaching.
I am looking to have a java collection class (java built in or 3rd party) to represent such data so that I can query for teachers based on student or vice versa.
Bidi map comes quite close but it enforces 1:1 relationship. I have many to many relationship.
Any clues?
i think you couldn't do that with a map. the most simple way would be, to create a Student class and a Teacher class. both of it could have a method like addTeacher(Teacher teacher) / addStudent(Student student). So each Student objects knows it Teachers and each Teacher object knows it Student.
I am not sure if this is what you are looking for, but you can have a look at Guava BiMap
Does this not suffice?
Map<Student, Set<Teacher>> studentsToTeachers;
Map<Teacher, Set<Student>> teachersToStudents;
It's not a single collection, but it would solve your problem, provided your implementation was correct.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
This the description of the program brief:
Produce a software that keeps track of courses taught by lecturers at College. For each course, a course code and title needs to be recorded, as well as a list of lecturers taking that course needs to be recorded. The system should allow courses and lecturers to be added and removed from the list and information such as lecturers taking a course and courses registered to a particular lecturer to be displayed.
So far I have two classes Lecturer and Course and they two attributes such as name id, and code and title respectively. I have then created two more classes to hold the data for both of those objects and I have used a Map, so I have mapped id and name for Lecturer and code and title for course, they are both in seperate classes called LecturerList and CourseList.
But now I can't allocate a course to a lecturer. have been stuck on this for a week now. Any ideas? Have I approached this the wrong way?
How about using a map, like:
Map<Course, Lecturer>
or
Map<Lecturer, List<Course>>
I disagree that the problem needs a database. It's the object-oriented part that you're having problems with. Get that right and a database will follow if you need persistence.
I'd start with Course and Lecturer, as you did. Course would have the required code and title attributes, plus a List of Lecturers teaching it. Likewise, Lecturer would have a name attribute and a List of Courses being taught. It sounds like a bi-directional 1:m relationship to me. The language of your problem statement is a bit confusing, but it sounds like a Course can be taught by several Lecturers (perhaps because there are several sections of a given Course), and a Lecturer can teach more than one Course.
If this sounds accurate, I don't think you need a CourseList or LecturerList class.
The problem description is screaming "Database!". This problem is about tracking related sets of information, the very thing database software was designed for. My solution to this problem would be to use mySQL, and the Java database connector for mysql.
Try to make a functional schema (I almost wrote UML) of what you are trying to achieve. Ideally, use a piece of paper, a pencil, and draw boxes for each functional entity (Lecturer, Course, etc), and draw a labeled, oriented line for each type of relationship between these entities. Use colors, if need be.
If you think of the lines you have drawn as pieces of rope, on what such relationships do you need to "pull" to get the lists you are asked for?
Then and only then, try to design the class structure...
I would suggest storing a list of courses within the Lecturer and a list of Lecturers in the Course. Then create a service that models the "system", something like:
Course {
String code;
String title;
List<Lecturer> lecturers = new ArrayList<Lecturer>();
... accessors & other business rules ...
}
Lecturer {
int id;
String name;
List<Course> courses = new ArrayList<Course>();
... accessors & other business rules ...
}
LectureServices {
List<Course> coursesAvailable = new ArrayList<Course>();
addCouse(Course course) {
coursesAvailable.add(course);
}
addLecturerToCourse(Lecturer l, Course c) {
... lookup course ...
c.addLecturer(l);
l.addCourse(c);
}
... etc ...
}
Start with your basic objects and what properties they have
student -> id, name, list of courses
lecturer -> name, list of courses
course -> code, name, list of students, list of lecturers
From your requirements, the course appears to be the major component of interest, so let that be the place to start.
Since a student takes a course (not the other way around), the key function should be an operation on the course [course.addstudent]. But, you also need to be able to list all the courses a specific student is taking, so you need to keep a list by student somewhere, and the student object makes sense [student.addCourse]. Since adding the student to a course and creating the list of courses a student is taking, is a single logical "action", it makes sense to have a single function that handles the entire thing- and I would go back to the course, since it's what your really maintaining, as the place to do that.
The psuedocode isn't be perfect, but you can get the idea:
course.addStudent(student) {
course.listOfStudents.add(student)
student.addCourse(this)
}
student.addCourse(course) { student.listOfCourses.add(course) }
Deleting a student from a course, and adding and deleting the lecturer is similar.
You've used maps to map lecturer and course IDs to attributes? That doesn't sound very object-oriented, and in Java you'll have trouble if you try to not be OO!
This sounds to me like the sort of application where you will want to maintain bidirectional links between Lecturer and Course: i.e. each course has one lecturer --- is that always true? --- so Course has get/setLecturer methods, and each Lecturer has a container of Courses with add/set/remove/getCourse(s) methods. You shouldn't need to maintain external tables to store the Lecturer & Course details, or their mappings: in Java the objects themselves can be stored directly as everything is really a reference.
If this is to be used in anything approaching a production environment then you will want a database and I can recommend the Hibernate object-relational mapping system (especially using annotation-driven persistence) for automating much of the database retrieval, storage, cascading deletion, query pooling strategies, etc. But if this is just a pedagogical exercise, then that will be far too heavy. Oh, and if you're using this in production, maybe having a look at some existing course management / virtual learning environment software: see the bottom of http://en.wikipedia.org/wiki/Virtual_learning_environment for some references.