Skip to content

JamieTaylor94/mentoring-solid-openclosedprinciple

Repository files navigation

Open-closed principle

Repositories I have labelled "mentoring" are from a time that I was taking on mentoring responsibilities of other developers. I like the idea of providing a lesson situation followed by a practical example. The developer would work through each part and then create a document of their learnings and then we would have a one-to-one session to go over the practical together.

What is the open-closed principle?

Brief introduction

Learn about the open closed principle

Understand the complexities of the first code example having to have 3 different methods to generate account types and the switch statement inside the generate method which would need to happen every time the customer object is instantiated.

Then how much simpler, cleaner and more manageable is it to create derived types from a super/base class.

Practical

You work abroad as a greeter for a nightclub which is miles more interesting than writing code. The problem is you need to be able to greet in multiple languages. This application has been written by a new developer who has not had extensive training in solid principles.

The naughty developer broke the open-closed principle by adding a switch statement to the Greet method to account for the French, Spanish and Italian languages (previously Greet() returned "Hello". This means that any additional greetings will need to be added to the greet method and the switch statement will become hard to manage.

Your task is to take this application and apply the open-closed principle to it.

1.) Extract an interface IGreeter.cs
2.) Create derived classes which are specific to the new languages
3.) Refactor the greet method in each class
4.) Update the tests to use the correct derived class

Bonus:

  • Commit your code so far

  • Use an abstract base class BaseGreeter.cs rather than an interface and use this in one of the greeters.

  • Document the differences between using an interface and an abstract base class.

Test your knowledge

Create a file called open-closed.md

Answer the following questions:

What is the open-closed principle?

Why do we use it?

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages