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

JavaJava
18
חלקPart 3: OOP: היסודותOOP: the basics כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

פרויקט קטן: המדף כמערכת מחלקותMini project: the shelf as classes

מתכננים ובונים יחד את Shelf, Book ו-Author: אחריות לכל מחלקה, ממשק נקי ובדיקה ידנית.

Designing and building Shelf, Book and Author together: one job per class, a clean interface and manual testing.

או Ctrl+Por Ctrl+P

הנקודות החשובותKey points

  • אחריות אחת: לכל מחלקה תפקיד אחד ברור:single responsibility: Each class has one clear job:
  • הרכבה (has-a): עצם שמחזיק עצמים אחרים בשדות:composition (has-a): An object holding other objects in its fields:
  • הרכבה: שני ספרים מצביעים על אותו עצם 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.
  • 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.
  • מדף, צעד 18: Shelf, Book ו-Author עובדים יחדShelf, step 18: Shelf, Book and Author working together

מונחיםTerms

  • אחריות אחתsingle responsibility: לכל מחלקה תפקיד אחד ברור: Author יודעת על סופר, Book על ספר, ו-Shelf מנהלת אוסף. כשמשהו משתנה, ברור איפה לתקן.Each class has one clear job: Author knows about an author, Book about a book, and Shelf manages a collection. When something changes, it's clear where to fix it.
  • הרכבה (has-a)composition (has-a): עצם שמחזיק עצמים אחרים בשדות: Book מחזיק Author. כמה ספרים יכולים להצביע על אותו סופר.An object holding other objects in its fields: Book holds an Author. Several books can point to the same author.
  • ממשק ציבוריpublic interface: המתודות שמחלקה מציעה לעולם (add, find, report). כל השאר פרטי, ואפשר לשנות אותו בלי לשבור את מי שמשתמש במחלקה.The methods a class offers the world (add, find, report). Everything else is private, and can change without breaking whoever uses the class.
  • האצלהdelegation: מתודה שמעבירה את העבודה לעצם אחר: Shelf.size() מחזירה books.size().A method handing the work to another object: Shelf.size() returns books.size().
  • בדיקה ידניתmanual test: קוד קטן שמריץ את המחלקות ובודק תוצאות צפויות, ומדפיס PASS או FAIL. במפגש 29 נעבור ל-JUnit, שעושה את זה בצורה מסודרת.Small code that runs the classes and checks expected results, printing PASS or FAIL. In session 29 we move to JUnit, which does this properly.
  • אחריות אחתsingle responsibility: לכל מחלקה תפקיד אחד ברור: Author יודעת על סופר, Book על ספר, ו-Shelf מנהלת אוסף. כשמשהו משתנה, ברור איפה לתקן.Each class has one clear job: Author knows about an author, Book about a book, and Shelf manages a collection. When something changes, it's clear where to fix it.
  • הרכבה (has-a)composition (has-a): עצם שמחזיק עצמים אחרים בשדות: Book מחזיק Author. כמה ספרים יכולים להצביע על אותו סופר.An object holding other objects in its fields: Book holds an Author. Several books can point to the same author.
  • ממשק ציבוריpublic interface: המתודות שמחלקה מציעה לעולם (add, find, report). כל השאר פרטי, ואפשר לשנות אותו בלי לשבור את מי שמשתמש במחלקה.The methods a class offers the world (add, find, report). Everything else is private, and can change without breaking whoever uses the class.
  • האצלהdelegation: מתודה שמעבירה את העבודה לעצם אחר: Shelf.size() מחזירה books.size().A method handing the work to another object: Shelf.size() returns books.size().
  • בדיקה ידניתmanual test: קוד קטן שמריץ את המחלקות ובודק תוצאות צפויות, ומדפיס PASS או FAIL. במפגש 29 נעבור ל-JUnit, שעושה את זה בצורה מסודרת.Small code that runs the classes and checks expected results, printing PASS or FAIL. In session 29 we move to JUnit, which does this properly.

Cheatsheet

גרף של עצמיםA graph of objects
Author austen = new Author("Austen");
List<Book> shelf = new ArrayList<>();
shelf.add(new Book("Emma", austen));
shelf.add(new Book("Persuasion", austen));
System.out.println(shelf.size() + " books, same author: " + (shelf.get(0).author == shelf.get(1).author));
המדף כמערכת: מי עושה מהThe shelf as a system: who does what
Shelf s = new Shelf();
Author austen = new Author("Austen");
s.add(new Book("Emma", austen, 4));
s.add(new Book("Dune", new Author("Herbert"), 5));
s.add(new Book("Persuasion", austen, 3));
s.add(new Book("Emma", austen, 4));
s.report();

שגיאות שפגשנוErrors we met

שגיאהErrorמה זה אומרWhat it meansמה עושיםWhat to do

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