- The completed implementation satisfies this requirement: Change the Name to
"SOLIDPrinciples". - The completed implementation satisfies this requirement: Change the Solution name to: 7.1 SOLID *YourLastName*.
7.1 SOLID Principles Lab
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.
"SOLIDPrinciples".Console App (.NET Core)Previously, the majority of projects you have worked on have been in .NET Framework. Here we are introducing you to another version of the C# language .NET Core, that has been made only fairly recently. Core is open source compared to Framework that is developed exclusively by Miscrosoft. Read the article below for more information.
.NET Core - Wikipedia
.NET Core - Wikipedia"SOLIDPrinciples".Console.WriteLine, call Console.Read().

This structure looks different from Framework in that this does not have a standalone exe file and has additional json files to help with configuration.
A file explorer window displaying the contents of the netcoreapp2.2 folder. In order top to bottom the following files are listed:
SOLIDPrinciples.deps.json
SOLIDPrinciples.dll
SOLIDPrinciples.pdb
SOLIDPrinciples.runetimeconfig.dev.json
SOLIDPrinciples.runtimeconfig.jsonOpenClosed and click OKClass1 class.OpenClosed and click OKClass1 class.OpenClosed project folder --> bin --> Debug --> netstandard2.0
This looks similar to the console app's debug folder without the json startup files because class libraries can't start up.
A File Explorer window displaying the contents of the netstandard2.0 folder. In order top to bottom, the following files are displayed:
OpenClosed.deps.json
OpenClosed.dll
OpenClosed.pdbSOLID Exercise.zip
OpenClosed and copy the Hero.csHero.cs class into the OpenClosed folder in your solution.OpenClosed project and select Add --> Existing Item...OpenClosed folder and select the Hero.csOpenClosed.SingleResponsibility folder --> SingeResponsibility.csprojC(3-4) for LiskovSubstitution, InterfaceSegregation and DependencyInversionSOLIDPrinciples project and select Add Reference..."Hello World" and replace it with "---------------- Single Responsibility Principle ----------------"Assignment exposes the required contract: name: string; points: int; Assignment(name: string, points: int); Name get.Student exposes the required contract: firstName: string; lastName: string; motivation: int; skill: int.We have a Student class with some functionality that relates to an assignment. But what if the formula for grading changes? Should we have to open up the Student code that really doesn't have anything to do with assignments other than use them to change functionality? No.
Assignment class based on the diagram below.It is easy to not want to create many classes for fear of cluttering up a solution/project, but it is always better than have many classes that do one thing than one large class that does many.
A UML Class diagram of the Assignment class is displayed:
Fields:
- name: string
- points: int
Methods and Properties:
+ Assignment(name: string, points: int)
+ Name <get>
+ Points <get>
+TurnIn(motivation: int, skill: int)TurnInAssignment method in the Student class and paste it into the Assignment class's TurnIn method.Student class based on the diagram below.A UML Class diagram of the Student class is displayed:
Fields:
- assignmentName: string (red with a line striking it out)
- assignmentPoints: int (red with a line striking it out)
-firstName: string
-lastName: string
-motivation: int
-skill: int
Methods and Properties:
+ Student(firstName: string, lastName: string, motivation: int, skill: int)
+ DoAssignment(assignmentName: string, assignmentPoints: int, assignment: Assignment) (the existing parameters assignmentName and Assignment Points are red and with a line striking them out. The new parameter as
+ GoToClass()
+ TakeBreak()
+ TurnInAssignment() (red with a line striking it out)DoAssignment method, fix the errors and call the TurnIn method after the WriteLine.SOLIDPrinciples project..."Hello World" and replace it with "---------------- Single Responsibility Principle ----------------"Student object with skill and motivation between 0 and 100.Assignment object.DoAssignment and pass in the Assignment object.
We have now broken the Student class out so that it no longer has anything to do with how an assignment functions. Both the Student and Assignment class only have ONE responsibility now.
A console window displaying the following text:
Line 1: Doing assignment "Final Project" worth 60 points.
Line 2: Received 54 out of 60.Battle method and pass in "Goblin".string to type Minion.Minion exposes the required contract: hp: int; attack: int; defense: int; HP: int get set.Goblin exposes the required contract: Goblin(hp: int, attack: int, defense: int); Battle(hero: Hero) override.Classes should be OPEN to extension and CLOSED to modification. This means that a class should not need to be messed with if you need to add new features or derive a subclass from it.
DoAssignment, print out "\n---------------- Open/Closed Principle ----------------"Hero, passing in 100, 50, 40 and 20 for arguments.Battle method and pass in "Goblin".What if we wanted to add more minions to the application? We would have to go in and change the Hero class to accommodate/modify that.
A UML Class diagram displaying the Minion <abstract> class with 4 subclasses inheriting from it: Goblin, Ogre, Zombie, and OwlBear.
Minion < Abstract >
Fields
# hp: int
# attack: int
# defense: int
Methods and Properties:
+ HP: int <get> <set>
+ Attack: int <get>
+ Defense: int <get>
+ Battle(Hero hero) <virtual>
+ ToString() <override>
Goblin (inheriting Minion)
No Fields
Methods and Properties
+ Goblin(hp: int, attack: int, defense: int)
+ Battle(Hero: hero)
Ogre (inheriting Minion)
No Fields
Methods and Properties
+ Ogre(hp: int, attack: int, defense: int)
+ Battle(Hero: hero)
Zombie(inheriting Minion)
No Fields
Methods and Properties
+ Zombie(hp: int, attack: int, defense: int)
+ Battle(Hero: hero)
OwlBear(inheriting Minion)
No Fields
Methods and Properties
+ OwlBear(hp: int, attack: int, defense: int)
+ Battle(Hero: hero)Minion class and the minions in a folder named MinionsMinion class's Battle method, paste the code below:hero.HP -= (int)Math.Round(this.attack - hero.Defense * .20);
this.hp -= (int)Math.Round(hero.Attack - this.defense * .10);Minion class's ToString method, paste the code below:return $"{this.GetType().Name} HP: {this.hp}";Goblin class's Battle method, call the base Battle method and the hero's HackNSlash method.Ogre class's Battle method, call the base Battle method and the hero's PutUpShield method.Owlbear class's Battle method, call the hero's Run method. Owlbears are too scary to fight.Zombie class's Battle method, call the base Battle method and the hero's PerformMagic method.Hero class...Change the minion field from type string to type Minion.
Change the Battle method to take a Minion type not a string type.
Change the references to the minion in the hero methods to minion.GetType().Name
Ensure the your battle method contains the following:
Assign the minion field
Print out a new line
Print out the hero's ToString
Print out the minion's ToString
Call the minion's Battle Method, passing in the hero object
Print out the hero's ToString
Print out the minion's ToString
Goblin, passing in 80, 40 and 10 as arguments and then call the hero's Battle method,…In the Main method, instantiate a Goblin, passing in 80, 40 and 10 as arguments and then call the hero's Battle method, passing in the Goblin object.

A console displaying the following text:
Line 1: Open/Closed Principle
Line 2: Blank
Line 3: Hero HP: 100
Line 4: Goblin HP: 80
Line 5: Blank
Line 6: Hero hacks n' slashes at the Goblin.
Line 7: Hero HP: 68
Line 8: Goblin HP: 31
Line 9: BlankGoblin object to a Zombie object and run the application. Your output should change to show a Zombie.Now that we modified the classes to that the Hero's Battle method changes depending on the object we no longer have to modify the Hero class if we need to add another minion. We could add a Dragon class and never have to touch the Hero class at all.
Along with our Hero we want a Sidekick. The Sidekick can do most of what the Hero can do with a little variation, but right now if we inherited from Hero the Sidekick would have to do everything exactly the same.
Hero class, make the PutUpShield and Run methods virtual.Sidekick class based on the diagram below:A class diagram showing the Sidekick class inheriting from the Hero class.
Hero
All the Fields, Methods and Properties of the existing Hero class are displayed.
Sidekick inheriting from Hero (in blue text)
No Fields
Methods and Properties
+ Sidekick(hp: int, attack: int, defense: int, magic: int) (in blue text)
+ PutUpShield() <override> (in blue text)
+ Run() <override> (in blue text)PutUpShield method, print out: "\nSidekick throws body in front of Hero".Run method, call the base Run behavior and then print out: "\nSidekick hides in a corner".Zombie object to an Ogre object.Ogre...Instantiate a new Sidekick, passing in 80, 30, 20 and 10.
Call the sidekick's Battle method and pass in the Ogre object.

A console window displaying the following text:
Line 1: Open/Closed Principle
Line 2: Blank
Line 3: Hero HP: 100
Line 4: Ogre HP: 80
Line 5: Blank
Line 6: Hero defends against the Ogre with their shield.
Line 7: Hero HP: 68
Line 8: Ogre HP: 31
Line 9: Blank
Line 10: Sidekick HP: 80
Line 11: Ogre HP: 31
Line 12: Blank
Line 13: Sidekick throws body in front of Hero.
Line 14: Sidekick HP: 44
Line 15: Ogre HP: 2FlyingBird named flyingBirds.Bird exposes the required contract: Eat() abstract.Penguin exposes the required contract: Eat() override.FlyingBird exposes the required contract: Fly() abstract.This principle states that subclasses should be able to be swapped without changing or breaking the application.
Ogre, paste the code below.Console.WriteLine("\n---------------- Liskov Substitution Principle ----------------");
Birdkeeper birdkeeper = new Birdkeeper();
birdkeeper.AddBird(new Hawk());
birdkeeper.AddBird(new Duck());
birdkeeper.AddBird(new Pigeon());
birdkeeper.AddBird(new Penguin());
birdkeeper.FeedBirds();
birdkeeper.BirdsFlyAround();Start the application. An exception will be thrown because Penguins can't fly! But they're in a list of things that should be able to fly.
Since not all Birds can fly, we need to remove that behavior from the Bird class. This violates the Liskov Substitution Principle. You aren't able to treat Penguins and Hawks the same way because they don't function in similar ways in all their behaviors.
FlyingBird class in the Birds folder.A UML class diagram is displayed. The new FlyingBird <abstract> class is inheriting from the Bird <abstract> class. The + Fly() <abstract> methods in both Bird and Penguin are red and with a line striking them ou
FlyingBird <abstract> inheriting from Bird
No Fields
Methods and Properties
+ Fly() <abstract>Birdkeeper class...FlyingBird named flyingBirds.AddBird method, if the Bird object is a FlyingBird, add it to the flyingBirds list, casting it as a FlyingBird.BirdsFlyAround method, change the Bird type to a FlyingBird type and target the flyingBirds list.
Now the Penguin isn't forced to implement a method that they shouldn't and an exception won't be thrown. Additionally, shared behaviors are now properly inherited.
A console window displaying the following text:
Line 1: Liskov Substitution Principle
Line 2: Hawk eats.
Line 3: Duck eats.
Line 4: Pigeon eats.
Line 5: Penguin eats.
Line 6: Hawk flies around.
Line 7: Duck flies around.
Line 8: Pigeon flies around.Restaurant class.Waiter class and have it implement IWorker.MakeFood and TakeOrder methods from the IWorker interface.Restaurant exposes the required contract: cook: Cook; waiter: Waiter; Restaurant(cook: Cook, waiter: Waiter); OpenRestaurant().Clients should not have to be forced to implement members of an interface.
Console.WriteLine("\n---------------- Interface Segregation Principle ----------------");
Restaurant restaurant = new Restaurant(new Cook());
restaurant.OpenRestaurant();Waiter because the Cook is taking too many orders.Restaurant class.A UML Class Diagram displaying the Restaurant class.
Restaurant
Fields
- cook: Cook
- waiter: Waiter (in blue text)
+ Restaurant(cook:Cook, waiter: Waiter) (The new parameter waiter: Waiter is in blue text)
+OpenRestaurant()Waiter class and have it implement IWorker.Waiter's PunchIn method, print out: "Waiter is punching in."Waiter's PunchOut method, print out: "Waiter is punching out."Waiter's TakeOrder method, print out: "Waiter is taking an order."We have a problem now. Waiters don't make food and Cooks don't take orders but they both have to implement methods because of the IWorker interface. The Restaurant still wants to make sure that only waiters take orders and only cooks make things.
MakeFood and TakeOrder methods from the IWorker interface.MakeFood()TakeOrder()Waiter implement IWaiter and remove the MakeFood method.Cook implement ICook and remove the TakeOrder method.OpenRestaurant method, replace the current calls with the calls below.this.cook.PunchIn();
this.waiter.PunchIn();
this.waiter.TakeOrder();
this.cook.MakeFood();
this.cook.PunchOut();
this.waiter.PunchOut();Waiter to the Restaurant constructor.
Now both workers have the IWorker methods and aren't forced to implement something unrelated to their functionality.
A Console window displaying the following text
Line 1: Interface Segregation Principle
Line 2: Cook punching in.
Line 3: Waiter is punching in.
Line 4: Waiter is taking an order.
Line 5: Cook making food.
Line 6: Cook punching out.
Line 7: Waiter is punching out.CharacterBuilder class from Human to ICharacter.ICharacter named character to the CharacterBuilder constructor and assign the field in the constructor.ICharacter exposes the required contract: Age: int get set; EyeColor: string get set; HairColor: string get set; Weight: double get set.Human exposes the required contract: Age: int get set; EyeColor: string get set; HairColor: string get set; Weight: double get set.High-level classes should not depend on low-level classes. Instead, abstractions should be used.
Console.WriteLine("\n---------------- Dependency Inversion Principle ----------------");
CharacterBuilder characterBuilder = new CharacterBuilder();
characterBuilder.ChangeAge(33);
characterBuilder.ChangeEyeColor("Blue");
characterBuilder.ChangeHairColor("Brown");
characterBuilder.ChangeWeight(130);
Console.WriteLine(characterBuilder.ToString());In the CharacterBuilder class the only type we have is Human. But what about other types? Aliens? Monsters? Robots? This design only works if the character we are trying to build is a Human type.
A UML class diagram displaying the existing Human class and the new Monster class both implementing the new <<interface>> ICharacter.
<<interface>> ICharacter (in blue text)
No Fields
Methods and Properties
Age: int <get> <set> (in blue text)
EyeColor: string <get> <set> (in blue text)
HairColor: string <get> <set> (in blue text)
Weight: double <get> <set> (in blue text)
Monster (implementing ICharacter) (in blue text)
No Fields
+ Age: int <get> <set> (in blue text)
+ EyeColor: string <get> <set> (in blue text)
+ HairColor: string <get> <set> (in blue text)
+ Weight: double <get> <set> (in blue text)
Human (implementing ICharacter)
No Fields.
+ Age: int <get> <set> (in blue text)
+ EyeColor: string <get> <set> (in blue text)
+ HairColor: string <get> <set> (in blue text)
+ Weight: double <get> <set> (in blue text)CharacterBuilder class from Human to ICharacter.There is still an issue, however, because if you want to change the type of thing being instantiated you'd have to open the class. It is still dependent on a low-level class.
ICharacter named character to the CharacterBuilder constructor and assign the field in the constructor.ToString:\nType: {this.character.GetType().Name}\nHuman into the CharacterBuilder.ToString, instantiate another CharacterBuilder and pass in a Monster type. Change the properties to…After the call to ToString, instantiate another CharacterBuilder and pass in a Monster type. Change the properties to whatever you like and call ToString again.
This is the power of Dependency Inversion. We can have two instances of the CharacterBuilder with two different types being passed it. All the class cares about is that the "thing" is an ICharacter. It is no longer dependent on knowing what the object type really is.

A Console window displaying the following text:
Line 1: Dependency Inversion Principle
Line 2: Blank
Line 3: Type: Human
Line 4: Age: 33
Line 5: Eye Color: Blue
Line 6: Hair Color: Brown
Line 7: Weight: 130
Line 8: Blank
Line 9: Type: Monster
Line 10: Age: 350
Line 11: Eye Color: Red
Line 12: Hair color: Black
Line 13: Weight: 400Return 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.
Create a new project. Create a Class Library Import classes and projects. Single Responsibility Principle Open/Closed Principle Liskov Substitution Principle Interface Segregation Principle Dependency Inversion Principle