c - 那么,如何将程序模块化并实现信息隐藏呢?

标签 c oop encapsulation information-hiding opaque-pointers

我创建了两个类“DEVICE_s”和“DEVICE_SET_s”,如下所示:

Device_Manager.h

typedef struct DEVICE_s DEVICE_s;
typedef struct DEVICE_SET_s DEVICE_SET_s;

Device_Manager.c

struct DEVICE_s
{
    uint32_t IP;
    TYPE_e Type;
    METHOD_e Method;
    GROUP_RULE_e GroupRule;
    char Name[NAME_SIZE];
};
struct DEVICE_SET_s
{
    uint8_t     Total;
    uint8_t     Used;
    uint8_t     Available;
    DEVICE_s    Set[SET_SIZE];
};
DEVICE_s Instance;
DEVICE_SET_s Objects;

因为我将这两个类放在同一个文件中,所以操作变量“Instance”和“Objects”的所有函数都放在这个文件中。

考虑到模块化,我认为这种方式很糟糕,所以我想创建另一个源文件来单独管理类“DEVICE_SET_s”,就像:

DeviceSet_Manager.h

typedef struct DEVICE_SET_s DEVICE_SET_s;

DeviceSet_Manager.c

#include "Device_Manager.h"
#include "DeviceSet_Manager.h"
struct DEVICE_SET_s
{
    uint8_t     Total;
    uint8_t     Used;
    uint8_t     Available;
    DEVICE_s    Set[SET_SIZE];    //Oops! Incomplete Type Is Not Allowed
};

但是,就DeviceSet_Manager.c而言,类“DEVICE_s”不可见(不是完整类型)。

我该如何解决这个问题?谢谢

最佳答案

您想要的是不透明类型

  • 设备
  • DEVICE_SET

这对于两者来说都是直接相同的方式:

  • header ,定义

    • 对象结构的不完整类型。指向它的指针是处理对象实例并传递给其接口(interface)函数的不透明类型
    • 接口(interface)函数的原型(prototype)
  • 实现

    • 完整类型
    • 接口(interface)函数

标题

设备.h

#ifndef DEVICE_H
#define DEVICE_H

struct device;

struct device * device_new(void);
void device_delete(struct device *);

#endif

device_set.h:

#ifndef DEVICE_H
#define DEVICE_H

#include "device.h"

struct device_set;

struct device_set * device_set_new(size_t);
void device_set_delete(struct device_set *);

int device_set_set_device(struct device_set *, size_t, struct device *);
struct device * device_set_get_device(struct device_set *, size_t); 


#endif

实现

设备.c

#include "device.h"

struct device {
  ...
};

struct device * device_new(void)
{
  struct device * pd = malloc(sizeof * pd);
  if (NULL != pd)
  {
    /* Init members here. */
  }

  return pd;
}

void device_delete(struct device * pd)
{
  if (pd)
  {
    /* de-init (free?) members here. */
  }

  free(pd);
}

device_set.c:

#include "device_set.h"

struct device_set
{
  size_t total;
  size_t used;
  size_t available; /* what is this for? isn't it just total - used? */
  struct device ** pd;
}

struct device_set * device_set_new(size_t nb)
{
  struct device_set pds = malloc(sizeof *pds);
  if (NULL != pds)
  {
    pds->pd = malloc(nb * sizeof *pds->pd);
    if (NULL == pds->pd)
    {
      free(pds);
      pds = NULL;
    }
    else
    {
      for (size_t d = 0; d < nb; ++d)
      {
        pds->pd[d] = NULL;
      }

      pds->total = nb;
      pds->used = 0;
      pds->available = 0;
    }
  }

  return pds;
}

void device_set_delete(struct device_set * pds)
{
  if (pds)
  {
    free(pds->pd);
    free(pds)
  }

  return;
}

int device_set_set_device(struct device_set * pds, size_t d, struct device * pd)
{
  int result = 0;

  if (pds->total <= d)      
  {
    result = ERANGE;
  }
  else
  {
    pds->pd[d] = pd;
  }

  return;
}    

struct device * device_set_get_device(struct device_set * pds, size_t d); 
  int result = 0;
  struct device * pd = NULL;

  if (pds->total <= d)      
  {
    result = ERANGE;
  }
  else
  {
    pd = pds->pd[d];
  }

  return pd;
}

关于c - 那么,如何将程序模块化并实现信息隐藏呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54399574/

相关文章:

我可以在 Linux 中执行写时复制 memcpy 吗?

c# - 继承还是不继承?需要一些专业意见

c# - 为什么我们不允许在接口(interface)中指定构造函数?

javascript - Typescript + Angular 公开服务方法

c - 你可以在 double 中存储多大的数字,在 c 中用 float 存储?

c - 信号处理程序中的格式化 I/O

.net - 抽象类和接口(interface)之间的主要区别是什么?

validation - 封装属性转换/验证

c++ - 是否可以使用命名空间来实现封装?

c - 在 C 中使用高斯消元法将二维矩阵转化为行梯形形式的问题