TddAndConcurrency.Slides.DoOneThingWellWell

<--Back | ^Top^| Next-->

=Do One Thing ... Well ... Well=

Focus, Focus, Focus
You’ve heard it...
 * Cohesion
 * Separation of concerns
 * Single responsibility principle
 * Square law of computation

It’s even more important here > ===Why?===

A Simple Server
Imagine a server that forever… code format="java5" ServerSocket serverSocket = new ServerSocket(8009);
 * Waits for a client connection,
 * Gets a message
 * Processes it

while (true) { Socket socket = null; try { socket = serverSocket.accept; String message = getMessage(socket); process(message); } finally { close(socket); } } code

And its client
Now we need a client that... code format="java5" Socket socket = new Socket("localhost", PORT); sendMessage(socket); getMessage(socket); socket.close; code
 * Connects to the server
 * Sends a message
 * Awaits a response
 * Disconnects from the server

You need to make this faster...
Threads to the rescue, right?
 * How can I introduce threading reliably?
 * What will change in the design to support threading?
 * How can I make this easier to test?

How do we test?
What makes this hard to test? > code format="java5" ServerSocket serverSocket = new ServerSocket(8009); Socket socket = null; socket = serverSocket.accept; close(socket); code
 * Direct dependency on Sockets

> code format="java5" getMessage(socket); process(message); code
 * Embedded logic not exposed


 * Class violates Single Responsibility
 * Connection Management
 * Process client request

We want to verify threading...
Extract Classes:
 * Pull client processing into its own class
 * Pull client acquisition into its own class
 * Verify code works as is
 * Introduce threading
 * Show everything still working

Demo
Now we’ll walk through a rewrite of the class to speed it up…
 * Client Request Processing
 * Client Acquisition
 * Dependency Injection
 * Server Update

<--Back | ^Top^| Next-->