For example, suppose you create a method like this:
Factory.Java
public class Factory {
/**
*Creates and returns a new instance of MyObj.<br>
*If creation fails, null is returned.
*
* @return
*/
public MyObj createMyObj() {
// ...
}
}
The side that uses this method will have code like this.
python
Factory factory;
// ...
MyObj myObj = factory.createMyObj();
if (myObj != null) {
//Processing on success
}
I tried to return Optional <>.
Factory.Java
public class Factory {
/**
*Creates and returns a new instance of MyObj.<br>
*If creation fails, null is returned.
*
* @return
*/
public Optional<MyObj> createMyObj() {
// ...
}
}
The user side looks like this.
python
Optional<MyObj> myObj = factory.createMyObj();
myObj.ifPresent(o -> {
//Processing on success
});
Recommended Posts