Last active
January 31, 2020 13:11
-
-
Save marlonklc/5d476296d957666f91fe68ca84c0f5a9 to your computer and use it in GitHub Desktop.
[spring-webflux] best way to build a object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Food { | |
private String pizza; | |
private String hamburger; | |
public String getPizza() { | |
return pizza; | |
} | |
public void setPizza(String pizza) { | |
this.pizza = pizza; | |
} | |
public String getHamburger() { | |
return hamburger; | |
} | |
public void setHamburger(String hamburger) { | |
this.hamburger = hamburger; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FoodBuilder { | |
private String pizza; | |
private String hamburger; | |
public FoodBuilder() { | |
} | |
public FoodBuilder pizza(String pizza) { | |
this.pizza = pizza; | |
return this; | |
} | |
public FoodBuilder hamburger(String hamburger) { | |
this.hamburger = hamburger; | |
return this; | |
} | |
public Food build() { | |
Food food = new Food(); | |
food.setPizza(pizza); | |
food.setHamburger(hamburger); | |
return food; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import reactor.core.publisher.Mono; | |
public class OrderFood { | |
private PizzaService pizzaService = new PizzaService(); | |
private HamburgerService hamburgerService = new HamburgerService(); | |
public Mono<Food> getOrder(int orderId) { | |
return Mono.just(new FoodBuilder()) | |
.zipWith(pizzaService.getPizza(orderId), FoodBuilder::pizza) | |
.zipWith(hamburgerService.getHamburger(orderId), FoodBuilder::hamburger) | |
.map(FoodBuilder::build); | |
} | |
public class PizzaService { | |
public Mono<String> getPizza(int orderId) { | |
return Mono.just("pizza xx to order: " + orderId); | |
} | |
} | |
public class HamburgerService { | |
public Mono<String> getHamburger(int orderId) { | |
return Mono.just("hamburger yy to order: " + orderId); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment