4.4 House Learning Activity

Overview

4.4 House Learning Activity

In this learning activity you will start with a basic structure of classes and build onto them using interfaces and inheritance to write a method that will wash all the things in the house depending on what type of thing it is.

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.

Getting Started
1.Getting Started
A Download the start code, unzip it and change the folder and solution from LastName to your last name.

House 4.4 LastName.zip

House 4.4 LastName.zip
B Review the starting structure.
Class Diagram: House; Members and relationships are shown in the editable reference; unrelated members may be omitted.
Class Diagram: House
  • New
  • Changed
  • Removed
C If you want, you can follow along with the instructor-led videos.

House Lab Walkthrough, Part 1 [18:21]

House Lab Walkthrough, Part 1 [18:21]

House Lab Walkthrough, Part 2 [18:57]

House Lab Walkthrough, Part 2 [18:57]

For the first video, code for the end solution does not begin until minute 11. The content prior explains how different objects with similar functionality can produce unwieldy code and how treating classes as the base object class is dangerous.

Have the GlassWindow and Door implement an interface.
Acceptance Criteria
Acceptance criteria
  • The completed implementation satisfies this requirement: Add a folder to the HouseScenario project named Interfaces.
  • The completed implementation satisfies this requirement: Add an interface to the Interfaces folder by right clicking the folder, selecting New Item..., selecting Interface, naming it IWashable and pressing okay.
  • The completed implementation satisfies this requirement: Define a method in the interface named Wash with a void return.
Instructions
2.Have the GlassWindow and Door implement an interface.
A Add a folder to the HouseScenario project named Interfaces.
B Add an interface to the Interfaces folder by right clicking the folder, selecting New Item..., selecting Interface, naming…

Add an interface to the Interfaces folder by right clicking the folder, selecting New Item..., selecting Interface, naming it IWashable and pressing okay.

C Strip off 'Interfaces' from the namespace and make the interface public.
D Define a method in the interface named Wash with a void return.
void Wash();

Members of interfaces cannot have a visibility nor can they define a body.

E In the Door and GlassWindow class, implement the IWashable interface.
Reduce two lists down to one.
Acceptance Criteria
Acceptance criteria
  • Attempt to add a string to the washables list. Ensure that you cannot.
  • Set a breakpoint at the end of the House constructor and run the application. Ensure that you have a list of 7 IWashables that are two different types (GlassWindow and Door).
  • The completed implementation satisfies this requirement: Define and instantiate a new list of type IWashable named washables.
Instructions
3.Reduce two lists down to one.
A In the House class, remove both of the list fields.
B Define and instantiate a new list of type IWashable named washables.
C Change all the references to adding to the windows or doors list to the washables list.
D In the Wash method in the House class, remove the second foreach loop.
E Modify the first foreach loop to be types of IWashable in this.washables.
F Check Your Work:
1 Attempt to add a string to the washables list. Ensure that you cannot.

This is called type safety. Before, when there was a list of type Object, you could throw anything into that list. Now, you can only put things that are IWashables into that list and because we have made sure of that we can also be sure that all objects in the list have the Wash method.

2 Set a breakpoint at the end of the House constructor and run the application. Ensure that you have a list of 7 IWashables…

Set a breakpoint at the end of the House constructor and run the application. Ensure that you have a list of 7 IWashables that are two different types (GlassWindow and Door).

Snip: IWashables List for 4.4 House Learning Activity (NEW). Use the adjacent task instructions to identify the required controls, output, or debugger values.
Snip: IWashables List
Add to the functionality of IWashable.
Acceptance Criteria
Acceptance criteria
  • The completed implementation satisfies this requirement: Remove all the generated exceptions from the method bodies.
  • The completed implementation satisfies this requirement: Have the GlassWindow class implement IWashable as well.
Instructions
4.Add to the functionality of IWashable.
A In the IWashable interface, add an Open, Close and Unlock method.
B Go to the Door class. The Door class does not fully implement all the IWashable members.
Snip: Does Not Implement Error for 4.4 House Learning Activity (NEW). Use the adjacent task instructions to identify the required controls, output, or debugger values.
Snip: Does Not Implement Error
C To implement all members automatically, hover over the IWashable interface, select Quick Actions and select 'Implement interface'.
Snip: Implement Interface for 4.4 House Learning Activity (NEW). Use the adjacent task instructions to identify the required controls, output, or debugger values.
Snip: Implement Interface
D Remove all the generated exceptions from the method bodies.
E Have the GlassWindow class implement IWashable as well.
F In the Wash method in the House class, call Unlock, Open, Wash and Close on the IWashable object.
Snip: Wash Method for 4.4 House Learning Activity (NEW). Use the adjacent task instructions to identify the required controls, output, or debugger values.
Snip: Wash Method
"Segregate" the interfaces.
Acceptance Criteria
Acceptance criteria
  • The completed implementation satisfies this requirement: Define a new class called Opening.
  • The completed implementation satisfies this requirement: Have Opening implement IWashable and remove all the thrown exceptions.
  • The completed implementation satisfies this requirement: Remove Open(), Close(), and Unlock() from the IWashable interface.
Instructions
5."Segregate" the interfaces.
A Define a new class called Opening.
B Have Opening implement IWashable and remove all the thrown exceptions.

There's an issue here. An "opening" can neither Open, Close nor Unlock, but IWashable says that it has to do those things. This is a violation of the Interface Segregation Principle that states that a class should not be forced to implement members not related to its functionality. Making interfaces this large are called Fat Interfaces.

The interface is also in violation of the Single Responsibility Principle that states that functionality of a class and/or interface should not have multiple functionalities. In this case the IWashable interface has methods like Open and Close and Unlock that have nothing to do with Wash.

C Remove Open(), Close(), and Unlock() from the IWashable interface.

This will cause errors in the Wash method that we will fix later. For now, comment out the errors.

D Remove the Open, Close and Unlock methods from the Opening class.

Now the Opening class doesn't have to implement an Open, Close or Unlock method because the IWashable interface doesn't require it.

E Add an IOpenable interface
1 Add an interface named IOpenable to the Interfaces folder.
2 Make the interface public and strip the Interfaces from the namespace.
3 Add an Open and a Close method to the interface.
4 Have the Door and GlassWindow classes implement IOpenable.
Snip: Implement IOpenable for 4.4 House Learning Activity (NEW). Use the adjacent task instructions to identify the required controls, output, or debugger values.
Snip: Implement IOpenable
F Add an ILockable interface
1 Add an interface named ILockable to the Interfaces folder.
2 Make the interface public and strip the Interfaces from the namespace.
3 Add a Lock and Unlock method to the interface.
4 Have Door and GlassWindow implement ILockable.
G Add a Pickle Jar
1 Add a new class to the project called PickleJar.
2 Have the PickleJar class implement IWashable and IOpenable.

Now that our interfaces are segregated, implementing interfaces in a class becomes much easier because we don't have to worry about implementing additional methods. In this case, we don't implement ILockable so we don't have to have a Lock or Unlock method that a pickle jar doesn't need.

"Polymorphize" and type check the washables.
Acceptance Criteria
Acceptance criteria
  • The completed implementation satisfies this requirement: Add a new Opening to the washables list.
  • The completed implementation satisfies this requirement: Add a PickleJar to the list of washables.
Instructions
6."Polymorphize" and type check the washables.
A When calling Unlock, first cast the IWashable object as an ILockable.

This makes the compiler happy because it now knows that the Unlock method is being called on an ILockable object.

Snip: Cast as ILockable for 4.4 House Learning Activity (NEW). Use the adjacent task instructions to identify the required controls, output, or debugger values.
Snip: Cast as ILockable
B In the Wash method in the House class, add a call to Lock after the call to Close and comment out all calls except for the…

In the Wash method in the House class, add a call to Lock after the call to Close and comment out all calls except for the Unlock call.

The errors here are because the IWashable object only has the Wash method. But for every "thing" that is washable we want to be able to treat it a special way. For example, openings are still washable they just can't be opened or closed, but we want to open and close windows and doors, etc.

C Add a new Opening to the washables list.
D Run the application and click the Wash button.

This causes a runtime error. When trying to cast the Opening as an ILockable it returns null because an Opening type cannot be "changed" to be an ILockable object.

Snip: Null ILockable for 4.4 House Learning Activity (NEW). Use the adjacent task instructions to identify the required controls, output, or debugger values.
Snip: Null ILockable
E Stop the application. Surround the cast and call in logic that checks if the IWashable object is ILockable.
Snip: Type Check for 4.4 House Learning Activity (NEW). Use the adjacent task instructions to identify the required controls, output, or debugger values.
Snip: Type Check
F Run the application and click the Wash button. There should no longer be a runtime error because only objects that are…

Run the application and click the Wash button. There should no longer be a runtime error because only objects that are lockable are getting unlocked.

G Shorten the casting syntax as:
Snip: Shortened Type Check for 4.4 House Learning Activity (NEW). Use the adjacent task instructions to identify the required controls, output, or debugger values.
Snip: Shortened Type Check
H Type check and cast the rest of the commented out calls.

The call to Wash doesn't need a type check or cast because everything in the list is already an IWashable.

I Add a PickleJar to the list of washables.
J Run the application and click the Wash button and make sure there are no exceptions thrown.
Use inheritance over interfaces
Acceptance Criteria
Acceptance criteria
  • The completed implementation satisfies this requirement: Remove the line of code that adds the Opening object to the list.
  • The completed implementation satisfies this requirement: Remove Opening implementing IWashable.
  • The completed implementation satisfies this requirement: Add auto-properties for Width and Height to the Opening class that are of type double.
Instructions
7.Use inheritance over interfaces

We have a Door, GlassWindow and Opening class that all have a Wash method and implement IWashable. In reality, both a door and a window are both "openings". Let's assume that the Door, GlassWindow and Opening Wash methods are the same. Instead of all of them having their own Wash methods, we can use inheritance to only have one.

A Remove the line of code that adds the Opening object to the list.
B Remove Opening implementing IWashable.
C Add auto-properties for Width and Height to the Opening class that are of type double.
D Remove the Wash methods from the Door and GlassWindow classes.

This will cause an error about the classes not implementing IWashable.

E In the Door class, before the first interface, inherit from the Opening class.
Snip: Inherit Opening for 4.4 House Learning Activity (NEW). Use the adjacent task instructions to identify the required controls, output, or debugger values.
Snip: Inherit Opening

The error now goes away because Door inherits from a class that has a Wash method (even though that class isn't an IWashable).

When inheriting AND implementing interfaces the inheritance always comes first.

F Inherit from Opening in the GlassWindow class.
Create a class diagram of the ending solution in Violet.
Acceptance Criteria
Acceptance criteria
  • The Violet class diagram represents the ending classes, inheritance, interfaces, and associations.
  • The submitted diagram and solution agree about the washable objects and their contracts.
Instructions
8.Create a class diagram of the ending solution in Violet.
A Create a class diagram of the ending solution in Violet.
Submit a zipped Visual Studio solution and class diagram after completing the following:
9.Submit a zipped Visual Studio solution and class diagram after completing the following:
A Export a PNG file of the class diagram from Violet into the Visual Studio solution folder.
B Ensure that the application has no compiler errors or warnings.
C Remove all bin and obj folders from the entire solution.
Completion review
10.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.

Have the GlassWindow and Door implement an interface. Reduce two lists down to one. Add to the functionality of IWashable. "Segregate" the interfaces. "Polymorphize" and type check the washables. Use inheritance over interfaces Create a class diagram of the ending solution in Violet.