Skip to content

Instantly share code, notes, and snippets.

@jbrains
Forked from CoryFoy/sample.rb
Last active November 28, 2016 21:20
Show Gist options
  • Save jbrains/34e8718cf773dade3b7bf28081e858b0 to your computer and use it in GitHub Desktop.
Save jbrains/34e8718cf773dade3b7bf28081e858b0 to your computer and use it in GitHub Desktop.
TDD no Expectations
package ca.jbrains.pos;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.HashMap;
public class PointOfSaleTerminal {
public static void main(String[] args) throws IOException {
final InputStreamReader commandSource = new InputStreamReader(System.in);
final SanitizeTextCommand sanitizeCommand = new SanitizePointOfSaleCommand();
final InterpretCommand interpretCommand = new WarnAboutEmptyCommands(
new SellOneItemController(
new InMemoryCatalog(
new HashMap<String, Price>() {{
put("8586013802135", Price.cents(1000));
put("12345", Price.cents(150));
}}
)
)::onBarcode
);
final CommandResponseFormat commandResponseFormat = CommandResponse::render;
final DisplayText displayText = new UdpPostOffice(
InetAddress.getLocalHost(),
5358,
"US-ASCII"
)::sendMessage;
new StreamTextCommands()
.streamCommandsFrom(commandSource)
.flatMap(sanitizeCommand::apply)
.map(interpretCommand::apply)
.map(commandResponseFormat::apply)
.forEach(displayText::apply);
}
}
@jbrains
Copy link
Author

jbrains commented Nov 28, 2016

It doesn't say much about the rest of the system yet, but this entry point manages to show the main event loop of a CLI application as consuming a stream of commands and building a pipeline to process them. This arrangement looks fundamentally different from my usual approach, in which I create a graph of objects, tell them to process stdin, and then have no clear picture of the overall flow of events. I haven't decided which one I prefer, although I have seen at least one thing to like about this approach. I have found other things that I don't like, but I haven't yet decided whether that amounts to my unlearning bad habits or inherent weakness in the approach.

I know for sure that I'd like to change the sanitize-interpret-format-display cycle from mapping over the entire stream of lines of text to becoming a pipeline processing a single line. I'm going to want to do that very soon, since I want to implement a "quit" command next.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment