Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

JCConf TW 2014 - Modern Design Pattern

1.927 Aufrufe

Veröffentlicht am

http://wang-steven.github.io/jcconf-tw-2014

1. MVC Pattern
過去 MVC 模式並不適合小型甚至中等規模的應用程式,這樣會帶來額外的工作量,增加應用的複雜性。 但現在多數軟體設計框架,能直接快速提供 MVC 骨架,供中小型應用程式開發,此問題不再存在。 對於開發存在大量使用者介面,並且邏輯複雜的大型應用程式,MVC 將會使軟體在健壯性、代碼重用和結構方面上一個新的台階。 儘管在最初構建 MVC 模式框架時會花費一定的工作量,但從長遠的角度來看,它會大大提高後期軟體開發的效率。 目前已經是各個 programmer 必備的基本觀念。

2. RESTful Pattern
最早是在2000年被提出,目前在三種主流的 Web 服務實現方案中,因為REST模式與複雜的 SOAP 和 XML-RPC 相比更加簡潔,越來越多的 Web 服務開始採用 REST 風格設計和實現已經有愈來愈多的 framework 支援。

3. CQRS Pattern
目前在 Domain Driven Design 中被廣泛的使用。在大型、高複雜度的專案中及對效能有較高要求的系統可以明顯體會到其價值

Veröffentlicht in: Präsentationen & Vorträge
  • Good presentation, explain MVC framework very well
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier

JCConf TW 2014 - Modern Design Pattern

  1. 1. JCConf Taiwan 2014 MODERN DESIGN PATTERN
  2. 2. ABOUT ME 王文農a.k.aSteven Softleader engineer, for build application and framework. Research interests are adjusted efficiencyand effectiveness of algorithms. Playsome of new frameworks/libraries and technology.
  3. 3. If you wantto be acoolguy. JOIN SOFTLEADER.
  4. 4. WHAT IS DESIGN PATTERN?
  5. 5. Descriptions of communicating objects and classes thatare customized to solve ageneral design problem. –Gamma, et. al
  6. 6. THE ONE CONSTANT IN SOFTWARE DEVELOPMENT: CHANGE!
  7. 7. TIGHT COUPLING!
  8. 8. IT MIGHT GET YOU INTO TROUBLE
  9. 9. BEWARE OF
  10. 10. GOF: 23
  11. 11. MVC PATTERN
  12. 12. A PROBLEM OR A SOLUTION ? Commonlyreasons: Reduced Code complexity Code reuse Increased flexibility Decoupled code
  13. 13. COOL, SOUNDS NICE, BUT Is ittrue ? Do allother patterns lack these coolthings ?
  14. 14. THE ANSWER IS NO! Doesn'tsolve the code complexity problem Doesn'tsolve the code reuse or no-flexibility problem Doesn'tguarantee decoupled code
  15. 15. MVC COMPONENTS
  16. 16. HOW MVC WORKS
  17. 17. POOR MVC GIVE BIRTH TO
  18. 18. MVC DEMO WITH SERVLET
  19. 19. MODEL-VIEW-CONTROLLER
  20. 20. MODEL publicclassModel{ privateStringname; publicvoidsetName(Stringname){ //businesslogic this.name=name; } //forviewaccess publicStringgetName(){ returnname; } }
  21. 21. VIEW <h1>Hello,${model.name}</h1> <formaction="controller"method="post"> <labelfor="name">Mynameis</label> <inputtype="text"id="name"name="name"value="${model.name}"/> <button>Submit</button> </form> <%--receivedata--%>
  22. 22. CONTROLLER @WebServlet(value="/controller") publicclassControllerextendsHttpServlet{ @Override protectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServlet //acceptingrequest Stringname=req.getParameter("name"); //validaterequest if(name.isEmpty()){ name="Guest"; } //useModeltodobusinesslogic Modelmodel=newModel(); model.setName(name); //bindingmodeltoview req.setAttribute("model",model); //forwardtoview req.getRequestDispatcher("/v/view.jsp").forward(req,resp); } @Override protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletE //acceptingrequest //useModeltodobusinesslogic Modelmodel=newModel(); model.setName("Guest");
  23. 23. MVP DEMO WITH VAADIN
  24. 24. MODEL-VIEW-PRESENTER
  25. 25. MODEL publicclassModel{ privateStringname; publicvoidsetName(Stringname){ //businesslogic this.name=name; } //forpresenteraccess publicStringgetName(){ returnname; } }
  26. 26. VIEW publicclassViewextendsCustomComponent{ privateLabeldisplay=newLabel("Hello,Guest"); privateTextFieldname=newTextField("Mynameis"); privateButtonbutton=newButton("Submit"); privatePresenterpresenter; publicView(){ //initiallayoutandpresenter initialLayout(); presenter=newPresenter(this); } protectedvoidinitialLayout(){ CustomLayoutlayout=newCustomLayout("view"); layout.addComponent(display,"display"); layout.addComponent(name,"name"); button.addClickListener(newClickListener(){ @Override publicvoidbuttonClick(ClickEventevent){ //whenbuttonclickthendispatchtopresenter presenter.changeDisplay(); } }); layout.addComponent(button,"submit"); setCompositionRoot(layout); } //viewprovidegetters&setters
  27. 27. PRESENTER publicclassPresenter{ privateViewview; privateModelmodel; publicPresenter(Viewview){ this.view=view; this.model=newModel(); } publicvoidchangeDisplay(){ Stringname=view.getInput().getValue(); //dosomelogic,eg:validate if(name.isEmpty()){ name="Guest"; } //callmodel model.setName(name); //controlview view.setDisplay("Hello,"+model.getName()); } }
  28. 28. MVVM DEMO WITH ZK
  29. 29. MODEL-VIEW-VIEWMODEL
  30. 30. MODEL publicclassModel{ privateStringname; publicvoidsetName(Stringname){ //businesslogic this.name=name; } publicStringgetName(){ returnname; } }
  31. 31. VIEW <zk> <windowtitle="MVVMExample"border="normal"width="100%" apply="org.zkoss.bind.BindComposer" viewModel="@id('vm')@init('tw.jcconf.example.mvvm.vm.ViewModel')"> <hbox> Hello,<labelvalue="@load(vm.model.name)"/> </hbox> <hbox> <labelvalue="Mynameis"/><textboxvalue="@bind(vm.name)"/> <buttononClick="@command('changeName')"label="Submit"/> </hbox> </window> </zk>
  32. 32. VIEWMODEL publicclassViewModel{ privateStringname; privateModelmodel; publicViewModel(){ model=newModel(); model.setName(name="Guest"); } //eventhandler @Command @NotifyChange("model") publicvoidchangeName()throwsException{ model.setName(name); } //ViewModelprovidergetter&setter publicvoidsetName(Stringname){ this.name=name; } publicStringgetName(){ returnname; } publicModelgetModel(){ returnmodel; } }
  33. 33. RESTFUL PATTERN
  34. 34. An architectural style thatabstracts the architectural elements within adistributed hypermediasystem. –Wikipedia
  35. 35. THE MEAS IS Use HTTPmethods explicitly. Be stateless. Expose directory structure-like URIs. Transfer XML, JavaScriptObjectNotation (JSON), or both. –IBM's developerWorks website
  36. 36. WHAT IS REST ?
  37. 37. HTTP ITSELF IS REST IMELEMENTATION
  38. 38. HOW TO USE ? User Action HTTP Method Create POST/PUT Read (Retrieve) GET Update (Modify) PUT/PATCH Delete (Destroy) DELETE
  39. 39. LOOK ALIKE User Action SQL Create INSERT Read (Retrieve) SELECT Update (Modify) UPDATE Delete (Destroy) DELETE
  40. 40. FOLLOW HTTP TO DESIGN RESTFUL WEB SERVICE Nouns Define the URL for your resources Verbs Choose the rightHTTP method ContentTypes Choose content-type which you wantto return
  41. 41.   «   ‹   ›   »
  42. 42. CREATE POSThttp://domain/books { "isbn":"123456789", "title":"ModernDesignPattern", "author":"StevenWang", "publisher":"JCConfTW", "year":"2014" } HTTP/1.1 200 | 401 { "id":"9876543" }
  43. 43. READ GEThttp://domain/books/123456789 or GEThttp://domain/books?isbn=123456789 HTTP/1.1 200 | 404 [{ "id":"9876543", "isbn":"123456789", "title":"ModernDesignPattern", "author":"StevenWang", "publisher":"JCConfTW", "year":"2014" }]
  44. 44. UPDATE PUThttp://domain/books/9876543 or PUThttp://domain/books { ["id":"9876543",] "isbn":"123456789",, "title":"ModernDesignPattern", "author":"StevenWang", "publisher":"JCConfTW", "year":"2014" } HTTP/1.1 200 | 401 |404
  45. 45. DELETE DELETE http://domain/books/9876543 or DELETE http://domain/books?isbn=9876543 HTTP/1.1 200 | 401 |404
  46. 46. RESTFUL DEMO WITH SPRING
  47. 47. @RestController @RequestMapping("/books") publicclassBooksResource{ @Autowired BookServiceservice; @RequestMapping(consumes="application/json",produces="application/json",method= @ResponseBody publicList<Book>getAll(){ returnservice.getAll(); } @RequestMapping(value="/{isbn}",consumes="application/json",produces="applicat @ResponseBody publicBookgetByIsbn(@PathVariable("isbn")Stringisbn){ returnservice.getBookByIsbn(isbn); } @RequestMapping(consumes="application/json",produces="application/json",method= @ResponseBody publicBooksave(@RequestBodyBookbook){ returnservice.save(book); } @RequestMapping(consumes="application/json",produces="application/json",method= @ResponseBody publicBookupdate(@RequestBodyBookbook){ returnservice.update(book); } @RequestMapping(value="/{id}",consumes="application/json",produces="applicatio publicvoiddelete(@PathVariable("id")Longid){
  48. 48. CQRS PATTERN
  49. 49. WHAT IS CQRS ?
  50. 50. CQRS is simply the creation of two objects where there was previously only one. The separation occurs based upon whether the methods are a command or aquery (the same definition thatis used by Meyer in Command and Query Separation, acommand is any method that mutates state and aquery is any method that returns avalue). –Wikipedia
  51. 51. Saywhat?
  52. 52. PUT ANOTHER WAY... Command/QueryResponsibilitySegregation (CQRS) is the idea thatyou can use adifferentmodelto update information than the modelyou use to read information. IN THIS CONTEXT, Commands = Writes Queries = Reads
  53. 53. OK, BUT WHY ?
  54. 54. WHAT IS CQRS NEED ?
  55. 55. MULTI-USER SYSTEM
  56. 56. TYPICAL APPLICATION OF THE CQRS PATTERN
  57. 57. COMMANDS AND EVENTS IN THE CQRS PATTERN
  58. 58. WHEN TO AVOID CQRS ? So, when should you avoid CQRS? The answer is mostof the time... –UdiDahan
  59. 59. OK, SOME SUGGESTIONS Large team. Difficultbusiness logic. Scalabilitymatters. Your top people can work on domain logic.
  60. 60. FINALLY
  61. 61. PATTERNS DON’T SOLVE PROBLEMS, THEY HELP US

×