8.3 Proxy Pattern Lab

Overview

8.3 Proxy Pattern Lab

This assignment is based on Head First Design p.429-491

Work through the stories in order. Use the acceptance criteria to check each feature, and complete the nested tasks using the specified names, values, and scenarios.

Inspect the existing DMV client
1.Set up start code.
A Download and extract the start code.

8.3 Proxy LastName.zip

8.3 Proxy LastName.zip
B Test the application.
1 F12 on the DMV object. The DMV object is from a dll called ObjectvilleDMV. Note that the DMV object implements an IDMV interface.
2 Put a breakpoint on the line that calls CheckAge and start the application.
3 Expand the dmv object. Note that the applicants list is a large list with 1000 values.

We have to instantiate the DMV object if we want to do any work on it and when we do it gets a large amount of data that may slow our application down. We don't want to get that large list of applicants until it's absolutely necessary.

4 Press continue to see what functionality the dmv object provides.
Delay expensive DMV creation behind a proxy
Acceptance Criteria
Acceptance criteria
  • DMVProxy implements IDMV and CheckAge returns whether age is at least 16.
  • FillOutPaperWork prints the required at-home message without creating the real DMV.
  • realDMV remains null before the first GetApplicants call; that call initializes it once and returns its list.
  • AddApplicant uses the proxy’s GetApplicants result, and the client uses the proxy through the same contract.
Instructions
2.Set up proxy pattern.
A Add a new class to the project named DMVProxy.
B Have the class implement IDMV.

You will need to add a using statement.

C Define a private field of type DMV named realDMV.
D In CheckAge, check if the age parameter is greater than or equal to 16 and return the result.
E In FillOutPaperWork, Console WriteLine "Filling out paperwork from home."
F In GetApplicants, check whether realDMV is null. If it is null, instantiate it once. Then call the realDMV's GetApplicants…

In GetApplicants, check whether realDMV is null. If it is null, instantiate it once. Then call the realDMV's GetApplicants method outside of the if statement and return the results.

G In AddApplicant, call the proxy's GetApplicants method and call Add, passing in a.
3.Use the proxy instead of the real object.
A Change the dmv object type from DMV to DMVProxy in the MainWindow.

There should be no compiler errors at this point. Since both DMV and DMVProxy both implement IDMV, the objects should be interchangeable.

B Rename dmv to dmvProxy.
C Check Your Work:
1 Set a breakpoint on the Console.WriteLine() in the Main method and run the application.
2 Expand the dmvProxy object in the Autos window. The realDMV object should be null.

This is the Proxy Pattern at work; before we had a "large" DMV object that we needed to instantiate first to use, but now we don't need it right away because our proxy is doing the simple tasks for us without having to ever use the real DMV.

3 F11 into GetApplicants. Since the realDMV object is null it should instantiate it and then return the list.

We didn't need to use the DMV explicitly so we didn't need to instantiate until now (lazy instantiation).

Completion review
4.Review the feature acceptance criteria
A Confirm the required results

Return to each story and verify its acceptance criteria. Preserve the submission format and destination stated in the activity; a reading, setup guide, lab, video demonstration, and oral final may require different evidence.

Delay expensive DMV creation behind a proxy