SHARE
TWEET

Untitled

a guest Oct 15th, 2019 101 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. // CHALLENGE (work in pairs):
  3. // Move from f(o) -> o.f()
  4. //
  5.  
  6.  
  7. // 1
  8. // Turn on object-thinking and re-design the code accordingly
  9. class CsvParser<T extends Line> {
  10.     Collection<T> parse(File location) {
  11.     }
  12. }
  13.  
  14. class Csv <T extends Line>{
  15.     public Csv(File location){
  16.  
  17.     }
  18.  
  19.     Collection<T> content();
  20. }
  21.  
  22. // 2
  23. // Turn on object-thinking and re-design the code accordingly
  24. interface Pinger {
  25.     void sendPing();
  26. }
  27.  
  28. interface Ping {
  29.     send();
  30. }
  31.  
  32.  
  33. // 3
  34. // rename the method to improve readability
  35. interface Input {
  36.     boolean validate();
  37. }
  38.  
  39. interface Input {
  40.     boolean isValid();
  41. }
  42.  
  43. // 4
  44. // turn procedural code into object-oriented
  45. class MortgageRiskService {
  46.  
  47.     BigDecimal calculateRisk(MortageApplication mortgageApplication) {
  48.     ...
  49.     }
  50.  
  51.     boolean isTolerable(BigDecimal risk) {
  52.     ...
  53.     }
  54.     boolean areRisksEquivalent(BigDecimal oneRisk, BigDecimal otherRisk) {
  55.     ...
  56.     }
  57. }
  58.  
  59. class Mortgage {
  60.     BigDecimal risk();
  61.     boolean isTolerable();
  62.     boolean isEquivalentTo(Mortgage other);
  63. }
  64.  
  65.  
  66.  
  67.  
  68. // 4.5
  69. // turn procedural code into object-oriented
  70. class BankruptcyProbabilityCalculator {
  71.     BigDecimal calculate(Business business) { ... }
  72. }
  73. class BankruptcyProbabilityUtils {
  74.     boolean isHigh(DecimalNumber decimal) { ... }
  75. }
  76.  
  77.  
  78. class BankruptcyProbability{
  79.     public BankruptcyProbability(Business business){
  80.  
  81.     }
  82.     boolean isHigh(){
  83.  
  84.     }
  85. }
  86.  
  87. // 5
  88. interface BlacklistingService {
  89.     boolean shouldBlockWebsiteVisitor(BlacklistRequest request);
  90. }
  91.  
  92. interface BlacklistRequest {
  93.     String getEmail();
  94.     String getIpAddress();
  95. }
  96.  
  97. class WebsiteVisitor {
  98.     public WebsiteVisitor(String email, String ipAddress);
  99.     boolean shouldBlock();
  100. }
  101.  
  102.  
  103.  
  104. // 6
  105. interface AnonymousUserAuthenticator {
  106.     Token authenticate(String username, String password) throws WrongCredentialsException;
  107. }
  108.  
  109. class UserNameAuthentication {
  110.     public Authentication(String username, String password){
  111.  
  112.     }
  113.     Token token() throws WrongCredentialsException;
  114. }
  115.  
  116.  
  117. // 7
  118. interface Suite {
  119.     interface SuiteTest {
  120.         void print();
  121.         boolean successful()
  122.     }
  123.     void runAndWait();
  124.     Collection<SuiteTest> listSuiteTests();
  125. }
  126.  
  127. suite.runAndWait()
  128. for (SuiteTest suiteTest : suite.listSuiteTests()) {
  129.     if (!suiteTest.successful() ) {
  130.         // pretty printing
  131.         suiteTest.print();
  132.     }
  133. }
  134.  
  135. interface SuiteTest {
  136.     void print();
  137.     boolean successful()
  138. }
  139.  
  140. class Suite{
  141.     Collection<SuiteTest> listSuiteTests = new ...;
  142.     void addTest(SuiteTest test) {
  143.         tests.add(test);
  144.     }
  145.     runAndWait(){
  146.         for (SuiteTest suiteTest : listSuiteTests) {
  147.             if (!suiteTest.successful() ) {
  148.                 // pretty printing
  149.                 suiteTest.print();
  150.             }
  151.         }
  152.     }
  153. }
  154.  
  155.  
  156.  
  157. // 8.1
  158. // What's the expected return type of the method (if you look at the code from the outside?)
  159. // If you didn't expect boolean (as most develoeprs), rename it so it's clear that the boolean is returned
  160. enum Severity {
  161.     boolean isMajor()
  162. }
  163.  
  164.  
  165.  
  166. // 8.2
  167. // What's the expected return type of the method (if you look at the code from the outside?)
  168. // If you didn't expect boolean (as most develoeprs), rename it so it's clear that the boolean is returned
  169. class Specification {
  170.     boolean isSatisfiedBy(Entity entity);
  171. }
  172.  
  173. class Entity {
  174.     boolean satisfies(Specification spec);
  175. }
  176.  
  177.  
  178.     // 9 SLAP!
  179.     boolean destroyButtonAvailable =
  180.             widgets
  181.                     .stream()
  182.                     .filter(Widget::isButton)
  183.                     .filter(button -> button.label().equals("Destroy The World"))
  184.                     .isPresent();
  185.  
  186.  
  187.     void destroyButtonAvailable () {
  188.         widgets
  189.                 .stream()
  190.                 .filter(Widget::isButton)
  191.                 .filter(this::buttonToDestroy)
  192.                 .isPresent();
  193.     }
  194.  
  195.  
  196.     boolean buttonToDestroy(Button button){
  197.         return button.label().equals("Destroy The World");
  198.     }
  199.  
  200.  
  201.  
  202.  
  203.     // 10 transform a full name into a twitter handle. Jack Ma must become @jack.ma
  204.     String fullName = "Jack Ma";
  205. ...
  206.         String twitterHandle =
  207.  
  208. class TwitterName {
  209.     String name;
  210.  
  211.     public TwitterName(String fullName){
  212.         name = '@' + Arrays.asList(fullName.split(" ")).stream().collect(Collectors.joining(".")).toLowercase();
  213.     }
  214.  
  215.     public String toString(){
  216.         return name;
  217.     }
  218. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top