Cyril  Parisian

Cyril Parisian


Decorator Pattern in C#

Design patterns in the software world aid in developing faster and tested paradigms. It addresses a futuristic issue which the code (or the developer) will not encounter until it the code is implemented.

In general, design patterns increase code reusability and improve code readability. There are three categories — Creational, Structural and Behavioral.

Creational deals with class instantiation techniques. Structural deals with class object composition and provides ways to add new functionality to a class. Behavioral deals with how the object of the classes communicates.

In this blog, we are going to see a decorator pattern that falls under the Structural category. Let us first see what is decorator pattern and then understand what issue is being addressed with the above mentioned design pattern.

Table of contents

  1. What is decorator pattern?
  2. How to Implement Decorator pattern in C#
  3. Conclusion

What is Decorator Pattern?

Decorator pattern addresses the problem of adding responsibility to the existing classes without affecting the Open Close principle. This is without the need to make any modifications to the existing class dynamically.

It is achieved by adding a wrapper to the existing class. A decorator pattern consists of four main elements -

  1. Component
  2. Concrete component
  3. Decorator
  4. Concrete decorator

The component is the interface which provides operation being consumed by the concrete component. A concrete component is a class that implements the component and has the original operation that needs to be performed. The decorator defines the interface for all the dynamic operations that need to be added to the concrete components. Concrete Decorators are the individual operations that need to be added to the concrete components. It inherits a decorator.

Revolving around the four items, the decorator pattern has been constructed. It is implemented in places where the functionality is being extended without modifying the existing classes.

How to Implement Decorator pattern in C#?

Let us explain this with the help of a real-life situation.

Consider an ice cream shop where a user can customize his/her order. A user has to choose the base flavor of the ice cream and then choose the toppings.

Now, to calculate the total price of the ice cream, we need to add the base price of the ice cream along with the base price of the individual toppings.

We are going to implement this simple scenario using a decorator pattern in C#.

To implement this we are going to use a simple console-based .Net application. We are going to concentrate on implementing the pattern in C## over implementing it particularly in one of the services that can be created using .Net libraries.

Step 1

Create a .Net Core-based console application and provide a valid name and path for the solution

Step 2

Once the solution is created, open the Program.cs file. This is where the changes are going to be made. The file by default looks like below.

Step 3

As mentioned in the previous section, the idea is to implement the four main elements that are required for the implementation of the decorator pattern. As a first step, we are going to implement the Component. Since it is an ice cream shop, we are creating a component for ice cream.

public interface Icecream ​ { ​ double CalculatePrice(); ​ }

Step 4

Create the concrete component, which is the ice cream flavors in our case. Here, we will consider vanilla and implement the logic for it. The concrete component inherits the component and implements the method CalculatePrice.

public class VanillaIcecream : Icecream ​ { ​ public double CalculatePrice() ​ { ​ return 15; ​ } ​ }

Step 5

Create the decorator which will be used by the concrete decorator. This is used to add the topping values to the base price of the ice cream. Here, the decorator inherits the component and implements the methods required, and also has a constructor which expects the component to be passed.

public abstract class ToppingDecorator : Icecream ​ { ​ readonly Icecream _icecream; ​ public ToppingDecorator(Icecream icecream) ​ { ​ _icecream = icecream; ​ } ​ public virtual double CalculatePrice() ​ { ​ return _icecream.CalculatePrice(); ​ } ​ }

Step 6

Create the concrete decorator which inherits the ToppingDecorator and overrides the CalculatePrice method. So, that when the updated concrete component or decorator concrete has been passed, the sum value is being calculated.

Below is an example of Wafers which inherits the toppingdecorator and implements the method CalculatePrice, where it holds the logic to add the extra amount along with the base price of the product.

public class Wafers : ToppingDecorator ​ { ​ public Wafers(Icecream icecream) : base(icecream) ​ { ​ } ​ public override double CalculatePrice() ​ { ​ return base.CalculatePrice() + 3; ​ } ​ }

Similar to wafers, any other toppings can also be added based on the requirement. Below is another example of having choco-chip topping.

public class Chocochip : ToppingDecorator ​ { ​ public Chocochip(Icecream icecream) : base(icecream) ​ { ​ ​ } ​ public override double CalculatePrice() ​ { ​ return base.CalculatePrice() + 2; ​ } ​ }

Step 7

Now that we have implemented the Component (Icecream interface), Concrete component (VaniallaIceCream), Decorator (ToppingDecorator), and Concrete Decorator (Wafers and Chocochip). Let’s see how to consume them to achieve the decorator pattern.

Open the Program.cs file and include the below lines of code.

var icecreamOrder = new VanillaIcecream(); Console.WriteLine($"Total price of icecream - { icecreamOrder.CalculatePrice() }."); var wafer = new Wafers(icecreamOrder); Console.WriteLine($"Total price of icecream with wafer - { wafer.CalculatePrice() }."); var chocoChip = new Chocochip(wafer); Console.WriteLine($"Total price of icecream with wafer and chocochip - { chocoChip.CalculatePrice() }."); Console.ReadLine();

In the above example, first, the object of the concrete component (VanillaIcecream) is created. In the next line, the price of the base ice cream is being printed.

Next, an object for the wafer is being created, and the vanillaicecream object is being passed as an input parameter to it. So, the price can be calculated after including the wafer on top of vanilla ice cream.

And if we add a choco-chip on top of the wafer, then the object of the wafer is being passed as an input parameter to the choco-chip class. Then the price is calculated after including the base price of vanilla ice cream, wafer, and choco-chip.

Below is the final data that will be displayed in the console window.

#decorator-pattern #csharp #c#

What is GEEK

Buddha Community

Decorator Pattern in C#
Tamale  Moses

Tamale Moses


How to Run C/C++ in Sublime Text?

C and C++ are the most powerful programming language in the world. Most of the super fast and complex libraries and algorithms are written in C or C++. Most powerful Kernel programs are also written in C. So, there is no way to skip it.

In programming competitions, most programmers prefer to write code in C or C++. Tourist is considered the worlds top programming contestant of all ages who write code in C++.

During programming competitions, programmers prefer to use a lightweight editor to focus on coding and algorithm designing. VimSublime Text, and Notepad++ are the most common editors for us. Apart from the competition, many software developers and professionals love to use Sublime Text just because of its flexibility.

I have discussed the steps we need to complete in this blog post before running a C/C++ code in Sublime Text. We will take the inputs from an input file and print outputs to an output file without using freopen file related functions in C/C++.

#cpp #c #c-programming #sublimetext #c++ #c/c++

Dicey Issues in C/C++

If you are familiar with C/C++then you must have come across some unusual things and if you haven’t, then you are about to. The below codes are checked twice before adding, so feel free to share this article with your friends. The following displays some of the issues:

  1. Using multiple variables in the print function
  2. Comparing Signed integer with unsigned integer
  3. Putting a semicolon at the end of the loop statement
  4. C preprocessor doesn’t need a semicolon
  5. Size of the string matters
  6. Macros and equations aren’t good friends
  7. Never compare Floating data type with double data type
  8. Arrays have a boundary
  9. Character constants are different from string literals
  10. Difference between single(=) and double(==) equal signs.

The below code generates no error since a print function can take any number of inputs but creates a mismatch with the variables. The print function is used to display characters, strings, integers, float, octal, and hexadecimal values onto the output screen. The format specifier is used to display the value of a variable.

  1. %d indicates Integer Format Specifier
  2. %f indicates Float Format Specifier
  3. %c indicates Character Format Specifier
  4. %s indicates String Format Specifier
  5. %u indicates Unsigned Integer Format Specifier
  6. %ld indicates Long Int Format Specifier

Image for post

A signed integer is a 32-bit datum that encodes an integer in the range [-2147483648 to 2147483647]. An unsigned integer is a 32-bit datum that encodes a non-negative integer in the range [0 to 4294967295]. The signed integer is represented in twos-complement notation. In the below code the signed integer will be converted to the maximum unsigned integer then compared with the unsigned integer.

Image for post

#problems-with-c #dicey-issues-in-c #c-programming #c++ #c #cplusplus

Ari  Bogisich

Ari Bogisich


Loops in C++ | For, While, and Do While Loops in C++

In this Video We are going to see how to use Loops in C++. We will see How to use For, While, and Do While Loops in C++.
C++ is general purpose, compiled, object-oriented programming language and its concepts served as the basis for several other languages such as Java, Python, Ruby, Perl etc.

#c #c# #c++ #programming-c

Ari  Bogisich

Ari Bogisich


Using isdigit() in C/C++

In this article, we’ll take a look at using the isdigit() function in C/C++. This is a very simple way to check if any value is a digit or not. Let’s look at how to use this function, using some simple examples.

#c programming #c++ #c #c#

Shaylee  Lemke

Shaylee Lemke


Object Oriented Programming in C++ | C++ OOPs Concepts | Learn Object Oriented C++

C++ is general purpose, compiled, object-oriented programming language and its concepts served as the basis for several other languages such as Java, Python, Ruby, Perl etc.

The goal of this course is to provide you with a working knowledge of C++. We’ll start with the basics, including syntax, operators, loops, and functions. This Course will explain you how to use data structures and create your own Functions. This Course will show you the details of the powerful object and template systems so you can create useful classes and objects.

Youtube channel: ProgrammingKnowledge -

#c #c# #c++ #programming-c