נבנה על ידיBuilt by Ariel Yohimovich

חזרה למפגשBack
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.

מה נעשה היוםToday's plan

  • מונחיםTerms
  • גרף של עצמיםA graph of objects
  • המדף כמערכת: מי עושה מהThe shelf as a system: who does what
  • פרויקט מדףShelf project
נושאTopic 1

מונחיםTerms

המונחים של המפגש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.
נושא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.
נושא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.
נושאTopic 4

פרויקט מדףShelf project

הצעד של היום בפרויקט מדףToday's step in the Shelf project

  • מדף, צעד 18: Shelf, Book ו-Author עובדים יחדShelf, step 18: Shelf, Book and Author working together

אחריות אחת: לכל מחלקה תפקיד אחד ברור: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.

הרכבה: שני ספרים מצביעים על אותו עצם 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 ו-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.

מדף, צעד 18: Shelf, Book ו-Author עובדים יחדShelf, step 18: Shelf, Book and Author working together

איפה הכי נכון לשים את הבדיקה "אין ספר כפול במדף"?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

ב-Shelf.addIn Shelf.add

למה לא כדאי ש-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

כי מבחוץ יוכלו להוסיף כפולים ולעקוף את addBecause outsiders could add duplicates and bypass add

שני ספרים נוצרו עם אותו משתנה austen. כמה עצמי Author יש?Two books were made with the same variable austen. How many Author objects are there?

  • אחדOne
  • שנייםTwo
  • שלושהThree
  • אפסZero

אחדOne

ספר שמחזיק סופר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)).

המדף מסרב לכפולים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().

חיפוש לפי כותרFind by title

  • Book find(String title) מחזירה את הספר (בלי תלות באותיות גדולות וקטנות) או null.Book find(String title) returns the book (ignoring upper and lower case) or null.

כל הספרים של סופרAll books by an author

  • List<Book> byAuthor(Author a) מחזירה רשימה חדשה עם הספרים של הסופר.List<Book> byAuthor(Author a) returns a new list with the author's books.

דירוג ממוצעAverage rating

  • double averageRating() מחזירה את ממוצע הדירוגים, ו-0.0 למדף ריק (בלי חריגה).double averageRating() returns the average rating, and 0.0 for an empty shelf (no exception).

מדף, צעד 18: Shelf, Book ו-Author עובדים יחדShelf, step 18: Shelf, Book and Author working together

  • חברו הכול:Put it all together:

השאלת ספריםLending books

  • הוסיפו ל-Shelf מעקב השאלות:Add lending to Shelf:

בדיקות ידניות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:

השדה author ב-Book הוא מסוג Author, לא String.The author field in Book is of type Author, not String.

משווים את הסופרים עם equals: זה סופר חדש באותו שם.Compare the authors with equals: it's a new author with the same name.

equalsIgnoreCaseequalsIgnoreCase

b.getAuthor().equals(a): כל ספר כאן קיבל עצם Author חדש, ולכן == לא היה עובד.b.getAuthor().equals(a): every book here got a new Author object, so == wouldn't work.

מה למדנו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

עכשיו לתרגולNow, practice

6 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית6 exercises and 2 challenges, auto-checked
הערות מרצהSpeaker notes

נבנה על ידיBuilt by Ariel Yohimovich