Can somebody help me with Observer patterns please?


Hi, I wrote some code but I'm really stack. Any guidance? Thanks

Here's the task:

Modify the class OverflowCounter of execise 10 so that it informs a single observer, when an overflow has occurred. The Observer is an interface class that any class can implement, if it wants to be informed about an overflow. Usually this class is a class that uses the counter. The advantage of this pattern is that when the OverflowCounter class has been implemented and tested, it does not need any modification even when we use it from any kind of “counter user objects”. Only thing we need to do to get our counter user class to be
informed is to implement the Observer interface in the counter user class.
To make this work and see how it works in practice you need the following modifications to the OverflowCounter class:

• Add a data member that points to the Observer interface class.
Observer* obs;

• Add function SetObserver, that sets the pointer member to point to any object that implements the Observer interface.
void SetObserver(Observer *)

• Add private function Notify, that informs the observer by calling the function
HandleLimitReached() of the observer.

Only one function is needed in the interface of the observer. The function is called HandleLimitReached. This function is used in a way to pass the message “Limit has been reached” from the OverflowCounter to the observer. This member function of observer is called from the member function Notify of the OverflowCounter.

class Observer {
public:
virtual void HandleLimitReached() = 0;
};

To test the new version of OverflowCounter, write an example class CounterUser, that uses a limited counter. It has an OverflowCounter as a data member and it implements the observer interface. To keep the test class as simple as possible only the following member functions are necessary:

• Constructor where the limited counter is initialized with a limit value (5 for
example) and where CounterUser object is set as the observer of the OverflowCounter.

• IncrementBy, where counter is incremented n times, where n is passed as a
parameter.

• HandleLimitReached() that displays that the limit has been reached.


MY CODE:

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  
    #pragma once
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    
    class Counter {
    public:
        virtual ~Counter() {};
        virtual void inc() = 0;
        virtual void dec() = 0;
        virtual operator int() = 0;
    };
    
    void UseCounter(Counter& ctr, int num);
    
    
    class Observer {
    public:
        virtual void HandleLimitReached() = 0;
    };
    
    
    class CounterUser {
    public:
        CounterUser(int lim_val = 5);
        IncrementBy
        HandleLimitReached() = 0;
    };
    
    
    
    class OverflowCounter : public Counter {
    public:
        OverflowCounter(int in_val = 0, int up_lim = 0);
    
        void inc() override = 0;
        void dec() override = 0;
        operator int() override = 0;
        Observer* obs;
        void SetObserver(Observer *)
    
    
    private:
        int in_val;
        int up_lim;
        //how to Notify for calling the function
    HandleLimitReached() of the observer  //
    };
    
    void main(){
        CounterUser cu(5);
        cu.IncrementBy(12); //OUTPUT: two times "Limit has been reached"
        CounterUser cu2(9);
        cu2.IncrementBy(9);
        cout << "Just passing time" << endl;
        cu2.IncrementBy(1); //OUTPUT: "Limit has been reached"
    } 
     

Copied and pasted from your other thread - where, I notice, you've modified your OP:

Firstly, fix the declaration of the SetObserver() method - there's something rather obvious missing from that line, which suggests you haven't even tried to compile it yet.

The next step would be to implement the SetObserver() method. That should be straightforward; the text of the question tells you exactly what to do.

FTR, here is the otiginal OP, as the poster seems to be dicking around with their posts:

Hi, I wrote some code but I'm really stack. Any guidance? Thanks

Here's the task:

Modify the class OverflowCounter of execise 10 so that it informs a single observer, when an overflow has occurred. The Observer is an interface class that any class can implement, if it wants to be informed about an overflow. Usually this class is a class that uses the counter. The advantage of this pattern is that when the OverflowCounter class has been implemented and tested, it does not need any modification even when we use it from any kind of “counter user objects”. Only thing we need to do to get our counter user class to be
informed is to implement the Observer interface in the counter user class.
To make this work and see how it works in practice you need the following modifications to the OverflowCounter class:

• Add a data member that points to the Observer interface class.
Observer* obs;

• Add function SetObserver, that sets the pointer member to point to any object that implements the Observer interface.
void SetObserver(Observer *)

• Add private function Notify, that informs the observer by calling the function
HandleLimitReached() of the observer.

Only one function is needed in the interface of the observer. The function is called HandleLimitReached. This function is used in a way to pass the message “Limit has been reached” from the OverflowCounter to the observer. This member function of observer is called from the member function Notify of the OverflowCounter.

class Observer {
public:
virtual void HandleLimitReached() = 0;
};

To test the new version of OverflowCounter, write an example class CounterUser, that uses a limited counter. It has an OverflowCounter as a data member and it implements the observer interface. To keep the test class as simple as possible only the following member functions are necessary:

• Constructor where the limited counter is initialized with a limit value (5 for
example) and where CounterUser object is set as the observer of the OverflowCounter.

• IncrementBy, where counter is incremented n times, where n is passed as a
parameter.

• HandleLimitReached() that displays that the limit has been reached.


MY CODE:
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

    #pragma once
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    
    class Counter {
    public:
        virtual ~Counter() {};
        virtual void inc() = 0;
        virtual void dec() = 0;
        virtual operator int() = 0;
    };
    
    void UseCounter(Counter& ctr, int num);
    
    
    class Observer {
    public:
        virtual void HandleLimitReached() = 0;
    };
    
    
    class CounterUser {
    public:
        CounterUser(int lim_val = 5);
        IncrementBy
        HandleLimitReached() = 0;
    };
    
    
    
    class OverflowCounter : public Counter {
    public:
        OverflowCounter(int in_val = 0, int up_lim = 0);
    
        void inc() override = 0;
        void dec() override = 0;
        operator int() override = 0;
        Observer* obs;
        void SetObserver(Observer *)
    
    
    private:
        int in_val;
        int up_lim;
        //how to Notify for calling the function
    HandleLimitReached() of the observer  //
    };
    
    void main(){
        CounterUser cu(5);
        cu.IncrementBy(12); //OUTPUT: two times "Limit has been reached"
        CounterUser cu2(9);
        cu2.IncrementBy(9);
        cout << "Just passing time" << endl;
        cu2.IncrementBy(1); //OUTPUT: "Limit has been reached"
    } 


     

Last edited on
Topic archived. No new replies allowed.