The adapter class implements the expected interface and keeps a reference to an object of the class you want to reuse. The methods defined by the interface call one or more methods on the referenced object and return a value of the expected type.
My Open Source Projects
By doing that, the adapter class fulfills the expected contract by implementing the interface and enables you to reuse existing, incompatible implementations. I like to start my morning with a fresh cup of coffee. The only issue is that I need to get out of bed and prepare the coffee before I can drink it.
It would be much better if it would be automatically prepared when my alarm rings.
- The Enneagram: Symbol of All and Everything – Mapping the path of your understanding;
- Democracy in Poland: Representation, participation, competition and accountability since 1989.
- Abstract Factory Pattern Using C# - Real World Example!
- Alien Phenomenology, or What Its Like to Be a Thing (Posthumanities).
- Design Patterns for Searching in C#!
- Pharmaceutical and Clinical Calculations.
The FilterCoffeeApp does exactly that. It expects an implementation of the FilterCoffeeMachine interface as a constructor parameter and uses it in the prepareCoffee method to brew a cup of filter coffee.
- Patterns and Practices in C# 7.
- The Mastery of Love: A Practical Guide to the Art of Relationship: A Toltec Wisdom Book!
- Ethnobotany of India, Volume 2: Western Ghats and West Coast of Peninsular India;
- The Ultimate Duct Tape Book: 25 Cool & Creative Projects to Get You on a Roll!?
- Design pattern for extending search criteria easily.
- A Delicious Slice Of Johnners!
The FilterCoffeeMachine interface is relatively simple. It just defines the brewCoffee method which you can call to make a cup of coffee.
Design Patterns(C#): Basic example Strategy pattern
That seems to be a good approach that enables you to use different coffee machines with your application. The only requirement is that all classes that represent a coffee machine implement the FilterCoffeeMachine interface. As you can see, the method has the same name as the one defined by the FilterCoffeeMachine interface, but the method signature is incompatible. It expects a parameter and declares an exception. I will not change the class so that it implements the required interface.
The BasicCoffeeMachine implements that interface and I would need to change that class whenever I change the interface. By introducing an adapter class, that implements the FilterCoffeeMachine interface and wraps the PremiumCoffeeMachine class, you enable your FilterCoffeeApp to use the coffee machine.
The interface and the existing class are not too different.
That makes the implementation of the adapter class relatively simple. As you can see in the code snippet, the FilterCoffeeAdapter class implements the FilterCoffeeMachine interface and expects a PremiumCoffeeMachine object as a constructor parameter.
Patterns and Practices in C# 7
It keeps that object in a private field so that it can use it in the brewCoffee method. The implementation of the brewCoffee method is the critical and for most adapter classes, the most difficult part. In this case, the PremiumCoffeeMachine class provides a method that you can call to perform the task.
In this example, there is no perfect way to do that. You can either write a log message and return null, as I did in the code snippet, or you can throw a RuntimeException. In your application, you might have better ways to handle the exception. You might be able to perform a retry or to trigger a different business operation.
If you have enjoyed reading this article and if you would like to receive the notifications about the freshly published.
NET Core content we encourage you to subscribe to our blog. Your email address will not be published. Want to make the best API possible?
Patterns and Practices in C# 7
Subscribe and get most out of your API with our latest guide. Download FREE guide:. This guide will help you get an edge over your competition. Add gift ;.
Remove gift ;. CalculateTotalPrice ;. WriteLine ;. Add truckToy ;. Add plainToy ;. Add soldierToy ;.
Add childBox ;. Previous C Design Patterns — Adapter. Related Posts. Leave a reply Cancel reply Your email address will not be published.
5 Replies to “Design Patterns(C#): Basic example Strategy pattern”
Insert details about how the information is going to be processed. Subscribe Now! Become an API master.