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

Implementation of the Composite_Exercise() function as used in the Composite Pattern. More...

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "helpers/datetime.h"
#include "helpers/dynamicstring.h"
#include "helpers/formatstring.h"
#include "helpers/makelocaltime.h"
#include "Composite_FileDirEntry.h"
#include "Composite_FileAccess.h"
#include "Composite_Exercise.h"
Include dependency graph for Composite_Exercise.c:

Go to the source code of this file.

Functions

static void Composite_Exercise_FormatEntry (FileDirEntry *entry, int depth, DynamicString *output)
 Format the specified entry for display.
 
static void Composite_Exercise_ShowEntry (FileDirEntry *entry)
 Recursively display the contents of the hierarchical list of FileDirEntry objects starting with the given object.
 
void Composite_Exercise (void)
 Example of using the Composite Pattern.
 

Detailed Description

Implementation of the Composite_Exercise() function as used in the Composite Pattern.

Definition in file Composite_Exercise.c.

Function Documentation

◆ Composite_Exercise()

void Composite_Exercise ( void  )

Example of using the Composite Pattern.

The Composite pattern is used when a collection of objects is to be formed in a hierarchical form where each object needs to be treated like any other object but some objects can contain other objects.

This example uses a file structure of file and directories to represent each object type.

Definition at line 112 of file Composite_Exercise.c.

References Composite_Exercise_ShowEntry(), and Composite_FileAccess_GetEntry().

◆ Composite_Exercise_FormatEntry()

static void Composite_Exercise_FormatEntry ( FileDirEntry entry,
int  depth,
DynamicString output 
)
static

Format the specified entry for display.

This is a recursive call.

Parameters
entryThe FileDirEntry object to format
depthThe current recursion depth. This is used as leading space.
outputA DynamicString in which to collect the output.

Definition at line 34 of file Composite_Exercise.c.

References Composite_Exercise_FormatEntry(), datetime_to_string(), DynamicString_Append(), FileDirEntry_GetChildren(), FileDirEntry_GetFileDirType(), FileDirEntry_GetLength(), FileDirEntry_GetName(), FileDirEntry_GetWhenModified(), FileDirType_Directory, formatstring(), and FileDirEntry::next.

Referenced by Composite_Exercise_FormatEntry(), and Composite_Exercise_ShowEntry().

◆ Composite_Exercise_ShowEntry()

static void Composite_Exercise_ShowEntry ( FileDirEntry entry)
static

Recursively display the contents of the hierarchical list of FileDirEntry objects starting with the given object.

Parameters
entryThe FileDirEntry object to display

Definition at line 87 of file Composite_Exercise.c.

References Composite_Exercise_FormatEntry(), DynamicString_Clear(), and DynamicString::string.

Referenced by DesignPatternExamples_cpp::Composite_Exercise(), and Composite_Exercise().