Design Pattern Examples
Overview of object-oriented design patterns
Proxy_Exercise.c
Go to the documentation of this file.
1
5
6#include <stdbool.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10
12
13#include "Proxy_ProxyService.h"
14
15#include "Proxy_Exercise.h"
16
17//=============================================================================
18//=============================================================================
19
37// ! [Using Proxy in C]
39{
40 printf("\nProxy Exercise\n");
41
42 DynamicString output;
44
45 printf(" Getting proxy object...\n");
46 IWorkByProxy* proxyObject = GetProxyService();
47
48 printf(" Calling Dowork() on proxy...\n");
49 bool success = DynamicString_Set(&output, "Initial call");
50 if (success)
51 {
52 success = proxyObject->DoWork(&output);
53 if (success)
54 {
55 printf(" Output from proxy = \"%s\"\n", output.string);
56 }
57 }
58 if (success)
59 {
60 printf(" Calling Dowork() on proxy...\n");
61 success = DynamicString_Set(&output, "Second call");
62 if (success)
63 {
64 success = proxyObject->DoWork(&output);
65 if (success)
66 {
67 printf(" Output from proxy = \"%s\"\n", output.string);
68 }
69 }
70 }
71 if (success)
72 {
73 printf(" Calling Dowork() on proxy...\n");
74 success = DynamicString_Set(&output, "Third call");
75 if (success)
76 {
77 success = proxyObject->DoWork(&output);
78 if (success)
79 {
80 printf(" Output from proxy = \"%s\"\n", output.string);
81 }
82 }
83 }
84
85 DynamicString_Clear(&output);
86
87 printf(" Done.\n");
88}
89// ! [Using Proxy in C]
void Proxy_Exercise(void)
Example of using the Proxy design pattern.
IWorkByProxy * GetProxyService(void)
Obtain the proxy service.
Declaration of the proxy service, accessed through the GetProxyService() function,...
Declaration of the Proxy_Exercise() function as used in the Proxy Pattern.
void DynamicString_Clear(DynamicString *string)
Clear a DynamicString object, releasing any allocated memory. Resets to an empty string.
Definition: dynamicstring.c:27
void DynamicString_Initialize(DynamicString *string)
Initialize a DynamicString object to an empty string.
Definition: dynamicstring.c:15
bool DynamicString_Set(DynamicString *string, const char *s)
Set the DynamicString object to the specified string, replacing whatever is in the DynamicString obje...
Definition: dynamicstring.c:73
Declaration of the DynamicString structure and supporting functions to work with dynamic strings.
Represents a string that can be grown dynamically.
Definition: dynamicstring.h:16
char * string
The string that can grow.
Definition: dynamicstring.h:17
Represents what can be done on the proxy object. This same interface is implemented on the real objec...
bool(* DoWork)(DynamicString *someArgument)
Does some work on the given argument and updates the given argument.