简体   繁体   中英

Design for interaction of child classes

I wanted to get some few pointers to below design problem.

Suppose I wanted to write some classes to enumerate USB devices and select particular device and transmit data through it for different platforms.(Just a very rudimentary scenario.More of a design question than anything related to USB).

Now I want to have a device base class for USB devices which can have functions like enumerate_devices, get_particular_device based on a string etc. Now this would be implemented differently on different platforms. So I'll probably have some child classes which will implement this using the platform specific apis.

Now the other class I want to have will be some buffer class that will transfer data to USB endpoint. This again will need to be implemented by different platforms based on the apis they provide.

Now suppose I create a Windows based USB device class and a buffer class and implement those using Windows provided apis. My question is what do I do if my Windows buffer class needs some data from my Windows device class. What kind of pattern can I use so that the base classes remain anonymous to the internal intermediate platform based structures used in child classes and yet buffer class can use particular device class data members? Want to get a good design for this. Dont know if I am clear enough in writing. Hope to get some insight.

Edit: I know factory and abstract factory. This is not related to them. I cant access child class member function through factory patterns through an object of UsbDevice*

Brief outline.

SomeWindowsUsbDevice : public UsbDevice {
    public:
        void findDevices() {  // just a child class function. Not in base class
            //Code to find devices using platform apis.
            //fills below two private structures.
        }
    private:
        DevEnum* pdevenum; //some platform specific structure.
        Buffsize* pbufsize; // some platform specific structure.
}

SomeWindowsUsbDataTrf :  public UsbDataTrf {
    public:
        DevEnum* getdevicelist() {
            //Need to get access to SomeWindowsUsbDevice::pdevenum

        }

        Buffsize* getbuffsize() {
            //Need to get access to SomeWindowsUsbDevice::pdevenum
        }

}

Try Endpoint Redirection, more details are available at Endpoint Redirection

I also suggest you to read factory pattern, on reading you will get the point why it is suggested, just google factory pattern and you will have a huge description available

Abstract Factory pattern applies to your case. "Provide an interface for creating families of related or dependent objects without specifying their concrete classes."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM