-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnotherApp.c
45 lines (38 loc) · 1.04 KB
/
AnotherApp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
// AnotherApp.c
// ObserverPattern
//
// Created by Balu Pillai on 16/11/2020.
// Copyright © 2020 Balu Pillai. All rights reserved.
//
#include <stdio.h>
#include "AnotherApp.h"
#include "TimeObserver.h"
#include "types.h"
#include <stdlib.h>
static void changedTime(void* instance, const uint32* newTime)
{
printf("Message from Analogue Watch app: Time has changed by %lu secs\n", *newTime);
}
AnalogueWatch_t* createAnalogueWatch(void)
{
AnalogueWatch_t* watch = malloc(sizeof watch);
if(NULL != watch)
{
/* Successfully created -> attach to the subject. */
TimeObserver observer = {0};
observer.instance = watch;
observer.notification = changedTime;
attach(&observer);
}
return watch;
}
void destroyAnalogueWatch(AnalogueWatch_t* watch)
{
/* Before deleting the instance we have to detach from the subject. */
TimeObserver observer = {0};
observer.instance = watch;
observer.notification = changedTime;
detach(&observer);
free(watch);
}