I am new to object oriented design. I am looking forward to some tips on how to model the below mentioned requirement using objects.
Requirement: A program has many Students. A program logs in to the application. First page displays a table of all the Students belonging to the program (Table Columns - Id, First name, last name, age, sex, etc. ). The Id is a link. By clicking the Id, the individual student page is displayed. In this page, an individual student related activities can be done - for eg. edit address, change name, add comments etc.
My Solution: Two Classes
Program - Will model a single program, perform all activities related to it and encapsulate all program related db tables.
Student - Will model a single student and perform all activities related to it and encapsulate all student specific db tables.
So far, so good.
In order to fetch details of all students related to a program, from Program object I need to call a method called 'fetch_student_details'.
The Question is where should this method be written? Should it be a Student Class method or Program Class method.
If I write this method in program class, how will the Program class handle it?
(a) First identify the list of student_ids that belong to it and for each id, instantiate a Student Class and get the specific student related information from it. DB: Each instantiated student object will run a query to fetch its information. So 100 students, 100 queries.
OR
(b) Fetch the list of student ids related to the program and fetch the student information directly for all the students. Db: Single query to fetch all the needed information.
In this case, if you can, go for solution (b). Students and Programs, in fact, are two differet entities that can exist independently, and you don't break encapsulation.
With solution (a) you need to pollute Program's interface by adding a method for each possible information that you want to get from the Students.
As final suggestion, if you want to learn OO Design, start by reading the book "Design patterns - Elements of Reusable Object-Oriented Software" by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides (AKA "The gang of four").
Related
Hello I have the following situation:
I have a product instance table that is nothing more than a product already assigned to a department and employee.
Well, but I have the following business logic: the product can be an equipment, or a component, that is, a product can have children.
thinking how a computer case is a device that has patrimony_code, and has child products (components) such as:
motherboard,
memoirs,
vga
etc etc
and all of these components are connected to the equipment.
I arrived at this with less table:
But I encountered some problems:
my components would not need departament_id, as they do not belong to the department but to the equipment that there belongs to the department.
So I came to this modeling, but I don't know if it is a good thing to do this relationship, could someone help me if there is a more clean / solid solution for this?
if I have understood your description correctly, only a piece of equipment can be assigned directly to a department, not the individual components that make up the piece of equipment?
Assuming this is the case then I suggest you split this problem into 2 separate tasks as they are, in reality, unrelated and trying to treat them as a single model is causing confusion:
Model the relationship between an instance of a piece of equipment and a department
Model the hierarchy of components (and their instances) that make up a piece of equipment
Once you have these two models you can then relate them if you need to but logically they are separate and you can change one without affecting the other
Hope this helps?
UPDATE 1
(in response to your questions)
You construct 1 sub-model that covers your Equipment entity and the Component entity (plus any other relevant entities) that describe how your products are modelled.
Your construct a second sub-model that describes how equipment is assigned to a department.
The only entity that would be (necessarily) common to the 2 sub-models is the Equipment entity; though, obviously, you can display both sub-models on the same diagram if that is required - even though they are logically separate (apart from the one common entity: Equipment)
I have different tables School, Student, Course on my SQL database. I want to design a search algorithm which should make user to search by any of those table names (School, Student, Course) and bring only their name list very fast. But that search keyword can be anything - either school name, student name or course name.
School is model and has: name, location.
Student is model and has student number, name, schoolid, courseid.
Course is model and has course name, courseid.
School name, student name or course name can be search for in all data. And result should bring only that model.
For example if I search for (John = student name), it should bring me all students named John only with student model values (object). Or course name (mathematics) also can be another example and it should only bring its properties as object too.
I came up with 2 different approaches:
I can create nested object array like School -> Student -> Course (includes each other on each object) and name will be searched on these nested arrays. I think this is not good because in anyway all those big and large arrays will be visited (Doesn't matter we checked for school name or not).
Second one I think makes more sense. Instead of making them nested I can search that keyword on each table one by one. First school then student then course (If its school then will be much easier than nested one even).
If you have a better approach please share it with me.
Well, it's still unclear, where you want to "design your search algorithm" and get your result "objects"! Since it's a question asked with SQL tag, I'd say that you should simply do 3 simple select queries on all tables for each search term. Any joins would mix the schemas and you'll have to distinguish results afterwards.
You're probably talking about some oop language with a database connectivity interface which does the job of queries and converting results into objects. In this case you would simply combine the result of each separate query with the features of language to get your objects.
I teach. I would like to solve the following problem:
Collect student tests and have an OCR read input from the test and detect which exam belongs to which student.
I've been using pytesseract pretty successfully to solve this problem. I give each student a paper with their student ID and when they turn in their test they put this paper on top. Then pytesseract is "pretty" good at reading the Student ID within an acceptable error that usually allows me to uniquely identify the student from a list of all student IDs.
I'm curious if there are better solutions out there for this? One hassle with my current solution is that I have to pass out this student ID page individually to each student. When there are many students this takes a while and bothers students while they're taking their test.
Is there something the student can write by hand (e.g., fill in bubbles, etc.) on the exam that an OCR can read to uniquely identify the student? Handwritten names don't seem to work, there's too much variation, and I need it to be very accurate.
I'm learning about the principles of OOP and was doing this exercise question.
We are given a school. The school has classes of students. Each class has
a set of teachers. Each teacher teaches a set of courses. The students
have a name and unique number in the class. Classes have a unique text
identifier. Teachers have names. Courses have a name, count of classes
and count of exercises. The teachers as well as the students are people.
Your task is to model the classes (in terms of OOP) along with their
attributes and operations define the class hierarchy and create a class
diagram with Visual Studio.
I am not sure how to ensure the parts in bold. What I was doing was just normally making the adjectives properties. I am not sure how to ensure that other objects are not able to have the same unique number. I was thinking of using the Dictionary data structure which doesn't allow duplicates. I would use this unique number as the key and the object to be its value. Is my line of thought correct here?
you can have for example static private array property (same for every instance of a class) in class, and on the constructor You can check if this value had been used
I have a simple entity data model where I have two entities and a 1-* relation between them. For simplification purposes let's say Person and Book. (i.e. people have 0 or more books and books must have an owner)
The system works fine and it has data in it.
Now I need to create other entities inherited from Person (i.e. Student, Teacher)
When I create them and update my software accordingly, everything works fine with this new model.
Now I need to migrate the old data (data in Person which is linked to books)
In my data I know which person's are student and which are Teacher and I have additional information on the Students and Teachers that I'll place in the new model.
The question is how do I migrate the data. I cannot add Students and Teachers before I delete the Person instance because that would create duplicate Id in Person. I cannot delete the Person as it creates an error. ("DELETE statement conflicted with REFERENCE constraint")
How do I acheive this data migration.
Thanks
If you really have mapped inheritance you should not delete Person - the Person is either Student or Teacher so you should just add correct columns (in case of TPH) or related tables (in case of TPT) and use existing data without their modification.
This migration must be done in SQL.