简体   繁体   English

子类互动设计

[英]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). 假设我想编写一些类来枚举USB设备并选择特定的设备并通过它为不同的平台传输数据(这只是一个非常基本的场景,更多的是设计问题,而不是与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. 现在,我想为USB设备提供一个设备基类,它可以具有基于字符串的enumerate_devices,get_particular_device等功能。现在,这将在不同的平台上以不同的方式实现。 So I'll probably have some child classes which will implement this using the platform specific apis. 因此,我可能会有一些子类,这些子类将使用特定于平台的api来实现此目的。

Now the other class I want to have will be some buffer class that will transfer data to USB endpoint. 现在,我要拥有的另一个类将是一些将数据传输到USB端点的缓冲区类。 This again will need to be implemented by different platforms based on the apis they provide. 同样,这将需要由不同的平台基于它们提供的api来实现。

Now suppose I create a Windows based USB device class and a buffer class and implement those using Windows provided apis. 现在,假设我创建了一个基于Windows的USB设备类和一个缓冲区类,并使用Windows提供的api来实现它们。 My question is what do I do if my Windows buffer class needs some data from my Windows device class. 我的问题是,如果Windows缓冲区类需要Windows设备类中的某些数据,该怎么办。 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* 我无法通过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 我还建议您阅读工厂模式,在阅读时您会明白为什么建议这样做,只是google工厂模式,您将获得大量描述

Abstract Factory pattern applies to your case. 抽象工厂模式适用于您的情况。 "Provide an interface for creating families of related or dependent objects without specifying their concrete classes." “提供一个用于创建相关或依赖对象族的接口,而无需指定其具体类。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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