חמשת עקרונות SOLID, ו-Strategy, Factory, Builder, Observer ו-Singleton (והבעיות שלו).
The five SOLID principles, plus Strategy, Factory, Builder, Observer and Singleton (and its problems).
כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-IntelliJ ולסמן פתרתי. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in IntelliJ and mark I solved it. Progress is saved.
חימום: 3 שאלותWarm-up: 3 questions
תרגיליםExercises
תרגילExercise 1
Strategy: הנחותStrategy: discounts
interface Discount עם apply(double) ו-name(). שלושה מימושים: בלי הנחה, אחוזים, וסכום קבוע (לא מתחת ל-0). Checkout מקבל Discount בבנאי. עבור מחיר 100 הדפיסו none: 100.0, 10% off: 90.0, 15.0 off: 85.0.
interface Discount with apply(double) and name(). Three implementations: no discount, a percentage, and a fixed amount (never below 0). Checkout takes a Discount in its constructor. For a price of 100 print none: 100.0, 10% off: 90.0, 15.0 off: 85.0.
איך הפלט אמור להיראות?What should the output look like?
none: 100.0
10% off: 90.0
15.0 off: 85.0
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:Checkout לא יודע איזו הנחה קיבל.Checkout doesn't know which discount it got.
ShapeFactory.create(String kind, double size) מחזירה Circle או Square (records), וזורקת unknown shape: star (use circle or square) על סוג לא מוכר. הדפיסו 3.14, 9.0, ואת ההודעה.
ShapeFactory.create(String kind, double size) returns a Circle or a Square (records), and throws unknown shape: star (use circle or square) for an unknown kind. Print 3.14, 9.0, and the message.
איך הפלט אמור להיראות?What should the output look like?
3.14
9.0
unknown shape: star (use circle or square)
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:switch על מחרוזת, עם default -> throw ....A switch on a string, with default -> throw ....
Pizza with a private constructor and Pizza.Builder(String size). Optional: cheese(boolean) (default false) and topping(String) (default plain). Print large +cheese olives and small plain.
איך הפלט אמור להיראות?What should the output look like?
large +cheese olives
small plain
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
Thermometer מחזיק רשימת TempListener ומודיע לכולם ב-set. Display מדפיס תמיד (Display: 25C), Alarm(30) רק מעל 30 (ALARM: 32 > 30). קבעו 25 ואז 32.
Thermometer keeps a list of TempListeners and notifies them all on set. Display always prints (Display: 25C), Alarm(30) only above 30 (ALARM: 32 > 30). Set 25 then 32.
איך הפלט אמור להיראות?What should the output look like?
Display: 25C
Display: 32C
ALARM: 32 > 30
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:סדר ההודעות כסדר ההרשמה.Notifications go in subscription order.
היפוך תלות ומזויף לבדיקהDependency inversion and a test fake
Reminder מקבל Sender בבנאי ושולח please return <title>. FakeSender רק שומר למי נשלח, ו-ConsoleSender מדפיס. עם המזויף שלחו ל-Noa ול-Avi והדפיסו [Noa, Avi], ואז עם ConsoleSender ל-Dana: to Dana: please return Ulysses.
Reminder takes a Sender in its constructor and sends please return <title>. FakeSender only records who it sent to, and ConsoleSender prints. With the fake, send to Noa and Avi and print [Noa, Avi], then with ConsoleSender to Dana: to Dana: please return Ulysses.
איך הפלט אמור להיראות?What should the output look like?
[Noa, Avi]
to Dana: please return Ulysses
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:Reminder לא יודע אם זה מייל, מסוף או מזויף: זה ה-D ב-SOLID.Reminder doesn't know if it's email, a console or a fake: that's the D in SOLID.
מדף, צעד 24: Strategy ו-BuilderShelf, step 24: Strategy and Builder
פרויקט מלווהCompanion project
בנו את Emma (1815, 4), Dune (1965, 5) ו-Ulysses (1922, דירוג ברירת מחדל 3) עם Book.Builder. מיינו לפי שנה (ByYear) ואז לפי דירוג יורד (ByRatingDesc), והדפיסו [Emma, Ulysses, Dune] ו-[Dune, Emma, Ulysses].
Build Emma (1815, 4), Dune (1965, 5) and Ulysses (1922, default rating 3) with Book.Builder. Sort by year (ByYear) then by rating descending (ByRatingDesc), and print [Emma, Ulysses, Dune] and [Dune, Emma, Ulysses].
איך הפלט אמור להיראות?What should the output look like?
[Emma, Ulysses, Dune]
[Dune, Emma, Ulysses]
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:יורד: Integer.compare(b.rating(), a.rating()) (b לפני a).Descending: Integer.compare(b.rating(), a.rating()) (b before a).
enum Config { INSTANCE; } עם מפה פרטית, set ו-get (ברירת מחדל (none)). קבעו theme=dark דרך משתנה אחד וקראו דרך אחר. הדפיסו true dark (none). חשבו: מה יקרה בבדיקה שנייה שמניחה ש-theme ריק?
enum Config { INSTANCE; } with a private map, set and get (default (none)). Set theme=dark through one variable and read through another. Print true dark (none). Think: what happens in a second test that assumes theme is empty?
איך הפלט אמור להיראות?What should the output look like?
true dark (none)
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:enum עם ערך יחיד הוא הדרך הבטוחה ל-Singleton ב-Java.An enum with one value is Java's safest Singleton.
רמזHint 2:המצב נשאר בין בדיקות: זו בדיוק הבעיה.The state survives between tests: that's exactly the problem.
Orders.byName(String) מחזירה Comparator<String>: alpha (לפי א-ב) או length (לפי אורך), ואחרת זורקת unknown order 'random', use: alpha, length. מיינו pear, fig, banana בשתי הדרכים והדפיסו alpha: [banana, fig, pear], length: [fig, pear, banana], ואת ההודעה.
Orders.byName(String) returns a Comparator<String>: alpha (alphabetical) or length (by length), otherwise it throws unknown order 'random', use: alpha, length. Sort pear, fig, banana both ways and print alpha: [banana, fig, pear], length: [fig, pear, banana], and the message.
איך הפלט אמור להיראות?What should the output look like?
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:Factory שמחזירה Strategy: הקוד שממיין לא יודע אילו מחלקות קיימות.A factory returning a strategy: the sorting code doesn't know which classes exist.