נבנה על ידיBuilt by Ariel Yohimovich
JavaJava
פרויקט קטן: המדף כמערכת מחלקותMini project: the shelf as classes
מפגש 18: מתכננים ובונים יחד את Shelf, Book ו-Author: אחריות לכל מחלקה, ממשק נקי ובדיקה ידנית.Session 18: Designing and building Shelf, Book and Author together: one job per class, a clean interface and manual testing.
מתכננים ובונים יחד את Shelf, Book ו-Author: אחריות לכל מחלקה, ממשק נקי ובדיקה ידנית. בסוף המפגש מוסיפים צעד לפרויקט המלווה.Designing and building Shelf, Book and Author together: one job per class, a clean interface and manual testing. At the end we add a step to the companion project.מה נעשה היוםToday's plan
- מונחיםTerms
- גרף של עצמיםA graph of objects
- המדף כמערכת: מי עושה מהThe shelf as a system: who does what
- פרויקט מדףShelf project
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.המונחים של המפגשThis session's terms
- אחריות אחת: לכל מחלקה תפקיד אחד ברור:single responsibility: Each class has one clear job:
- הרכבה (has-a): עצם שמחזיק עצמים אחרים בשדות:composition (has-a): An object holding other objects in its fields:
- ממשק ציבורי: המתודות שמחלקה מציעה לעולם (
add, find, report).public interface: The methods a class offers the world (add, find, report). - האצלה: מתודה שמעבירה את העבודה לעצם אחר:delegation: A method handing the work to another object:
- בדיקה ידנית: קוד קטן שמריץ את המחלקות ובודק תוצאות צפויות, ומדפיס PASS או FAIL.manual test: Small code that runs the classes and checks expected results, printing PASS or FAIL.
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.Before the code, the words. Every term here is also linked in the course glossary.נושאTopic 2
גרף של עצמיםA graph of objects
גרף של עצמיםA graph of objects
- הרכבה: שני ספרים מצביעים על אותו עצם
Author.Composition: two books point to the same Author object. - בזיכרון רואים את הרשימה, שני ספרים, וחץ משותף לסופר.In memory you see the list, two books, and a shared arrow to the author.
- שינוי בסופר (למשל שם מתוקן) היה נראה דרך שני הספרים.A change to the author (say, a corrected name) would show through both books.
הרכבה: שני ספרים מצביעים על אותו עצם Author. בזיכרון רואים את הרשימה, שני ספרים, וחץ משותף לסופר. שינוי בסופר (למשל שם מתוקן) היה נראה דרך שני הספרים.Composition: two books point to the same Author object. In memory you see the list, two books, and a shared arrow to the author. A change to the author (say, a corrected name) would show through both books.נושאTopic 3
המדף כמערכת: מי עושה מהThe shelf as a system: who does what
המדף כמערכת: מי עושה מהThe shelf as a system: who does what
Author ו-Book הם עצמים קטנים ובלתי משתנים.Author and Book are small immutable objects.Shelf מחזיקה רשימה פרטית ומציעה ממשק: add (שמסרבת לכפולים) ו-report.Shelf holds a private list and offers an interface: add (which refuses duplicates) and report.- main רק משתמש בממשק, ולא נוגע ברשימה.main only uses the interface, never touching the list.
Author ו-Book הם עצמים קטנים ובלתי משתנים. Shelf מחזיקה רשימה פרטית ומציעה ממשק: add (שמסרבת לכפולים) ו-report. main רק משתמש בממשק, ולא נוגע ברשימה.Author and Book are small immutable objects. Shelf holds a private list and offers an interface: add (which refuses duplicates) and report. main only uses the interface, never touching the list.נושאTopic 4
פרויקט מדףShelf project
הצעד של היום בפרויקט מדףToday's step in the Shelf project
- מדף, צעד 18: Shelf, Book ו-Author עובדים יחדShelf, step 18: Shelf, Book and Author working together
חברו הכול: Shelf.add (בלי כפולים) ו-Shelf.report() שמדפיסה את מספר הספרים, את הכותרים לפי סופר (לפי סדר ההופעה), ואת הספר עם הדירוג הגבוה. עם Emma, Dune, Persuasion ושוב Emma, הדוח הוא:
Shelf: 3 books / Austen: Emma, Persuasion / Herbert: Dune / Best: Dune (5).Put it all together: Shelf.add (no duplicates) and Shelf.report() printing the number of books, the titles per author (in order of appearance), and the top-rated book. With Emma, Dune, Persuasion and Emma again, the report is:
Shelf: 3 books / Austen: Emma, Persuasion / Herbert: Dune / Best: Dune (5).אחריות אחת: לכל מחלקה תפקיד אחד ברור:single responsibility: Each class has one clear job:
המונחים של המפגשThis session's termsהרכבה (has-a): עצם שמחזיק עצמים אחרים בשדות:composition (has-a): An object holding other objects in its fields:
המונחים של המפגשThis session's termsממשק ציבורי: המתודות שמחלקה מציעה לעולם (add, find, report).public interface: The methods a class offers the world (add, find, report).
המונחים של המפגשThis session's termsהאצלה: מתודה שמעבירה את העבודה לעצם אחר:delegation: A method handing the work to another object:
המונחים של המפגשThis session's termsבדיקה ידנית: קוד קטן שמריץ את המחלקות ובודק תוצאות צפויות, ומדפיס PASS או FAIL.manual test: Small code that runs the classes and checks expected results, printing PASS or FAIL.
המונחים של המפגשThis session's termsהרכבה: שני ספרים מצביעים על אותו עצם Author.Composition: two books point to the same Author object.
גרף של עצמיםA graph of objectsבזיכרון רואים את הרשימה, שני ספרים, וחץ משותף לסופר.In memory you see the list, two books, and a shared arrow to the author.
גרף של עצמיםA graph of objectsשינוי בסופר (למשל שם מתוקן) היה נראה דרך שני הספרים.A change to the author (say, a corrected name) would show through both books.
גרף של עצמיםA graph of objectsAuthor ו-Book הם עצמים קטנים ובלתי משתנים.Author and Book are small immutable objects.
המדף כמערכת: מי עושה מהThe shelf as a system: who does whatShelf מחזיקה רשימה פרטית ומציעה ממשק: add (שמסרבת לכפולים) ו-report.Shelf holds a private list and offers an interface: add (which refuses duplicates) and report.
המדף כמערכת: מי עושה מהThe shelf as a system: who does whatmain רק משתמש בממשק, ולא נוגע ברשימה.main only uses the interface, never touching the list.
המדף כמערכת: מי עושה מהThe shelf as a system: who does whatמדף, צעד 18: Shelf, Book ו-Author עובדים יחדShelf, step 18: Shelf, Book and Author working together
הצעד של היום בפרויקט מדףToday's step in the Shelf projectאיפה הכי נכון לשים את הבדיקה "אין ספר כפול במדף"?Where does the check "no duplicate book on the shelf" belong best?
- ב-main, לפני כל addIn main, before every add
- ב-
Shelf.addIn Shelf.add - ב-
AuthorIn Author - בשום מקוםNowhere
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.ב-Shelf.addIn Shelf.add
נכון: המדף אחראי על התוכן שלו.Right: the shelf is responsible for its contents.למה לא כדאי ש-Shelf תחזיר את הרשימה הפנימית עצמה (return books;)?Why shouldn't Shelf return its inner list itself (return books;)?
- כי זה איטיBecause it's slow
- כי מבחוץ יוכלו להוסיף כפולים ולעקוף את
addBecause outsiders could add duplicates and bypass add - כי Java לא מרשהBecause Java doesn't allow it
- אין בעיהThere's no problem
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.כי מבחוץ יוכלו להוסיף כפולים ולעקוף את addBecause outsiders could add duplicates and bypass add
נכון. מחזירים עותק או List.copyOf.Right. Return a copy or List.copyOf.שני ספרים נוצרו עם אותו משתנה austen. כמה עצמי Author יש?Two books were made with the same variable austen. How many Author objects are there?
- אחדOne
- שנייםTwo
- שלושהThree
- אפסZero
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.אחדOne
נכון: שני הספרים מחזיקים הפניה לאותו עצם.Right: both books hold a reference to the same object.ספר שמחזיק סופרA book that holds an author
- כתבו את
Author (שם, equals ו-hashCode לפי השם, toString שמחזיר את השם) ואת Book (כותר, Author, דירוג, getters, toString Dune (5)).Write Author (a name, equals and hashCode by name, toString returning the name) and Book (a title, an Author, a rating, getters, toString Dune (5)).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.המדף מסרב לכפוליםThe shelf refuses duplicates
- ב-
Shelf (עם private final List<Book> books) כתבו boolean add(Book b) שמחזירה false ולא מוסיפה אם כבר יש ספר עם אותו כותר ואותו סופר, ו-int size().In Shelf (with private final List<Book> books) write boolean add(Book b) that returns false and adds nothing if a book with the same title and author is already there, and int size().
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.חיפוש לפי כותרFind by title
Book find(String title) מחזירה את הספר (בלי תלות באותיות גדולות וקטנות) או null.Book find(String title) returns the book (ignoring upper and lower case) or null.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.כל הספרים של סופרAll books by an author
List<Book> byAuthor(Author a) מחזירה רשימה חדשה עם הספרים של הסופר.List<Book> byAuthor(Author a) returns a new list with the author's books.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.דירוג ממוצעAverage rating
double averageRating() מחזירה את ממוצע הדירוגים, ו-0.0 למדף ריק (בלי חריגה).double averageRating() returns the average rating, and 0.0 for an empty shelf (no exception).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מדף, צעד 18: Shelf, Book ו-Author עובדים יחדShelf, step 18: Shelf, Book and Author working together
- חברו הכול:Put it all together:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.השאלת ספריםLending books
- הוסיפו ל-
Shelf מעקב השאלות:Add lending to Shelf:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.בדיקות ידניותManual tests
- כתבו ב-
Main מתודת עזר static void check(boolean ok, String name) שמדפיסה `PASS:In Main write a helper static void check(boolean ok, String name) printing `PASS:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.השדה author ב-Book הוא מסוג Author, לא String.The author field in Book is of type Author, not String.
ספר שמחזיק סופרA book that holds an authorמשווים את הסופרים עם equals: זה סופר חדש באותו שם.Compare the authors with equals: it's a new author with the same name.
המדף מסרב לכפוליםThe shelf refuses duplicatesequalsIgnoreCaseequalsIgnoreCase
חיפוש לפי כותרFind by titleb.getAuthor().equals(a): כל ספר כאן קיבל עצם Author חדש, ולכן == לא היה עובד.b.getAuthor().equals(a): every book here got a new Author object, so == wouldn't work.
כל הספרים של סופרAll books by an authorמה למדנוWhat we learned
- אחריות אחת: לכל מחלקה תפקיד אחד ברור:single responsibility: Each class has one clear job:
- הרכבה: שני ספרים מצביעים על אותו עצם
Author.Composition: two books point to the same Author object. Author ו-Book הם עצמים קטנים ובלתי משתנים.Author and Book are small immutable objects.- מדף, צעד 18: Shelf, Book ו-Author עובדים יחדShelf, step 18: Shelf, Book and Author working together
כל אחד אומר דבר אחד שלמד היום.Everyone says one thing they learned today.עכשיו לתרגולNow, practice
6 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית6 exercises and 2 challenges, auto-checked
מפנים לעמוד התרגול.Point to the practice page.