Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // CHALLENGE (work in pairs):
- // Move from f(o) -> o.f()
- //
- // 1
- // Turn on object-thinking and re-design the code accordingly
- class CsvParser<T extends Line> {
- Collection<T> parse(File location) {
- }
- }
- class Csv <T extends Line>{
- public Csv(File location){
- }
- Collection<T> content();
- }
- // 2
- // Turn on object-thinking and re-design the code accordingly
- interface Pinger {
- void sendPing();
- }
- interface Ping {
- send();
- }
- // 3
- // rename the method to improve readability
- interface Input {
- boolean validate();
- }
- interface Input {
- boolean isValid();
- }
- // 4
- // turn procedural code into object-oriented
- class MortgageRiskService {
- BigDecimal calculateRisk(MortageApplication mortgageApplication) {
- ...
- }
- boolean isTolerable(BigDecimal risk) {
- ...
- }
- boolean areRisksEquivalent(BigDecimal oneRisk, BigDecimal otherRisk) {
- ...
- }
- }
- class Mortgage {
- BigDecimal risk();
- boolean isTolerable();
- boolean isEquivalentTo(Mortgage other);
- }
- // 4.5
- // turn procedural code into object-oriented
- class BankruptcyProbabilityCalculator {
- BigDecimal calculate(Business business) { ... }
- }
- class BankruptcyProbabilityUtils {
- boolean isHigh(DecimalNumber decimal) { ... }
- }
- class BankruptcyProbability{
- public BankruptcyProbability(Business business){
- }
- boolean isHigh(){
- }
- }
- // 5
- interface BlacklistingService {
- boolean shouldBlockWebsiteVisitor(BlacklistRequest request);
- }
- interface BlacklistRequest {
- String getEmail();
- String getIpAddress();
- }
- class WebsiteVisitor {
- public WebsiteVisitor(String email, String ipAddress);
- boolean shouldBlock();
- }
- // 6
- interface AnonymousUserAuthenticator {
- Token authenticate(String username, String password) throws WrongCredentialsException;
- }
- class UserNameAuthentication {
- public Authentication(String username, String password){
- }
- Token token() throws WrongCredentialsException;
- }
- // 7
- interface Suite {
- interface SuiteTest {
- void print();
- boolean successful()
- }
- void runAndWait();
- Collection<SuiteTest> listSuiteTests();
- }
- suite.runAndWait()
- for (SuiteTest suiteTest : suite.listSuiteTests()) {
- if (!suiteTest.successful() ) {
- // pretty printing
- suiteTest.print();
- }
- }
- interface SuiteTest {
- void print();
- boolean successful()
- }
- class Suite{
- Collection<SuiteTest> listSuiteTests = new ...;
- void addTest(SuiteTest test) {
- tests.add(test);
- }
- runAndWait(){
- for (SuiteTest suiteTest : listSuiteTests) {
- if (!suiteTest.successful() ) {
- // pretty printing
- suiteTest.print();
- }
- }
- }
- }
- // 8.1
- // What's the expected return type of the method (if you look at the code from the outside?)
- // If you didn't expect boolean (as most develoeprs), rename it so it's clear that the boolean is returned
- enum Severity {
- boolean isMajor()
- }
- // 8.2
- // What's the expected return type of the method (if you look at the code from the outside?)
- // If you didn't expect boolean (as most develoeprs), rename it so it's clear that the boolean is returned
- class Specification {
- boolean isSatisfiedBy(Entity entity);
- }
- class Entity {
- boolean satisfies(Specification spec);
- }
- // 9 SLAP!
- boolean destroyButtonAvailable =
- widgets
- .stream()
- .filter(Widget::isButton)
- .filter(button -> button.label().equals("Destroy The World"))
- .isPresent();
- void destroyButtonAvailable () {
- widgets
- .stream()
- .filter(Widget::isButton)
- .filter(this::buttonToDestroy)
- .isPresent();
- }
- boolean buttonToDestroy(Button button){
- return button.label().equals("Destroy The World");
- }
- // 10 transform a full name into a twitter handle. Jack Ma must become @jack.ma
- String fullName = "Jack Ma";
- ...
- String twitterHandle =
- class TwitterName {
- String name;
- public TwitterName(String fullName){
- name = '@' + Arrays.asList(fullName.split(" ")).stream().collect(Collectors.joining(".")).toLowercase();
- }
- public String toString(){
- return name;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement