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

JavaJava
24
חלקPart 4: OOP מתקדםAdvanced OOP כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

SOLID ותבניות עיצובSOLID & design patterns

חמשת עקרונות SOLID, ו-Strategy, Factory, Builder, Observer ו-Singleton (והבעיות שלו).

The five SOLID principles, plus Strategy, Factory, Builder, Observer and Singleton (and its problems).

או Ctrl+Por Ctrl+P

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

  • SOLID: חמישה עקרונות:SOLID: Five principles:
  • Strategy: משפחת אלגוריתמים מאחורי interface אחד (Comparator, Discount).Strategy: A family of algorithms behind one interface (Comparator, Discount).
  • books.sort(...) לא יודע איך משווים.books.sort(...) doesn't know how to compare.
  • כל אסטרטגיה היא מחלקה קטנה שמממשת Comparator, ומחליפים אותן בלי לגעת ברשימה.Each strategy is a small class implementing Comparator, and you swap them without touching the list.
  • הבנאי של Book הוא private.Book's constructor is private.
  • בונים דרך Book.Builder (מחלקה מקוננת static): כל מתודה מחזירה את ה-Builder עצמו, ולכן אפשר לשרשר.You build through Book.Builder (a static nested class): each method returns the Builder itself, so calls chain.
  • Shelf לא יודע מי מאזין ומה הוא עושה.Shelf doesn't know who listens or what they do.
  • הוא רק עובר על הרשימה וקורא ל-bookAdded.It just goes over the list calling bookAdded.
  • מדף, צעד 24: Strategy ו-BuilderShelf, step 24: Strategy and Builder

מונחיםTerms

  • SOLIDSOLID: חמישה עקרונות: S – אחריות אחת למחלקה; O – פתוח להרחבה, סגור לשינוי; L – תת-מחלקה עובדת בכל מקום שההורה עובד; I – interfaces קטנים וממוקדים; D – תלות ב-interfaces, לא במימושים.Five principles: S – one job per class; O – open to extension, closed to change; L – a subclass works wherever the parent works; I – small, focused interfaces; D – depend on interfaces, not implementations.
  • StrategyStrategy: משפחת אלגוריתמים מאחורי interface אחד (Comparator, Discount). מי שמשתמש מקבל אסטרטגיה ולא יודע איזו.A family of algorithms behind one interface (Comparator, Discount). The user gets a strategy and doesn't know which one.
  • FactoryFactory: מתודה (או מחלקה) שמחליטה איזה עצם ליצור לפי קלט, כך שהשאר לא תלוי במחלקות הקונקרטיות.A method (or class) deciding which object to create from some input, so the rest doesn't depend on the concrete classes.
  • BuilderBuilder: בונים עצם צעד אחרי צעד: new Book.Builder("Dune").year(1965).build(). נוח כשיש הרבה שדות אופציונליים, במקום בנאים עם עשרה פרמטרים.Build an object step by step: new Book.Builder("Dune").year(1965).build(). Handy with many optional fields, instead of constructors with ten parameters.
  • ObserverObserver: עצם שומר רשימת "מאזינים" ומודיע לכולם כשמשהו קורה. המאזינים לא תלויים זה בזה.An object keeps a list of "listeners" and notifies them all when something happens. The listeners don't depend on each other.
  • SingletonSingleton: מחלקה עם מופע יחיד לכל התוכנית (הכי פשוט: enum עם ערך אחד). הבעיה: זה מצב גלובלי, וקשה לבדוק ולהחליף אותו.A class with a single instance for the whole program (simplest: an enum with one value). The problem: it's global state, hard to test and to swap.
  • היפוך תלותdependency inversion: מחלקה מקבלת את מה שהיא צריכה מבחוץ, כ-interface (Reminder(Sender sender)), במקום ליצור אותו בעצמה. כך בבדיקה מעבירים מימוש מזויף.A class receives what it needs from outside, as an interface (Reminder(Sender sender)), instead of creating it itself. In a test you then pass a fake.
  • SOLIDSOLID: חמישה עקרונות: S – אחריות אחת למחלקה; O – פתוח להרחבה, סגור לשינוי; L – תת-מחלקה עובדת בכל מקום שההורה עובד; I – interfaces קטנים וממוקדים; D – תלות ב-interfaces, לא במימושים.Five principles: S – one job per class; O – open to extension, closed to change; L – a subclass works wherever the parent works; I – small, focused interfaces; D – depend on interfaces, not implementations.
  • StrategyStrategy: משפחת אלגוריתמים מאחורי interface אחד (Comparator, Discount). מי שמשתמש מקבל אסטרטגיה ולא יודע איזו.A family of algorithms behind one interface (Comparator, Discount). The user gets a strategy and doesn't know which one.
  • FactoryFactory: מתודה (או מחלקה) שמחליטה איזה עצם ליצור לפי קלט, כך שהשאר לא תלוי במחלקות הקונקרטיות.A method (or class) deciding which object to create from some input, so the rest doesn't depend on the concrete classes.
  • BuilderBuilder: בונים עצם צעד אחרי צעד: new Book.Builder("Dune").year(1965).build(). נוח כשיש הרבה שדות אופציונליים, במקום בנאים עם עשרה פרמטרים.Build an object step by step: new Book.Builder("Dune").year(1965).build(). Handy with many optional fields, instead of constructors with ten parameters.
  • ObserverObserver: עצם שומר רשימת "מאזינים" ומודיע לכולם כשמשהו קורה. המאזינים לא תלויים זה בזה.An object keeps a list of "listeners" and notifies them all when something happens. The listeners don't depend on each other.
  • SingletonSingleton: מחלקה עם מופע יחיד לכל התוכנית (הכי פשוט: enum עם ערך אחד). הבעיה: זה מצב גלובלי, וקשה לבדוק ולהחליף אותו.A class with a single instance for the whole program (simplest: an enum with one value). The problem: it's global state, hard to test and to swap.
  • היפוך תלותdependency inversion: מחלקה מקבלת את מה שהיא צריכה מבחוץ, כ-interface (Reminder(Sender sender)), במקום ליצור אותו בעצמה. כך בבדיקה מעבירים מימוש מזויף.A class receives what it needs from outside, as an interface (Reminder(Sender sender)), instead of creating it itself. In a test you then pass a fake.

Cheatsheet

Strategy: סדר מיון כמחלקהStrategy: a sort order as a class
List<Book> books = new ArrayList<>(List.of(new Book("Emma", 1815), new Book("Dune", 1965), new Book("Ulysses", 1922)));
books.sort(new ByTitle());
System.out.println(books);
books.sort(new ByYear());
System.out.println(books);
Builder: הרבה שדות, קריאה ברורהBuilder: many fields, a clear call
Book b = new Book.Builder("Dune").author("Herbert").year(1965).rating(5).build();
Book c = new Book.Builder("Notes").build();
System.out.println(b);
System.out.println(c);
Observer: להודיע לכל המאזיניםObserver: tell every listener
Shelf shelf = new Shelf();
shelf.subscribe(new Logger());
shelf.subscribe(new Counter());
shelf.add("Dune");
shelf.add("Emma");

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

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

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