从不同的类访问对象 - 设计

发布于 2024-09-17 00:54:22 字数 1290 浏览 8 评论 0原文

我有三个类,TImageProcessingEngineTImageTProcessing

TImageProcessingEngine 是我用来公开所有我的类的类。走向世界的方法。

TImage 是我计划使用通用图像读取和图像写入功能的一个。

TProcessing 包含执行成像操作的方法。

class TImageProcessingEngine
{
    public:
        TImage* mpImageProcessingEngine;

};

class TImage
{
    public:
        int ReadImage();
        int WriteImage();

    private:
            //a two dimensional array holding the pixel values
        tImageMatrix*  mpImageMatrix;
};

class TProcessing
{
    public:
        int ConvertToBinary();
        int ConvertToGrayScale();
};

我的问题是如何访问TProcessing类中的对象mpImageMatrix以便我的调用应用程序可以使用以下内容

TImageProcessingEngine* vEngine = new TImageProcessingEngine;

//Converts an input gray scsale image to binary image
vEngine->ReadImage().ConvertToBinary();

//Write the converted image to disk
vEngine->WriteImage();

delete vEngine;
vEngine = NULL;

//During this whole processing internally, 
//the image is read in to `mpImageMatrix` 
//and will also be holding the binarised image data, 
//till writing the image to disk.

或者您会吗推荐我的班级设计的任何其他方法吗?

I have three classes, TImageProcessingEngine, TImage and TProcessing

TImageProcessingEngine is the one which i am using to expose all my methods to the world.

TImage is the one i plan to use generic image read and image write functions.

TProcessing contains methods that will perform imaging operations.

class TImageProcessingEngine
{
    public:
        TImage* mpImageProcessingEngine;

};

class TImage
{
    public:
        int ReadImage();
        int WriteImage();

    private:
            //a two dimensional array holding the pixel values
        tImageMatrix*  mpImageMatrix;
};

class TProcessing
{
    public:
        int ConvertToBinary();
        int ConvertToGrayScale();
};

My question is how do i access the object mpImageMatrix in class TProcessing? So that my calling application can use the following

TImageProcessingEngine* vEngine = new TImageProcessingEngine;

//Converts an input gray scsale image to binary image
vEngine->ReadImage().ConvertToBinary();

//Write the converted image to disk
vEngine->WriteImage();

delete vEngine;
vEngine = NULL;

//During this whole processing internally, 
//the image is read in to `mpImageMatrix` 
//and will also be holding the binarised image data, 
//till writing the image to disk.

Or Do you recommend any other approach to my class design?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鯉魚旗 2024-09-24 00:54:22

我当然会推荐一种不同的实现,但让我们先检查一下设计。

我不太明白 TImageProcessingEngine 的附加值,它没有带来任何功能。

事实上,我的建议非常简单:

  • Image类,保存值
  • Processing类(接口),应用操作
  • EncoderDecoder 类(接口),用于读取和写入不同的格式

只有当您能够从中获得效率时,Processing 类才能访问内部图像,这确实有意义(这很可能),在这种情况下,您可以简单地使 Processing 成为朋友,并让它解压其派生的 EncoderDecoder 的值,

class Image
{
public:
  Image();

  void Accept(Processing& p);
  void Encode(Encoder& e) const; // Image is not modified by encoding

  void Decode(Decoder& d); // This actually resets the image content

private:
  friend class Processing;

  size_t mHeight;
  size_t mWidth;
  std::vector<Pixel> mPixels; // 2D array of Pixels
};

class Processing
{
public:
  void apply(Image& image)
  {
    this->applyImpl(image.mHeight, image.mWidth, image.mPixels);
  }

private:
  virtual void applyImpl(size_t h, size_t w, std::vector<Pixel>& pixels) = 0;
};

遵循相同的原则。

请注意,我从来不需要显式指针,以及由此保证的正确性。

I would certainly recommend a different implementation, but let's check the design first.

I don't really understand the added value of TImageProcessingEngine, it doesn't bring any functionality.

My advice would be quite simple in fact:

  • Image class, to hold the values
  • Processing class (interface), to apply operations
  • Encoder and Decoder classes (interfaces), to read and write to different formats

It does make sense for the Processing class to have access to the images internal only if you can get efficiency from it (which is likely), in this case you can simply makes Processing friend and having it unpack the values for its derived

class Image
{
public:
  Image();

  void Accept(Processing& p);
  void Encode(Encoder& e) const; // Image is not modified by encoding

  void Decode(Decoder& d); // This actually resets the image content

private:
  friend class Processing;

  size_t mHeight;
  size_t mWidth;
  std::vector<Pixel> mPixels; // 2D array of Pixels
};

class Processing
{
public:
  void apply(Image& image)
  {
    this->applyImpl(image.mHeight, image.mWidth, image.mPixels);
  }

private:
  virtual void applyImpl(size_t h, size_t w, std::vector<Pixel>& pixels) = 0;
};

Encoder and Decoder follow the same principle.

Note how I never needed an explicit pointer, and the guaranteed correctness that results from it.

强者自强 2024-09-24 00:54:22

首先,根据您提供的代码,没有 ReadImage() & WriteImage() 函数位于 TImageProcessingEngine 类中,因此后面使用此类功能的代码存在缺陷。

至于解决方案,您可以为 tImageMatrix 指针创建一个 getter 函数,如下所示:

tImageMatrix* GetImageMatrix() { return mpImageMatrix; }

然后将该指针(或指向整个 TImage 实例的指针)传递给您要调用的 TProcessing 函数。

First off, based on your provided code there are no ReadImage() & WriteImage() functions in the TImageProcessingEngine class, so the later code where you use such functionality is flawed.

As for the solution, you can make a getter function for the tImageMatrix pointer like this:

tImageMatrix* GetImageMatrix() { return mpImageMatrix; }

Then just pass that pointer (or a pointer to the whole TImage instance) to the TProcessing function you want to call.

岁吢 2024-09-24 00:54:22

为什么你想要一个单独的 TProcessing 进程,当它专门具有仅访问 mpImageMatrix; 的函数时

,在 OOP 中,你必须绑定 数据成员及其操作..

因此,IMO,删除您的 TProcessing 类并在 TImage 中拥有这两个函数..

您的 TImage就像,

class TImage
{
public:
    int ReadImage();
    int WriteImage();
    int ConvertToBinary();
    int ConvertToGrayScale();

private:
        //a two dimensional array holding the pixel values
    tImageMatrix*  mpImageMatrix;
};

Why you want to have a separate TProcessing process, when it specifically has functions just accessing mpImageMatrix;

In OOP, you have to bind the data members and it's operations..

So, IMO, remove your TProcessing class and have both the functions within TImage..

Your TImage will be like,

class TImage
{
public:
    int ReadImage();
    int WriteImage();
    int ConvertToBinary();
    int ConvertToGrayScale();

private:
        //a two dimensional array holding the pixel values
    tImageMatrix*  mpImageMatrix;
};
倒带 2024-09-24 00:54:22

您可以创建一个访问器 TImage 类:

byte * pixelAt(unsigned x, unsigned y);

You could create an accessor TImage class:

byte * pixelAt(unsigned x, unsigned y);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文