Last active
January 20, 2026 17:36
-
-
Save trikitrok/f6838c13b24e6509591617f8980ac1a0 to your computer and use it in GitHub Desktop.
Refactoring Adapt Parameter example to better abstractions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface LinePrinter { | |
| void print(String line); | |
| } | |
| interface LibraryData { | |
| String getLibraryName(); | |
| } | |
| class ConsoleLinePrinter implements LinePrinter { | |
| private final Console console; | |
| public ConsoleLinePrinter(Console console) { | |
| this.console = console; | |
| } | |
| @Override | |
| public void print(String line) { | |
| console.printf(line); | |
| } | |
| } | |
| class ConsoleLibraryData implements LibraryData { | |
| private final Console console; | |
| public ConsoleLibraryData(Console console) { | |
| this.console = console; | |
| } | |
| @Override | |
| public String getLibraryName() { | |
| return console.readLine(); | |
| } | |
| } | |
| record LibraryInformation(String libraryName, List<Book> books) { | |
| } | |
| interface LibraryInformationPrinter { | |
| void print(LibraryInformation libraryInformation); | |
| } | |
| class LineByLineLibraryInformationPrinter implements LibraryInformationPrinter { | |
| private final LinePrinter linePrinter; | |
| public LineByLineLibraryInformationPrinter(LinePrinter linePrinter) { | |
| this.linePrinter = linePrinter; | |
| } | |
| @Override | |
| public void print(LibraryInformation libraryInformation) { | |
| linePrinter.print(libraryInformation.libraryName()); | |
| for (Book book : libraryInformation.books()) { | |
| linePrinter.print(book.getName()); | |
| } | |
| } | |
| } | |
| public class Library { | |
| private final LibraryData libraryData; | |
| private final LibraryInformationPrinter libraryInformationPrinter; | |
| public Library(LibraryData libraryData, LibraryInformationPrinter libraryInformationPrinter) { | |
| this.libraryData = libraryData; | |
| this.libraryInformationPrinter = libraryInformationPrinter; | |
| } | |
| public void printBooks(List<Book> books) { | |
| libraryInformationPrinter.print(libraryInformation(books)); | |
| } | |
| private LibraryInformation libraryInformation(List<Book> books) { | |
| return new LibraryInformation( | |
| libraryData.getLibraryName(), | |
| List.copyOf(books) | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment