Design Pattern Examples
Overview of object-oriented design patterns
Decorator_Exercise.c File Reference

Implementation of the various decorator functions that are called from the Decorator_Exercise() function as used in the Decorator Pattern. More...

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "helpers/dynamicstring.h"
#include "helpers/formatstring.h"
#include "Decorator_Exercise.h"
Include dependency graph for Decorator_Exercise.c:

Go to the source code of this file.

Functions

DynamicString_Decorate (DynamicString *s, const char *decoration)
 Helper function for applying decorations to the given string.
 
DynamicStringRedForegroundDecorator (DynamicString *s)
 Represents the RedForeground decorator, which alters the wrapped content to render as red text.
 
DynamicStringWhiteBackgroundDecorator (DynamicString *s)
 Represents the WhiteBackground decorator, which alters the wrapped content to render the background color as white.
 
DynamicStringUnderlineDecorator (DynamicString *s)
 Represents the Underline decorator, which alters the wrapped content to render it as underlined.
 
void Decorator_Exercise (void)
 Example of using the Decorator Pattern.
 

Detailed Description

Implementation of the various decorator functions that are called from the Decorator_Exercise() function as used in the Decorator Pattern.

Definition in file Decorator_Exercise.c.

Function Documentation

◆ _Decorate()

DynamicString * _Decorate ( DynamicString s,
const char *  decoration 
)

Helper function for applying decorations to the given string.

Parameters
sA DynamicString object that contains the string to be decorated.
decorationA string passed to sprintf() that wraps the %s (which represents the given string) in some kind of decoration.
Returns
Returns the DynamicString object so the decorator functions can be chained together.

Definition at line 29 of file Decorator_Exercise.c.

References DynamicString_Set(), formatstring(), and DynamicString::string.

Referenced by RedForegroundDecorator(), UnderlineDecorator(), and WhiteBackgroundDecorator().

◆ Decorator_Exercise()

void Decorator_Exercise ( void  )

Example of using the Decorator Pattern.

The Decorator pattern is used when some data element at run time needs to have its behavior altered. This is supported by providing wrapper functions called decorators that take an instance of the data element and return the data element. Each wrapper function alters the data element and returns it so it can be passed to the next wrapper function.

Definition at line 97 of file Decorator_Exercise.c.

References DynamicString_Clear(), DynamicString_Initialize(), DynamicString_Set(), RedForegroundDecorator(), DynamicString::string, UnderlineDecorator(), and WhiteBackgroundDecorator().

◆ RedForegroundDecorator()

DynamicString * RedForegroundDecorator ( DynamicString s)

Represents the RedForeground decorator, which alters the wrapped content to render as red text.

Parameters
sA DynamicString object that contains the string to be decorated.
Returns
Returns the DynamicString object so the decorator functions can be chained together.

Definition at line 52 of file Decorator_Exercise.c.

References _Decorate().

Referenced by Decorator_Exercise().

◆ UnderlineDecorator()

DynamicString * UnderlineDecorator ( DynamicString s)

Represents the Underline decorator, which alters the wrapped content to render it as underlined.

Parameters
sA DynamicString object that contains the string to be decorated.
Returns
Returns the DynamicString object so the decorator functions can be chained together.

Definition at line 78 of file Decorator_Exercise.c.

References _Decorate().

Referenced by Decorator_Exercise().

◆ WhiteBackgroundDecorator()

DynamicString * WhiteBackgroundDecorator ( DynamicString s)

Represents the WhiteBackground decorator, which alters the wrapped content to render the background color as white.

Parameters
sA DynamicString object that contains the string to be decorated.
Returns
Returns the DynamicString object so the decorator functions can be chained together.

Definition at line 65 of file Decorator_Exercise.c.

References _Decorate().

Referenced by Decorator_Exercise().