15. Flyweight
Sharing the part of an object that does not vary, so a thousand uses cost one object
Page measures a line of text by making a Glyph for each character and adding up the widths. The tests pass, and the answer is right.
It makes one Glyph per character position. For a word that is fine. For a document it is one object per letter on the page, and a glyph holds nothing that depends on where the letter sits.
GlyphCache.of() hands back the same Glyph for the same character every time. Rewrite width() to use it.
This one is graded on how many objects you make, not just on the number you return. Glyph counts itself.
Run compiles every file and runs Tests.java — the tests you can read.
Submit runs those again, then applies 1 requirement you have not seen, one at a time.
Your code is never edited. Each requirement is added to the project as it stands. If the design is right, nothing you wrote has to change.
- Ctrl/⌘ + Enter runs the tests
Run the tests to see where you are.