Apache Kafka: A high-throughput distributed messaging system @ JCConf 2014
JCConf TW 2014 - Modern Design Pattern
-
Upload
steven-wang -
Category
Presentations & Public Speaking
-
view
478 -
download
0
description
Transcript of JCConf TW 2014 - Modern Design Pattern
![Page 1: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/1.jpg)
JCConf Taiwan 2014
MODERNDESIGN PATTERN
![Page 2: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/2.jpg)
ABOUT ME王文農 a.k.a StevenSoftleader engineer, for build application and framework.Research interests are adjusted efficiency and effectiveness ofalgorithms.Play some of new frameworks/libraries and technology.
![Page 3: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/3.jpg)
If you want to be a cool guy.
JOIN SOFTLEADER.
![Page 4: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/4.jpg)
WHAT IS DESIGN PATTERN?
![Page 5: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/5.jpg)
Descriptions of communicating objects andclasses that are customized to solve a general
design problem.
–Gamma, et. al
![Page 6: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/6.jpg)
THE ONE CONSTANT IN SOFTWARE DEVELOPMENT:
CHANGE!
![Page 7: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/7.jpg)
TIGHT COUPLING!
![Page 8: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/8.jpg)
IT MIGHT GET YOU INTO TROUBLE
![Page 9: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/9.jpg)
BEWARE OF
![Page 10: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/10.jpg)
GOF: 23
![Page 11: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/11.jpg)
![Page 12: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/12.jpg)
MVC PATTERN
![Page 13: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/13.jpg)
A PROBLEM OR A SOLUTION ?Commonly reasons:
Reduced Code complexityCode reuseIncreased flexibilityDecoupled code
![Page 14: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/14.jpg)
COOL, SOUNDS NICE, BUTIs it true ?Do all other patterns lack these cool things ?
![Page 15: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/15.jpg)
THE ANSWER IS NO!Doesn't solve the code complexity problemDoesn't solve the code reuse or no-flexibility problemDoesn't guarantee decoupled code
![Page 16: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/16.jpg)
MVC COMPONENTS
![Page 17: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/17.jpg)
HOW MVC WORKS
![Page 18: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/18.jpg)
![Page 19: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/19.jpg)
![Page 20: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/20.jpg)
![Page 21: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/21.jpg)
POOR MVC GIVE BIRTH TO
![Page 23: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/23.jpg)
MODEL-VIEW-CONTROLLER
![Page 24: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/24.jpg)
MODELpublic class Model {
private String name;
public void setName(String name) { // business logic this.name = name; }
// for view access public String getName() { return name; }}
![Page 25: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/25.jpg)
VIEW<h1>Hello, ${model.name}</h1>
<form action="controller" method="post"> <label for="name">My name is </label> <input type="text" id="name" name="name" value="${model.name}" /> <button>Submit</button></form>
<%-- receive data --%>
![Page 26: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/26.jpg)
CONTROLLER@WebServlet(value = "/controller")public class Controller extends HttpServlet {
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // accepting request String name = req.getParameter("name");
// validate request if (name.isEmpty()) { name = "Guest"; }
// use Model to do business logic Model model = new Model(); model.setName(name);
// binding model to view req.setAttribute("model", model); // forward to view req.getRequestDispatcher("/v/view.jsp").forward(req, resp); }
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // accepting request
// use Model to do business logic Model model = new Model(); model.setName("Guest");
![Page 28: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/28.jpg)
MODEL-VIEW-PRESENTER
![Page 29: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/29.jpg)
MODELpublic class Model {
private String name;
public void setName(String name) { // business logic this.name = name; }
// for presenter access public String getName() { return name; }}
![Page 30: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/30.jpg)
VIEWpublic class View extends CustomComponent {
private Label display = new Label("Hello, Guest"); private TextField name = new TextField("My name is "); private Button button = new Button("Submit");
private Presenter presenter;
public View() { // initial layout and presenter initialLayout(); presenter = new Presenter(this); }
protected void initialLayout() { CustomLayout layout = new CustomLayout("view"); layout.addComponent(display, "display"); layout.addComponent(name, "name"); button.addClickListener(new ClickListener() {
@Override public void buttonClick(ClickEvent event) { // when button click then dispatch to presenter presenter.changeDisplay(); } }); layout.addComponent(button, "submit"); setCompositionRoot(layout); }
// view provide getters & setters
![Page 31: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/31.jpg)
PRESENTERpublic class Presenter {
private View view; private Model model;
public Presenter(View view) { this.view = view; this.model = new Model(); }
public void changeDisplay() { String name = view.getInput().getValue();
// do some logic, eg: validate if (name.isEmpty()) { name = "Guest"; }
// call model model.setName(name);
// control view view.setDisplay("Hello, " + model.getName()); }}
![Page 33: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/33.jpg)
MODEL-VIEW-VIEWMODEL
![Page 34: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/34.jpg)
MODELpublic class Model {
private String name;
public void setName(String name) { // business logic this.name = name; }
public String getName() { return name; }}
![Page 35: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/35.jpg)
VIEW<zk> <window title="MVVM Example" border="normal" width="100%" apply="org.zkoss.bind.BindComposer" viewModel="@id('vm') @init('tw.jcconf.example.mvvm.vm.ViewModel')"> <hbox> Hello, <label value="@load(vm.model.name)" /> </hbox> <hbox> <label value="My name is " /><textbox value="@bind(vm.name)" /> <button onClick="@command('changeName')" label="Submit" /> </hbox> </window></zk>
![Page 36: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/36.jpg)
VIEWMODELpublic class ViewModel {
private String name; private Model model;
public ViewModel() { model = new Model(); model.setName(name = "Guest"); }
// event handler @Command @NotifyChange("model") public void changeName() throws Exception { model.setName(name); }
// ViewModel provider getter & setter
public void setName(String name) { this.name = name; }
public String getName() { return name; }
public Model getModel() { return model; }}
![Page 37: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/37.jpg)
RESTFUL PATTERN
![Page 38: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/38.jpg)
An architectural style that abstracts thearchitectural elements within a distributed
hypermedia system.
–Wikipedia
![Page 39: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/39.jpg)
THE MEAS ISUse HTTP methods explicitly.Be stateless.Expose directory structure-like URIs.Transfer XML, JavaScript Object Notation(JSON), or both.
–IBM's developerWorks website
![Page 40: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/40.jpg)
WHAT IS REST ?
![Page 41: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/41.jpg)
HTTP ITSELF IS REST IMELEMENTATION
![Page 42: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/42.jpg)
HOW TO USE ?User Action HTTP MethodCreate POST/PUTRead (Retrieve) GETUpdate (Modify) PUT/PATCHDelete (Destroy) DELETE
![Page 43: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/43.jpg)
LOOK ALIKEUser Action SQLCreate INSERTRead (Retrieve) SELECTUpdate (Modify) UPDATEDelete (Destroy) DELETE
![Page 44: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/44.jpg)
FOLLOW HTTP TO DESIGN RESTFUL WEB SERVICE
NounsDefine the URL for your resources
VerbsChoose the right HTTP method
Content TypesChoose content-type which you want to return
![Page 45: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/45.jpg)
« ‹ › »
![Page 46: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/46.jpg)
CREATEPOST http://domain/books
{ "isbn": "123456789", "title": "Modern Design Pattern", "author": "Steven Wang", "publisher": "JCConf TW", "year": "2014"}
HTTP/1.1 200 | 401{ "id": "9876543"}
![Page 47: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/47.jpg)
READGET http://domain/books/123456789
or
GET http://domain/books?isbn=123456789
HTTP/1.1 200 | 404[{ "id": "9876543", "isbn": "123456789", "title": "Modern Design Pattern", "author": "Steven Wang", "publisher": "JCConf TW", "year": "2014"}]
![Page 48: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/48.jpg)
UPDATEPUT http://domain/books/9876543
or
PUT http://domain/books{ ["id": "9876543",] "isbn": "123456789",, "title": "Modern Design Pattern", "author": "Steven Wang", "publisher": "JCConf TW", "year": "2014"}
HTTP/1.1 200 | 401 |404
![Page 49: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/49.jpg)
DELETEDELETE http://domain/books/9876543
or
DELETE http://domain/books?isbn=9876543
HTTP/1.1 200 | 401 |404
![Page 51: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/51.jpg)
@RestController@RequestMapping("/books")public class BooksResource {
@Autowired BookService service;
@RequestMapping(consumes = "application/json", produces = "application/json", method = RequestMethod.GET) @ResponseBody public List<Book> getAll() { return service.getAll(); }
@RequestMapping(value = "/{isbn}", consumes = "application/json", produces = "application/json" @ResponseBody public Book getByIsbn(@PathVariable("isbn") String isbn) { return service.getBookByIsbn(isbn); }
@RequestMapping(consumes = "application/json", produces = "application/json", method = RequestMethod.POST) @ResponseBody public Book save(@RequestBody Book book) { return service.save(book); }
@RequestMapping(consumes = "application/json", produces = "application/json", method = RequestMethod.PUT) @ResponseBody public Book update(@RequestBody Book book) { return service.update(book); }
@RequestMapping(value = "/{id}", consumes = "application/json", produces = "application/json" public void delete(@PathVariable("id") Long id) {
![Page 52: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/52.jpg)
CQRS PATTERN
![Page 53: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/53.jpg)
WHAT IS CQRS ?
![Page 54: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/54.jpg)
CQRS is simply the creation of two objects wherethere was previously only one. The separationoccurs based upon whether the methods are a
command or a query (the same definition that isused by Meyer in Command and Query
Separation, a command is any method thatmutates state and a query is any method that
returns a value).
–Wikipedia
![Page 55: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/55.jpg)
Say what?
![Page 56: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/56.jpg)
PUT ANOTHER WAY...Command/Query Responsibility Segregation (CQRS) is the idea
that you can use a different model to update information than themodel you use to read information.
IN THIS CONTEXT,Commands = WritesQueries = Reads
![Page 57: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/57.jpg)
OK, BUT WHY ?
![Page 58: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/58.jpg)
WHAT IS CQRS NEED ?
![Page 59: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/59.jpg)
![Page 60: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/60.jpg)
MULTI-USER SYSTEM
![Page 61: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/61.jpg)
TYPICAL APPLICATION OF THE CQRS PATTERN
![Page 62: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/62.jpg)
COMMANDS AND EVENTS IN THE CQRS PATTERN
![Page 63: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/63.jpg)
WHEN TO AVOID CQRS ?So, when should you avoid CQRS?
The answer is most of the time...
–Udi Dahan
![Page 64: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/64.jpg)
OK, SOME SUGGESTIONSLarge team.Difficult business logic.Scalability matters.Your top people can work on domain logic.
![Page 65: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/65.jpg)
FINALLY
![Page 66: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/66.jpg)
PATTERNS DON’T SOLVE PROBLEMS,THEY HELP US
![Page 67: JCConf TW 2014 - Modern Design Pattern](https://reader034.fdocuments.us/reader034/viewer/2022052508/559c60b11a28abd73d8b4758/html5/thumbnails/67.jpg)