The Interface Segregation Principle With Lambdas

Garden in blossom by Kazimir Malevich

The Problem

class UserService {

User register(String username) { … }

User find(String username) { … }

void lock(User user) { … }
}

class UserRegistrationClient {

private UserService userService;

/*constructor*/

void registerUser() {
String username = …

User user = userService
.register(username);

}
}
interface RegisterUser {

User register(String username);
}

class UserService implements RegisterUser {

/* as above */
}

class UserRegistrationClient {

private RegisterUser registerUser;

/*constructor*/

void registerUser() {
String username = …

User user = registerUser
.register(username);

}
}

The Solution

class UserService {

/* as above */
}

class UserRegistrationClient {

private Function<String,User> registerUser;

/*constructor*/

void registerUser() {
String username = …

User user = registerUser
.apply(username);

}
}
new UserRegistrationClient(
userService::register
);
class UserRegistrationClient {

constructor(registerUser) {
this._registerUser = registerUser;
}

registerUser() {
let username = …
this._registerUser(username);

}
}
class UserRegistrationClient {

constructor(userService) {
this._userService = userService;
}

registerUser() {
let username = …
this._userService
.register(username);

}
}

Conclusion

--

--

--

Software developer and occasional blogger: https://blog.ttulka.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Coursera’s Applied data science with Python specialization Review? Is it Really Worth it?

Coursera’s Applied data science with Python specialization by university of Michigan review

Advanced Way to Remove PST Duplicates From Outlook

Building a Rails API with Pragma (Part 1)

Kilohearts — On Request

Integrating Data Quality Management into Continuous integration and continuous delivery (CI/CD)

Cloud Computing, When?

INFORMATION ON CMS MAINTENANCE

4 practical methods to set your stop-loss when algo-trading Bitcoin

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tomas Tulka

Tomas Tulka

Software developer and occasional blogger: https://blog.ttulka.com

More from Medium

Refactoring for a better future

Rubber Duck Debugging

Self-Documenting Code is Not Enough

Why We Shouldn’t Design Software the Same Way We Design Physical Things