Don’t just create Setter methods, try creating operation methods

public class Monitor {
private Integer active;
public Integer getActive() {
return active;
}
public void setActive(Integer active) {
this.active = active;
}
}
public class MonitorDto {
private boolean active;
//setter and getter here
}
public class MonitorService {
public void saveMonitor(Monitor monitor) {
//save the monitor to the database
}
public void activateMonitor(Long monitorId) {
Monitor monitor = findMonitorById(monitorId); //say method exists
monitor.setActive(1);
saveMonitor(monitor);
}
}
public class MonitorController { @Autowired
private MonitorService monitorService;
@PutMethod("/monitor/save")
public String save(@RequestParam MonitorDto monitorDto) {
Monitor monitor = new Monitor();
monitor.setActive(monitorDto.getActive() ? 1 : 0);
monitorService.saveMonitor(monitor);
return ... //something
//skipping handling errors for code simplicity
}
@GetMapping("/monitor/activate/{id}")
public String save(@PathVariable Long id) {
monitorService.activateMonitor(id);
return ... //something
//skipping handling errors for code simplicity
}
}

Solution 1: Keep it simple

public class Monitor {
private Integer active;
public boolean getIsActive() {
return active ? 1 : 0;
}
public void setActive(boolean active) {
this.active = active ? 1 : 0;
}
}
public class MonitorDto {
private boolean active;
//setter and getter here
}
public class MonitorService {
public void saveMonitor(Monitor monitor) {
//save the monitor to the database
}
public void activateMonitor(Long monitorId) {
Monitor monitor = findMonitorById(monitorId); //say method exists
monitor.setIsActive(true);
saveMonitor(monitor);
}
}
public class MonitorController { @Autowired
private MonitorService monitorService;
@PutMethod("/monitor/save")
public String save(@RequestParam MonitorDto monitorDto) {
Monitor monitor = new Monitor();
monitor.setActive(monitorDto.getActive());
monitorService.saveMonitor(monitor);
return ... //something
//skipping handling errors for code simplicity
}
@GetMapping("/monitor/activate/{id}")
public String save(@PathVariable Long id) {
monitorService.activateMonitor(id);
return ... //something
//skipping handling errors for code simplicity
}
}

Solution 2: Borrow from DDD

public class Monitor {
private Integer active;
public Monitor(active) {
this.active = active ? 1 : 0;
}

public boolean getIsActive() {
return active ? 1 : 0;
}

public void activate() {
this.active = 1;
}

public void deactivate() {
this.active = 0;
}
}
public class MonitorDto {
private boolean active;
//setter and getter here
}
public class MonitorService {
public void saveMonitor(Monitor monitor) {
//save the monitor to the database
}
public void activateMonitor(Long monitorId) {
Monitor monitor = findMonitorById(monitorId); //say method exists
monitor.activate();
saveMonitor(monitor);
}
}
public class MonitorController {@Autowired
private MonitorService monitorService;
@PutMethod("/monitor/save")
public String save(@RequestParam MonitorDto monitorDto) {
Monitor monitor = new Monitor(monitorDto.getActive());
monitorService.saveMonitor(monitor);
return ... //something
//skipping handling errors for code simplicity
}
@GetMapping("/monitor/activate/{id}")
public String save(@PathVariable Long id) {
monitorService.activateMonitor(id);
return ... //something
//skipping handling errors for code simplicity
}
}

--

--

--

Java, Golang, PHP, JavaScript, Databases

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

Recommended from Medium

Getting Started with Python for Google Cloud Functions

Latest Trends in Web Development at the Moment

Come, Film Makers, Come. the Game Engine Is Afoot!

Hobbit house for a sold sign out the front as Weta Digital is bought by Unity3d

It’s Android Rewind Time !!

How to Create Energy

Greyboxing a top-down space shooter in Unity

Lawyers in Utah

lawyers in utah

Coding in Swift Playgrounds

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
Eli Segev

Eli Segev

Java, Golang, PHP, JavaScript, Databases

More from Medium

Visitor Design Pattern in Java

Use infinispan in JAVA

Introduction to Dynamic Proxies in Java