| |

VerySource

 Forgot password?
 Register
Search
Author: barstear

Structural design issues, experts please come in

[Copy link]

1

Threads

11

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-7-9 18:00:01
| Show all posts
If you don't want to define too many interfaces, you can define it like this:
struct IDeviceEvent
{
    long OnEvent1(int nEventId, void* pSender, void* pParam);
};
CWhat only needs to inherit IDeviceEvent, and then judge by conditions such as nEventId.

Also don’t understand what your CWhat does it do?
Reply

Use magic Report

1

Threads

11

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-7-9 18:45:01
| Show all posts
It should be an empty virtual function
struct IDeviceEvent
{
virtual long OnEvent1(int nEventId, void* pSender, void* pParam){};
};
Reply

Use magic Report

0

Threads

70

Posts

42.00

Credits

Newbie

Rank: 1

Credits
42.00

 China

Post time: 2020-7-10 09:00:01
| Show all posts
class IDevice // Abstract excuse for CWhat calling device

class IConnectPoint // Device calls CWhat interface

class CWhat
{
    static IConnectPoint* RegisterDevice(IDevice *ptr) // All device objects call this function to register themselves with what, and the function returns an interface to interact with themselves. (This class can achieve single-piece mode)
}
Reply

Use magic Report

1

Threads

11

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-7-10 11:15:02
| Show all posts
//Does it mean this

#include "stdafx.h"

struct IDeviceEvent
{
    typedef enum {EM_DO1 = 0, EM_DO2 }; //Event Id
    virtual long OnEvent1(int nEventId, void* pSender, void* pParam){return 0;};
};

class IDevice
{
public:
    IDevice():m_pEvent(NULL){};
    virtual ~IDevice(){};
    void SetEventHandle(IDeviceEvent* pEvent){m_pEvent=pEvent;};
protected:
    IDeviceEvent* m_pEvent;
};

class CDevice1: public IDevice
{
public:
    void Do1()
    {
        printf("do1\n");
        if (NULL != m_pEvent)
            m_pEvent->OnEvent1(IDeviceEvent::EM_DO1, this, NULL);
    };
};

class CDevice2: public IDevice
{
public:
    void Do2()
    {
        printf("do2\n");
        if (NULL != m_pEvent)
            m_pEvent->OnEvent1(IDeviceEvent::EM_DO2, this, NULL);
    };
};

class CWhat: public IDeviceEvent
{
public:
    long OnEvent1(int nEventId, void* pSender, void* pParam)
    {
        switch (nEventId)
        {
        case EM_DO1: printf("Ondo1\n");break;
        case EM_DO2: printf("Ondo2\n");break;
        }
        return 0;
    };
};


int _tmain(int argc, _TCHAR* argv[])
{
    CWhat wh;

    CDevice1 d1;
    d1.SetEventHandle(&wh);

    CDevice2 d2;
    d2.SetEventHandle(&wh);

    d1.Do1();

    d2.Do2();

    return 0;
}
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-7-10 11:45:01
| Show all posts
I still think it is better to set a common device interface. For the different operations of different devices, it depends on the internal packaging of DEVICE1 and DEVICE2.
Reply

Use magic Report

2

Threads

13

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

 Author| Post time: 2020-7-10 16:15:01
| Show all posts
to:huanwu
Ha ha, this method implements a "one-way" interaction: that is, the device calls CWhat. Of course in OnEvent1, you can return to remove device1,
But only in OnEvent1(), when the CWhat internal clock triggers polling and wants to request data from the device, this method is not enough.
This is to keep a reference table inside CWhat, and the clock will trigger the callback of each device. "Bidirectional" means this.
But I am not very willing to use "reference table", because the content of each device is called back differently, it may be called by device1: location, then its speed, and finally its status. CWhat may be required during the call Intersperse other operations to match the return result of device1.
So this situation determines that it is impossible to extract an interface like IDeviceFace for all devices, which cannot be achieved! Ha ha, that is to say
"Reference table" is not something like list<> vector<>, but one by one IDevice1* _dev1; IDevice2* _dev2;...
Something like that.
Imagine the following, so many devices, there are so many interface pointers in CWhat..., alas, this is why I am "very unwilling".
Reply

Use magic Report

2

Threads

13

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

 Author| Post time: 2020-7-10 18:00:01
| Show all posts
Hehe, everyone responded so quickly...
The reply at the beginning is for this part ofhuanwu:
struct IDeviceEvent
{
virtual long OnEvent1(int nEventId, void* pSender, void* pParam){};
};

to:raccoon1982
Make sense
Reply

Use magic Report

2

Threads

13

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

 Author| Post time: 2020-7-10 21:00:01
| Show all posts
to:爱德华德
//class IDevice // Abstract excuse for CWhat to call the device
//class IConnectPoint // device calls CWhat interface
Hehe, I can't cover it, if I can cover it, I won't have a headache.
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-7-12 10:30:01
| Show all posts
You can find a simple thread pool example on the Internet. If you only want to implement those functions, the simplest thread pool will do.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list