Prezi

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in the manual

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Adapt Parameter

A short presentation working through the Adapt Parameter example from Michael Feathers book
by Nicholas Vaidyanathan on 19 September 2012

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Adapt Parameter

Adapt Parameter Conclusion Thank you for your attention! Try it on: git@github.com:leadVisionary/PHX_Softcrafties.git is here Often we prefer to Extract Interface, which is usually the best choice

But we can't if
Parameter's type is pretty low level
Type is specific to an implementation tech
Extracting an interface is counterproductive/impossible public class ARMDispatcher
{
public void populate(HttpServletRequest request) {
String [] values
= request.getParameterValues(pageStateName);
if (values != null && values.length > 0)
{
marketBindings.put(pageStateName + getDateStamp(),
values[0]);
}
...
}
...
} HttpServletRequest is a J2EE core interface.

Instantiating a new one is hard in a test harness. We have to create a class that implements all its methods.

Lotsa methods!
http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html

What we want is an interface with only the thing we need to use.

Mock? Breaking Dependency is Hard Counter-productive? Impossible? Someone else's interface sucks Use The ADAPTER PATTERN to transform an external interface into an internal interface!

Move toward interfaces that communicate responsibilities rather than implementation details. This makes code easier to read and easier to maintain. A Better Way? Create an Interface Finished Create Fake for Testing Create Adapter for Production Start (cc) image by nuonsolarteam on Flickr interface ParameterSource {
String getParameterForName(String name);
} class FakeParameterSource implements ParameterSource
{
public String value;
public String getParameterForName(String name) {
return value;
}
} class ServletParameterSource implements ParameterSource
{
private HttpServletRequest request;
public ServletParameterSource(HttpServletRequest request) {
this.request = request;
}
String getParameterValue(String name) {
String [] values = request.getParameterValues(name);
if (values == null || values.length < 1)
return null;
return values[0];
}
} Replace with Adapter public class ARMDispatcher
public void populate(ParameterSource source) {
String values = source.getParameterForName(pageStateName);
if (value != null) {
marketBindings.put(pageStateName + getDateStamp(),
value);
}
...
}
} Write Tests! public class ArmsDispatcherTest{

@Test
public void testPopulate(){
ARMDispatcher toTest = new ARMDispatcher();
FakeParameterSource fake = new FakeParameterSource("boo");
toTest.populate(fake);
assertContains("boo", toTest.marketBindings.values());
}
} Adapt Parameter is one case in which we don't Preserve Signatures (312). Use extra care.

Remember that the goal is to break dependencies well enough to get tests in place. Your bias should be toward making changes that you feel more confident in rather than changes that give you the best structure. Those can come after your tests

Safety first. Once you have tests in place, you can make invasive changes much more confidently. Risky!
See the full transcript