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
ADownload the start code, unzip it and change the folder and solution from LastName to your last name.
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.
AAdd a folder to the HouseScenario project named Interfaces.
BAdd 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.
CStrip off 'Interfaces' from the namespace and make the interface public.
DDefine 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.
EIn 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.
AIn the House class, remove both of the list fields.
BDefine and instantiate a new list of type IWashable named washables.
CChange all the references to adding to the windows or doors list to the washables list.
DIn the Wash method in the House class, remove the second foreach loop.
EModify the first foreach loop to be types of IWashable in this.washables.
FCheck Your Work:
1Attempt 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.
2Set 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
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.
AIn the IWashable interface, add an Open, Close and Unlock method.
BGo to the Door class. The Door class does not fully implement all the IWashable members.
Snip: Does Not Implement Error
CTo implement all members automatically, hover over the IWashable interface, select Quick Actions and select 'Implement interface'.
Snip: Implement Interface
DRemove all the generated exceptions from the method bodies.
EHave the GlassWindow class implement IWashable as well.
FIn the Wash method in the House class, call Unlock, Open, Wash and Close on the IWashable object.
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.
ADefine a new class called Opening.
BHave 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.
CRemove 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.
DRemove 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.
EAdd an IOpenable interface
1Add an interface named IOpenable to the Interfaces folder.
2Make the interface public and strip the Interfaces from the namespace.
3Add an Open and a Close method to the interface.
4Have the Door and GlassWindow classes implement IOpenable.
Snip: Implement IOpenable
FAdd an ILockable interface
1Add an interface named ILockable to the Interfaces folder.
2Make the interface public and strip the Interfaces from the namespace.
3Add a Lock and Unlock method to the interface.
4Have Door and GlassWindow implement ILockable.
GAdd a Pickle Jar
1Add a new class to the project called PickleJar.
2Have 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.
AWhen 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
BIn 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.
CAdd a new Opening to the washables list.
DRun 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
EStop the application. Surround the cast and call in logic that checks if the IWashable object is ILockable.
Snip: Type Check
FRun 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.
GShorten the casting syntax as:
Snip: Shortened Type Check
HType 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.
IAdd a PickleJar to the list of washables.
JRun 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.
ARemove the line of code that adds the Opening object to the list.
BRemove Opening implementing IWashable.
CAdd auto-properties for Width and Height to the Opening class that are of type double.
DRemove the Wash methods from the Door and GlassWindow classes.
This will cause an error about the classes not implementing IWashable.
EIn the Door class, before the first interface, inherit from the Opening class.
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.
FInherit 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.
ACreate 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:
AExport a PNG file of the class diagram from Violet into the Visual Studio solution folder.
BEnsure that the application has no compiler errors or warnings.
CRemove all bin and obj folders from the entire solution.
Completion review
10.Review the feature acceptance criteria
AConfirm 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.