Object Oriented Analysis and Design Model View Controller Cheat Sheet PDF

Title Object Oriented Analysis and Design Model View Controller Cheat Sheet
Course OO Analysis and Design
Institution Cork Institute of Technology
Pages 8
File Size 305.9 KB
File Type PDF
Total Downloads 34
Total Views 126

Summary

Model View Control cheat sheet...


Description

Describe the pattern. MVC Pat t er ns t andsf orMode l Vi e wCo nt r o l l erPat t e r n.Thi spat t er ni sus edt o e par at esappl i c at i o nl o gi cf r o mt her es tof s epar at eappl i c at i on' sc o nc e r ns .MVCs t heus e ri nt e r f ac e.Todot hi si ts e par at est heappl i c at i oni nt ot hr eepar t s :t he mo de l ,t hevi e w,andt hec o nt r o l l er . Themodelmanagesf undame nt albe havi o ur sanddat aoft heappl i c at i on.I tc an r es pondt or e que s t sf ori nf or mat i o n,r e s pondt oi ns t r uc t i onst oc hanget hes t at eof i t si nf or mat i on,ande ve nt ono t i f yobs e r ver si ne ve nt dr i ve ns ys t emswhe n i nf or mat i onc hanges .Thi sc o ul dbeadat abas e ,oranynumberofdat as t r uc t ur es o rs t or ages ys t ems .I ti st hedat aanddat amanagementoft heappl i c at i o n. Thevi ew r e pr es ent st hevi s ual i z at i onoft hedat at hatmode lc o nt ai ns .I tr ende r s dat af r o mt hemode li nt oaf or mt hati ss ui t abl ef o rt heus e ri nt e r f ac e . Thecont rol l err e c e i vesus eri nputandmake sc al l st omo de lobj ec t sandt he vi e wt ope r f or m appr o pr i at eac t i o ns . The s et hr eec o mponent swo r kt oge t hert oc r eat et het hr eebas i cc o mpone nt sof MVC. What problems does the pattern solve? Youc anc hangec o dei nas mal l ers ubs e tt hati smo r eorl es si s ol at e df r om t he l ar ger pi ec eo fc ode .Thi s al l o ws you t o add,modi f y or r e mo ve c ode mor e e ffic i ent l yandl o gi c al l y .I tal s ohe l psi nt es t i ng,ass i mi l arc o dei ss e c t i one dyou maybeabl et ohavebe t t erc o ver ageofyourt e s t sc as es .I tal s omi ni mi s est he c odeyoumus twr i t e.

Reverse engineer the code in each question into a class diagram.

Explain how the pattern is applied to the given code. Thec o dei ss pl i tupi nt o3mai ns ec t i ons . Fi r s ti smodel . j avawhi c hobvi o us l yr e pr e s ent st hemode li nt hepat t er n. Thi se xt endsobservabl ewhi c hi sanyobj ec twhos es t at emaybeofi nt er es tand i nwhom ano t herobj e c tmayr e gi s t erani nt er es t .Mo de li sas ubt ypeo fobs er ve r . I two r ksal ongs i deano t herc l as sobs erver,( whi c hi sus e di nVi e w)t ohandl e t heaut o mat i cno t i fic at i onf unc t i onoft heMVC ar c hi t e c t ur e .The ypr o vi det he mec hani s m bywhi c ht hevi e wc anbeno t i fie daut o mat i c al l yofanyc hange st o t hemode l . Mo de lhas2at t r i but es ,c o l orwhi c hi spubl i candi sani ns t anc eo fac ol orobj ec t , Andapr i vat eat t r i but ec al l edc o unt ,t hati ss e tt o10.The r ear e4ope r at i ons , s e t Changed( )( publ i c ) , no t i f yObs e r ver s ( )( publ i c ) , do So me t hi ng( )( As kedi nc l as s ,Pac kageac c o r di ngt oMar y) , andaddObs e r ver ( ). TheaddObs e r ve r ( )me t hodaddso t herc l as s e sasobs e r ver sal l o wi ngt he mt os e e t hec hange smadet ot heobs e r vabl eMode lc l as s .WhendoSome t hi ng( )i sus ed s e t Changed( )s e t st hei nt er nalflagt hati ndi c at e st hi sobs er vabl ehasc hanged s t at eandc hange sar es e ntt ot heobs er ver sus i ngt heno t i f yObs er ver s ( )me t hod. Fr om her ei tc anupdat et heVi e wc l as s . Ne xt ,wewi l ll ookatt hevi e wc l as s .Thevi e wc l as si mpl e ment sObs e r ver ,whi c h al l o wsi tt obeupdat edwhe nt heobs er vabl ec l as s( Mode l )make sac hange.Vi e w hasoneat t r i but e,c ol orwhi c hi sapac kage .The r ear et woo per at i ons ,bo t har e publ i c .The s ear epai nt ( )andupdat e( ) .Updat e( )i sc al l edwhenac hangehas o c c ur r edi nt hes t at eoft heobs er vabl e. Fi nal l y ,t her ei st heCont r o l l erc l as s .I ti ni t i al i z est hei ns t anc eoft heMode l ,and vi e wc l as s e sal ongwi t hano t heri ns t anc eofvi e wc al l edat t r i but e( ? ) .Thes ear eal l pac kage s .I tal s ohaso neoper at i on,t hemai nme t hodt hi si sus edt or unt hec ode . Thi si spubl i c . Thear r o wsar eas s oc i at i ons .Co nt r o l l erhadac c es st obo t hMode landVoi dbut t he ydono thaveac c es st oCont r ol l er .

  

+ denotes public attributes or operations - denotes private attributes or operations # denotes protected attributes or operations

~ is a package

MVC Pattern stands for Model-View-Controller Pattern. This pattern is used to separate application's concerns. 

Model - Model represents an object or JAVA POJO carrying data. It can also have logic to update controller if its data changes.



View - View represents the visualization of the data that model contains.



Controller - Controller acts on both model and view. It controls the data flow into model object and updates the view whenever data changes. It keeps view and model separate.

Implementation .

Step 1 Create Model. Student.java public class Student { private String rollNo; private String name;

public String getRollNo() { return rollNo; }

public void setRollNo(String rollNo) { this.rollNo = rollNo; }

public String getName() {

return name; }

public void setName(String name) { this.name = name; } }

Step 2 Create View. StudentView.java public class StudentView { public void printStudentDetails(String studentName, String studentRollNo){ System.out.println("Student: "); System.out.println("Name: " + studentName); System.out.println("Roll No: " + studentRollNo); } }

Step 3 Create Controller. StudentController.java public class StudentController { private Student model; private StudentView view;

public StudentController(Student model, StudentView view){ this.model = model; this.view = view; }

public void setStudentName(String name){ model.setName(name); }

public String getStudentName(){ return model.getName(); }

public void setStudentRollNo(String rollNo){ model.setRollNo(rollNo); }

public String getStudentRollNo(){ return model.getRollNo(); }

public void updateView(){ view.printStudentDetails(model.getName(), model.getRollNo()); } }

Step 4 Use the StudentController methods to demonstrate MVC design pattern usage. MVCPatternDemo.java public class MVCPatternDemo { public static void main(String[] args) {

//fetch student record based on his roll no from the database

Student model

= retriveStudentFromDatabase();

//Create a view : to write student details on console StudentView view = new StudentView();

StudentController controller = new StudentController(model, view);

controller.updateView();

//update model data controller.setStudentName("John");

controller.updateView(); }

private static Student retriveStudentFromDatabase(){ Student student = new Student(); student.setName("Robert"); student.setRollNo("10"); return student; } }

Step 5 Verify the output. Student: Name: Robert Roll No: 10 Student: Name: John Roll No: 10

MVC is a software architecture - the structure of the system - that separates domain/application/business (whatever you prefer) logic from the rest of the user interface. It does this by separating the application into three parts: the model, the view, and the controller. The model manages fundamental behaviors and data of the application. It can respond to requests for information, respond to instructions to change the state of its information, and even to notify observers in event-driven systems when information changes. This could be a database, or any number of data structures or storage systems. In short, it is the data and data-management of the application. The view effectively provides the user interface element of the application. It'll render data from the model into a form that is suitable for the user interface. The controller receives user input and makes calls to model objects and the view to perform appropriate actions. All in all, these three components work together to create the three basic components of MVC....


Similar Free PDFs