חלק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.
הנקודות החשובות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 sameAuthorobject. - בזיכרון רואים את הרשימה, שני ספרים, וחץ משותף לסופר.In memory you see the list, two books, and a shared arrow to the author.
Authorו-Bookהם עצמים קטנים ובלתי משתנים.AuthorandBookare small immutable objects.Shelfמחזיקה רשימה פרטית ומציעה ממשק:add(שמסרבת לכפולים) ו-report.Shelfholds a private list and offers an interface:add(which refuses duplicates) andreport.- מדף, צעד 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:Authorknows about an author,Bookabout a book, andShelfmanages 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:Bookholds anAuthor. 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()returnsbooks.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:Authorknows about an author,Bookabout a book, andShelfmanages 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:Bookholds anAuthor. 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()returnsbooks.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 |
|---|