jcconf tw 2014 - modern design pattern

67
MODERN DESIGN PATTERN

Upload: steven-wang

Post on 08-Jul-2015

478 views

Category:

Presentations & Public Speaking


0 download

DESCRIPTION

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 中被廣泛的使用。在大型、高複雜度的專案中及對效能有較高要求的系統可以明顯體會到其價值

TRANSCRIPT

Page 1: JCConf TW 2014 - Modern Design Pattern

JCConf Taiwan 2014

MODERNDESIGN PATTERN

Page 2: JCConf TW 2014 - Modern Design Pattern

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

If you want to be a cool guy.

JOIN SOFTLEADER.

Page 4: JCConf TW 2014 - Modern Design Pattern

WHAT IS DESIGN PATTERN?

Page 5: JCConf TW 2014 - Modern Design Pattern

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

THE ONE CONSTANT IN SOFTWARE DEVELOPMENT:

CHANGE!

Page 7: JCConf TW 2014 - Modern Design Pattern

TIGHT COUPLING!

Page 8: JCConf TW 2014 - Modern Design Pattern

IT MIGHT GET YOU INTO TROUBLE

Page 9: JCConf TW 2014 - Modern Design Pattern

BEWARE OF

Page 10: JCConf TW 2014 - Modern Design Pattern

GOF: 23

Page 11: JCConf TW 2014 - Modern Design Pattern
Page 12: JCConf TW 2014 - Modern Design Pattern

MVC PATTERN

Page 13: JCConf TW 2014 - Modern Design Pattern

A PROBLEM OR A SOLUTION ?Commonly reasons:

Reduced Code complexityCode reuseIncreased flexibilityDecoupled code

Page 14: JCConf TW 2014 - Modern Design Pattern

COOL, SOUNDS NICE, BUTIs it true ?Do all other patterns lack these cool things ?

Page 15: JCConf TW 2014 - Modern Design Pattern

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

MVC COMPONENTS

Page 17: JCConf TW 2014 - Modern Design Pattern

HOW MVC WORKS

Page 18: JCConf TW 2014 - Modern Design Pattern
Page 19: JCConf TW 2014 - Modern Design Pattern
Page 20: JCConf TW 2014 - Modern Design Pattern
Page 21: JCConf TW 2014 - Modern Design Pattern

POOR MVC GIVE BIRTH TO

Page 22: JCConf TW 2014 - Modern Design Pattern

MVC DEMO WITH SERVLET

Page 23: JCConf TW 2014 - Modern Design Pattern

MODEL-VIEW-CONTROLLER

Page 24: JCConf TW 2014 - Modern Design Pattern

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

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

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

MODEL-VIEW-PRESENTER

Page 29: JCConf TW 2014 - Modern Design Pattern

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

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

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

MODEL-VIEW-VIEWMODEL

Page 34: JCConf TW 2014 - Modern Design Pattern

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

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

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

RESTFUL PATTERN

Page 38: JCConf TW 2014 - Modern Design Pattern

An architectural style that abstracts thearchitectural elements within a distributed

hypermedia system.

–Wikipedia

Page 39: JCConf TW 2014 - Modern Design Pattern

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

WHAT IS REST ?

Page 41: JCConf TW 2014 - Modern Design Pattern

HTTP ITSELF IS REST IMELEMENTATION

Page 42: JCConf TW 2014 - Modern Design Pattern

HOW TO USE ?User Action HTTP MethodCreate POST/PUTRead (Retrieve) GETUpdate (Modify) PUT/PATCHDelete (Destroy) DELETE

Page 43: JCConf TW 2014 - Modern Design Pattern

LOOK ALIKEUser Action SQLCreate INSERTRead (Retrieve) SELECTUpdate (Modify) UPDATEDelete (Destroy) DELETE

Page 44: JCConf TW 2014 - Modern Design Pattern

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

 «   ‹   ›   »

Page 46: JCConf TW 2014 - Modern Design Pattern

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

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

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

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

@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

CQRS PATTERN

Page 53: JCConf TW 2014 - Modern Design Pattern

WHAT IS CQRS ?

Page 54: JCConf TW 2014 - Modern Design Pattern

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

Say what?

Page 56: JCConf TW 2014 - Modern Design Pattern

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

OK, BUT WHY ?

Page 58: JCConf TW 2014 - Modern Design Pattern

WHAT IS CQRS NEED ?

Page 59: JCConf TW 2014 - Modern Design Pattern
Page 60: JCConf TW 2014 - Modern Design Pattern

MULTI-USER SYSTEM

Page 61: JCConf TW 2014 - Modern Design Pattern

TYPICAL APPLICATION OF THE CQRS PATTERN

Page 62: JCConf TW 2014 - Modern Design Pattern

COMMANDS AND EVENTS IN THE CQRS PATTERN

Page 63: JCConf TW 2014 - Modern Design Pattern

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

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

FINALLY

Page 66: JCConf TW 2014 - Modern Design Pattern

PATTERNS DON’T SOLVE PROBLEMS,THEY HELP US

Page 67: JCConf TW 2014 - Modern Design Pattern