1. Introduction to MVC for Desktop Application Course C1001 He Shiming2010-9 Part 1 - Low-coupling Application Design and Application Architecture 射手科技(SPlayer.org)
2. Seminar Covers… Low-coupling application design and application architecture MVC in web application MVC in desktop application MVC in Windows desktop application with cross-platform consideration Designing applications using MVC Recommended readings 射手科技(SPlayer.org)
3. Low-coupling Application Design and Application Architecture solving the problem of coupling in hardware and software products 射手科技(SPlayer.org)
4. The Problem of Coupling Inappropriate design creates closely-coupled components Closely-coupled components are not suitable for independent development Closely-coupled components establish complicated dependency tree Components with complicated dependency tree are hard to test Difficulty in testing means difficulty in quality assurance Difficulty in quality assurance leads to unstable products 射手科技(SPlayer.org)
5. Writing Software Products Today’s software is easy to writeIt’s possible to start quickly without learning the underlying details Today’s books aren’t necessarily written by wise peopleBooks are often misleading, they are written to explain one problem but not all of them “Quickness” is today’s primary focusWhich makes it impossible to think of alternative ways, when the current one is feasible It’s difficult for non-technicians to tell whether a product is technically goodTherefore, management won’t make architectural decisions based on non-visible data 射手科技(SPlayer.org)
7. An Example, Writing a Notepad Application A frame window, that handles menu commands An edit control, that resides within the client area of the frame window Something to handle text file read and write 射手科技(SPlayer.org)
8. An Example, Writing a Notepad Application Naturally, we have:class CMainFrame: public CFrameWindowImpl<CMainFrame>{public:CEditm_edit; BEGIN_MSG_MAP(CMainFrame) MESSAGE_HANDLER(WM_CREATE, OnCreate)MESSAGE_HANDLER(WM_SIZE, OnSize) COMMAND_ID_HANDLER(ID_FILE_NEW, OnFileNew) COMMAND_ID_HANDLER(ID_FILE_OPEN, OnFileOpen) COMMAND_ID_HANDLER(ID_FILE_SAVE, OnFileSave)END_MSG_MAP()... 射手科技(SPlayer.org)
9. An Example, Writing a Notepad Application So, during WM_CREATE, we would like to:LRESULT CMainFrame::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled){m_edit.CreateWindow(m_hWnd, WS_CHILD|WS_VISIBLE|... And there are times in WM_SIZE, we would like to align our views togetherLRESULT CMainFrame::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled){ RECT rc_client;GetClientRect(&rc_client);m_edit.SetWindowPos(NULL, &rc_client... 射手科技(SPlayer.org)
10. An Example, Writing a Notepad Application When a file should be opened:LRESULT CMainFrame::OnFileOpen(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled){CFileDialogfd(TRUE, NULL, NULL, OFN_EXPLORER, L”*.txt”, m_hWnd); if (!fd.DoModal(m_hWnd)) return 0; FILE* file; if (_wfopen_s(&file, fd.m_szFileName, L”rb”) == 0) { // fread logic // produce a wchar_t* string (content)m_view.SetWindowText(content);... 射手科技(SPlayer.org)
11. An Example, Writing a Notepad Application Then we realized that file operation can be encapsulated into a single class:LRESULT CMainFrame::OnFileOpen(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled){CTextFile file;file.OpenFile(m_hWnd, &m_view);... 射手科技(SPlayer.org)
12. An Example, Writing a Notepad Application Then we discovered, opening large files maybe slow, we don’t want the UI to be blocked, and we even would like to display progress, so we implement this in a thread:CTextFileCMainFrame::m_file;LRESULT CMainFrame::OnFileOpen(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled){m_file.OpenFileThreaded(m_hWnd, &m_view);... 射手科技(SPlayer.org)
13. An Example, Writing a Notepad Application We encountered a problem, there are garbage characters in the edit control, we don’t know the cause, so we start a single-step debug process, and set a breakpoint:m_file.OpenFileThreaded(m_hWnd, &m_view);... We have no idea whether the file is physically wrong, threading problem, fread process is flawed, or null-termination issue in SetWindowText 射手科技(SPlayer.org)
14. An Example, Writing a Notepad Application Now imagine, we would like to:Support all kinds of Unicode BOMSupport tabbing, editing multiple filesSupport syntax highlightetc… 射手科技(SPlayer.org)
15. An Example, Writing a Notepad Application Naturally, we thought of:Enhancing CTextFile to handle many other thingsEnhancing CMainFrame by providing multiple instances of CEditEnhancing CEdit by subclassing the control, it’ll accept regular text through SetWindowText,then renders highlight automatically 射手科技(SPlayer.org)
16. An Example, Writing a Notepad Application Did you notice:Impossible for parallel developmentImpossible to identify bugsDevelopment tends to make project bloatedImpossible for codebase to support new projectsCannot be properly designed 射手科技(SPlayer.org)
17. Application Architecture is: A standard for development, component/class definition To enable parallel development, by isolating logics and components in different domain To simplify quality assurance and code defect targeting To simplify design and implementation process To create reusable codebase 射手科技(SPlayer.org)
18. Macro Application Architecture Main Views Reduce connections of components Reduce interaction of components Isolate components for independent development COUPLING 射手科技(SPlayer.org)
19. And Model-View-Controller is: A type of macro architecture A pattern/standard to follow during software development An approach to isolate presentation (UI) from application logic, to enable independent development An approach to ensure code reusability (a.k.a. keep code DRY – don’t-repeat-yourself) 射手科技(SPlayer.org)
The ideal testing method should reach 100% code coverage, meaning each line of the project source code is tested by this method. As dependency tree gets complicated, it becomes impossible to reach high code coverage
macro architecture versus micro architecture is like steel infrastructure of a tower versus floor layout and electricity details