“数据隐藏”原型代码 [英] "data hiding" prototype code

查看:69
本文介绍了“数据隐藏”原型代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定数据隐藏是否属于是正确的术语,但我正在尝试使用这种面向对象的技术来模仿这种面向对象的技术。我知道C ++可能比我的例子提供了更多的b $ b,但是如果我做错了什么我只想要一些反馈来找出

。另外,我正在为一个

嵌入式环境工作,所以如果这个

代码效率很低,我也想知道。我意识到

使用访问者的开销很大。功能。


我想:

1.)隐藏

文件(algorithm.c)中的数据(在文件范围内声明为静态的变量)

2.)使用算法函数(Algorithm())操纵数据

3.)使用访问者提供对数据的外部访问。功能

(GetAlgorithmData())

这是我的代码:


main.c:

#include< stdio.h>

#include" algorithm.h"


int main(void)

{

const ALGORITHM_DATA * alg_ptr;


算法();

GetAlgorithmData(& alg_ptr);

/ * alg_ptr-> data1 = 99.9; * / / *这是不允许的* /

printf("%f\ n",alg_ptr-> data1);

printf("%f\ n",alg_ptr-> data2);

printf("%f\ n",alg_ptr- > data3);


返回0;

}


algorithm.c:

#include" algorithm.h"


static ALGORITHM_DATA alg;


void GetAlgorithmData(const ALGORITHM_DATA ** out)

{

* out =& alg;

}


void算法(无效)

{

alg.data1 = 1.0;

alg.data2 = 2.0;

alg。 data2 = 3.0;

}


algorithm.h:

#ifndef ALGORITHM_H_

#define ALGORITHM_H_


typedef struct {

double data1;

double data2;

double data3;

} ALGORITHM_DATA;


void GetAlgorithmData(const ALGORITHM_DATA ** out);

void算法(无效);


#endif / * ALGORITHM_H _ * /

I''m not sure if "data hiding" is the correct term, but I''m trying to
emulate this object-oriented technique. I know C++ probably provides
much more than my example, but I''d just like some feedback to find out
if I''ve done anything wrong. Also, I am working on this for an
embedded environment, so if there are great inefficiencies with this
code, I''d like to know that also. I realize there is overhead with
using an "accessor" function.

I''m trying to:
1.) "hide" data (variables declared as static at file scope) within a
file (algorithm.c)
2.) manipulate the data using an algorithm function (Algorithm())
3.) provide external access to the data using an "accessor" function
(GetAlgorithmData())

Here is my code:

main.c:
#include <stdio.h>
#include "algorithm.h"

int main(void)
{
const ALGORITHM_DATA *alg_ptr;

Algorithm();
GetAlgorithmData(&alg_ptr);
/*alg_ptr->data1 = 99.9;*/ /* This is not allowed */
printf("%f\n", alg_ptr->data1);
printf("%f\n", alg_ptr->data2);
printf("%f\n", alg_ptr->data3);

return 0;
}

algorithm.c:
#include "algorithm.h"

static ALGORITHM_DATA alg;

void GetAlgorithmData(const ALGORITHM_DATA **out)
{
*out = &alg;
}

void Algorithm(void)
{
alg.data1 = 1.0;
alg.data2 = 2.0;
alg.data2 = 3.0;
}

algorithm.h:
#ifndef ALGORITHM_H_
#define ALGORITHM_H_

typedef struct {
double data1;
double data2;
double data3;
} ALGORITHM_DATA;

void GetAlgorithmData(const ALGORITHM_DATA **out);
void Algorithm(void);

#endif /*ALGORITHM_H_*/

推荐答案

4月11日下午12:37, ; sofeng" < sofeng ... @ gmail.comwrote:
On Apr 11, 12:37 pm, "sofeng" <sofeng...@gmail.comwrote:

我不确定是否数据隐藏是正确的术语,但我正在尝试使用这种面向对象的技术来模仿这种面向对象的技术。
I''m not sure if "data hiding" is the correct term, but I''m trying to
emulate this object-oriented technique.



我喜欢把它称为封装。

I like to call it encapsulation.


我想要一些反馈来查找

如果我做错了什么。
I''d just like some feedback to find out
if I''ve done anything wrong.



我认为你可以采取一些措施来改善你的策略

这里,评论如下。

I think there are some things you can do to improve your strategy
here, comments to follow.


我意识到使用访问者会产生开销。功能。
I realize there is overhead with using an "accessor" function.



有,并且有几种方法可以缓解这种情况。但是,

除非你的访问器被紧紧地调用,否则它可能不是你需要解决的问题。

There are, and there are a couple of ways to mitigate that. However,
unless your accessors are being called in a tight loop, it''s probably
not an issue you need to solve.


我正在尝试:

1.)隐藏

文件中的数据(在文件范围内声明为静态的变量)(algorithm.c)
I''m trying to:
1.) "hide" data (variables declared as static at file scope) within a
file (algorithm.c)



首先,使变量成为静态在文件范围内并不是真的实现封装的目标,特别是当您在标题中发布

结构定义时。其次,文件

范围内的静态变量并不比全局变量好得多:它们肯定不是线程

安全,你也可以打开其他类型的错误。

First of all, making the variables static at file scope doesn''t really
achieve the goal of encapsulation, particularly when you publish the
struct definition in the header. Secondly, static variables at file
scope are not much better than globals: they''re certainly not thread
safe, and you open yourself to other sorts of errors as well.


2.)使用算法函数(Algorithm())操纵数据

3.)使用外部访问数据访问者函数

(GetAlgorithmData())
2.) manipulate the data using an algorithm function (Algorithm())
3.) provide external access to the data using an "accessor" function
(GetAlgorithmData())



您的设计并未明确谁拥有这个数据。算法

通常是应用于其他人数据的特定模式,尽管它可能会在过程中存储一些自己的数据。因此,使用

算法因为你的OO类的通用占位符可能会让你感到困惑。典型的算法将*获取*数据并返回*结果*。


但是,假设算法确实拥有这些数据,我会将
定义为内部结构.c文件,在.h

文件中提供typedef。 * * *隐藏您的数据。 Algorithm()可以返回一个指向这个

类型的指针,然后你可以用指针调用访问器来获得数据的各个元素。


没有时间重写你的代码,所以我希望有所帮助。


-bluejack

Your design does not make it clear who "owns" this data. An algorithm
is typically a particular pattern applied to someone else''s data,
although it may store some of it''s own in the process. Thus, using
"Algorithm" as the generic placeholder for your OO class could be
confusing. Typical algorithms will *take* data and return *results*.

However, assuming that Algorithm really does own this data, I would
define the struct inside the .c file, providing the typedef in the .h
file. *This* hides your data. Algorithm() can return a pointer to this
type, and then you can call accessors with the pointer to obtain
individual elements of the data.

Don''t have time to rewrite your code, so I hope that helps.

-bluejack


bluejackaécrit:
bluejack a écrit :

>

首先,在文件范围内使变量保持静态不会''确实

实现了封装的目标,特别是当您在标题中发布

结构定义时。其次,文件

范围内的静态变量并不比全局变量好得多:它们肯定不是线程

安全,你也可以打开其他类型的错误。
>
First of all, making the variables static at file scope doesn''t really
achieve the goal of encapsulation, particularly when you publish the
struct definition in the header. Secondly, static variables at file
scope are not much better than globals: they''re certainly not thread
safe, and you open yourself to other sorts of errors as well.



这是非常重要的一点。


您应该只输入.h文件:


typedef struct data;


隐藏来自外部用户程序的所有数据定义。


这有很多优点:


1)您的用户无法了解数据背后的内容。

2)无法分配新数据结构但必须通过通过你的
分配。

3)你发布带有访问者/设定者功能的界面。

4)你可以改变任何结构布局时间不需要

来重新编译任何用户代码。

This is a very important point.

You should put in the .h files just:

typedef struct data;

hiding ALL the definitions of data from external user programs.

This has many advantages:

1) Impossible for your user to know ANYTHING about what is behind data.
2) Can''t allocate new data structures but MUST pass through YOUR
allocation.
3) You publish the interface with accessor/setter functions.
4) You can change the structure layout at any time without needing
to recompile any user code.


感谢您的回复。请参阅下面的评论。


4月11日下午1:18,bluejack < bluuj ... @ gmail.comwrote:
Thank you for your reply. See my comments below.

On Apr 11, 1:18 pm, "bluejack" <bluuj...@gmail.comwrote:

首先,在文件范围内将变量设为静态并不是真的

实现封装的目标,特别是当您在标头中发布

结构定义时。其次,文件

范围内的静态变量并不比全局变量好得多:它们肯定不是线程

安全,你也可以打开其他类型的错误。
First of all, making the variables static at file scope doesn''t really
achieve the goal of encapsulation, particularly when you publish the
struct definition in the header. Secondly, static variables at file
scope are not much better than globals: they''re certainly not thread
safe, and you open yourself to other sorts of errors as well.



我不确定你的意思是在

标题中发布结构定义。我只把typedef放在标题中 - 没有发布

结构定义,对吧?

I''m not sure what you mean by "publish the struct definition in the
header". I only put the typedef in the header-- that doesn''t publish
the struct definition, right?


你的设计没有明确谁拥有这个数据。算法

通常是应用于其他人数据的特定模式,尽管它可能会在过程中存储一些自己的数据。因此,使用

算法因为你的OO类的通用占位符可能会让你感到困惑。典型的算法将*获取*数据并返回*结果*。


但是,假设算法确实拥有这些数据,我会将
定义为内部结构.c文件,在.h

文件中提供typedef。 * * *隐藏您的数据。 Algorithm()可以返回一个指向这个

类型的指针,然后你可以用指针调用访问器来获得数据的各个元素。
Your design does not make it clear who "owns" this data. An algorithm
is typically a particular pattern applied to someone else''s data,
although it may store some of it''s own in the process. Thus, using
"Algorithm" as the generic placeholder for your OO class could be
confusing. Typical algorithms will *take* data and return *results*.

However, assuming that Algorithm really does own this data, I would
define the struct inside the .c file, providing the typedef in the .h
file. *This* hides your data. Algorithm() can return a pointer to this
type, and then you can call accessors with the pointer to obtain
individual elements of the data.



我意识到名称算法可能会令人困惑。也许我应该

称它为对象。我的意图是algorithm.c。 "拥有"

数据。我确实在.c文件中定义了结构(它被命名为alg)

我在头文件中提供了typedef(名为

" ALGORITHM_DATA" )。也许我的命名令人困惑 - 也许我应该将

命名为AlgorithmDataType?我不希望Algorithm()直接

返回指向数据的指针,因为它的调用者不直接使用

数据。我希望能够使用来自

的另一个函数的algorithm.c'的数据,比如,Algorithm2(),它不会调用Algorithm()。

我想要的Algorithm2()调用algorithm.c的访问器函数,
GetAlgorithmData()获取指向const ALGORITHM_DATA的指针。我把const限定符加上

,这样Algorithm2()就无法修改algorithm.c中的数据。我意识到这个调用结构并不是在我过度简化的例子中显而易见的。我将我的例子修改为

包括Algorithm2()。如果我误解了你,请告诉我。


-sofeng


main.c:

#include" ; algorithm.h"

#include" algorithm2.h"


int main(void)

{

算法();

算法2();


返回0;

}
< br $> b $ b algorithm.c:

#include" algorithm.h"


静态ALGORITHM_DATA alg;


void GetAlgorithmData(const ALGORITHM_DATA ** out)

{

* out =& alg;

}


void算法(无效)

{

alg.data1 = 1.0;

alg.data2 = 2.0 ;

alg.data2 = 3.0;

}


algorithm.h:

#ifndef ALGORITHM_H_

#define ALGORITHM_H_


typedef struct {

double data1;

double data2;

double data3;


} ALGORITHM_DATA;


void GetAlgorithmData(const ALGO RITHM_DATA ** out);

void算法(无效);


#endif / * ALGORITHM_H _ * /


algorithm2.c:

#include< stdio.h>

#include" algorithm.h"


void Algorithm2(无效)

{

const ALGORITHM_DATA * alg_ptr;


GetAlgorithmData(& alg_ptr);

/ * alg_ptr-> data1 = 99.9; * / / *这是不允许的* /


/ *用这里的数据做一些事情* /

printf("%f\ n",alg_ptr-> data1);

printf("%f\ n",alg_ptr-> data2);

printf("%f\ n",alg_ptr-> data3);

}


algorithm2.h:

#ifndef ALGORITHM2_H_

#define ALGORITHM2_H_


void Algorithm2(void);


# endif / * ALGORITHM2_H _ * /

I realize that the name "Algorithm" might be confusing. Maybe I should
have called it "Object". My intention is that "algorithm.c" "owns" the
data. I did define the struct inside the .c file (it is named "alg")
and I provided the typedef in the header file ( named
"ALGORITHM_DATA"). Maybe my naming was confusing-- maybe I should have
named it "AlgorithmDataType"? I do not want Algorithm() to directly
return the pointer to the data because its caller does not use the
data directly. I want to be able to use algorithm.c''s data from
another function, say, Algorithm2(), which does not call Algorithm().
I want Algorithm2() to call algorithm.c''s accessor function,
GetAlgorithmData() to get a pointer to a const ALGORITHM_DATA. I put
the const qualifier so that Algorithm2() would not be able to modify
the data in algorithm.c. I realize this calling structure was not
evident in my over-simplified example. I modified my example to
include Algorithm2(). Let me know if I have misunderstood you.

-sofeng

main.c:
#include "algorithm.h"
#include "algorithm2.h"

int main(void)
{
Algorithm();
Algorithm2();

return 0;
}

algorithm.c:
#include "algorithm.h"

static ALGORITHM_DATA alg;

void GetAlgorithmData(const ALGORITHM_DATA **out)
{
*out = &alg;
}

void Algorithm(void)
{
alg.data1 = 1.0;
alg.data2 = 2.0;
alg.data2 = 3.0;
}

algorithm.h:
#ifndef ALGORITHM_H_
#define ALGORITHM_H_

typedef struct {
double data1;
double data2;
double data3;

} ALGORITHM_DATA;

void GetAlgorithmData(const ALGORITHM_DATA **out);
void Algorithm(void);

#endif /*ALGORITHM_H_*/

algorithm2.c:
#include <stdio.h>
#include "algorithm.h"

void Algorithm2(void)
{
const ALGORITHM_DATA *alg_ptr;

GetAlgorithmData(&alg_ptr);
/*alg_ptr->data1 = 99.9;*/ /* This is not allowed */

/* do something with data here */
printf("%f\n", alg_ptr->data1);
printf("%f\n", alg_ptr->data2);
printf("%f\n", alg_ptr->data3);
}

algorithm2.h:
#ifndef ALGORITHM2_H_
#define ALGORITHM2_H_

void Algorithm2(void);

#endif /*ALGORITHM2_H_*/


这篇关于“数据隐藏”原型代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆