OOP与程序性 [英] OOP vs procedural

查看:67
本文介绍了OOP与程序性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅我开始讨论。

我不是巨魔,我喜欢OOP架构。

但是我是C ++新手,有时我很难完全理解OOP的改进。请原谅我可怜的英语。

我希望它足以解释我的问题。


一般来说OOP优于程序性的,但在一个案例中我正在努力,

我不确定。这是我怀疑的一个例子。


我正在编写一个简单的基于GUI的GUI。

我有一些按钮,必须调用一些功能。


程序风格


我认为:


- 按钮小部件有一个身份。

-创建按钮时分配身份;例如:

new Button(" foo")指定ID =" foo"创建按钮。

- 按钮小部件有一个布尔button.mouseOver。

-I使用GLUT:当按下鼠标时,执行回调。这个

回调检查,对于所有按钮,如果mouseOver为真。

- 如果mouseOver为真,按钮返回他的ID


程序代码非常简单(它是伪代码):


--------------------- ---------------------

//声明一些功能


myfunction1(string)

myfunction2(int)

myfunction3(string,int)


//声明一些按钮:


new Button(" foo1");

new Button(" foo2");

new Button(" foo3");


// GLUT的鼠标回调函数


鼠标{

字符串ID

为每个小部件:

如果widget.mouseOver == true:

switch(widget.getID())

case(" foo1" ;):

myfunction1(widget.getname())

case(" foo2"):

myfunction1(widget.getvalue() )

case(" foo3"):

myfunction1(wi dget.getname(),widget.getvalue())

}

--------------------- ---------------------


这就是全部。


我所看到的:

- 它很灵活:我可以用我想要的任何功能链接一个按钮,不需要数量和参数类型的限制。

- 即使对于新手来说也是如此简单易读。

- 它不需要复杂的模式,指针,避免内存不足或

悬空参考可能性。

然而,我知道,它不是完全的OOP。所以一些编码人员,C ++专家,

告诉我应该使用functionoids。所以我从这里开始:

http://www.parashift.com/c++-faq-lit...html#faq-33.10


但我不明白为什么功能块(在这种情况下)更好。

我没有完全跟随它们,但是看起来,为了使用它们,我

必须写很多额外的代码(对我来说非常复杂)。


1)我必须用纯虚方法编写基类

2)我必须为每个函数编写派生类(所以如果我有100个
函数,我必须编写100个派生类?)

3)如何删除由functionoids创建的指针?也许对你来说

(C ++大师)这很容易,但是对于一个新手/中级编码器这个

意味着很多困难,硬调试,可能的内存不足或者悬空

引用。


请注意,在这两种情况下,我必须编写相同数量的函数。

但是在第二例如,编写一个简单的函数是不够的,但

需要将它插入到functionoid模式中......所以我几乎可以肯定

第二种情况下要写的行数增加了。


然而,怀疑的不仅仅是复杂性的增加,而是关于b / b的优点。 functionoids(总是,在这种特殊情况下);

再次,我承认还没有完全理解functionoids,但似乎

优势是关于可能通过的论点:


"然后你使用那​​个指针传递剩余的args,并且

系统神奇地采用你原来的args(那个冷冻干燥),
将它们与任何局部变量结合起来,这些变量在冷冻干燥之前计算出来的价格为b $ b,将所有这些与新传递的结合起来

args,并继续执行函数的执行,当它停止时,它冻结了



听起来不错!但是我已经具备了程序性的所有灵活性

方法:按钮可以调用任何类型的函数,任何类型和

参数的数量,混合局部变量与小部件变量......


最后,我引用Thinking in C ++,vol.2第10章:

---- -------------------------------------------------- -

理解继承和多态是一个挑战,你可能会开始过分重视这些技术。我们看到很多

过于复杂的设计(我们自己包含的),这是由于

继承放纵? - 例如,许多多重继承

设计通过坚持在任何地方都使用继承而发展。


极限编程中的一个指导原则是做最简单的事情

可能有效"一个似乎想要继承的设计可以通过使用组合来大大简化,并且你将会发现结果更加灵活,因为你将。那么在考虑设计时,你会问自己:这可能更简单吗?使用

组合?我真的需要继承这里,还有什么能买我?

-------------------------- -----------------------------


所以,最后一个问题是:在这里特定的情况确实是OOP

更好的程序性吗?


请记住我只是一个新手,也许我不能在这种情况下看OOP的优点是因为我对C ++的了解很少,或者可能存在一个更好的OOP方式,不需要复杂的功能,而且我不会我知道

因为我的无知。

再说一遍,我不是一个巨魔...只是我想要深刻理解,因为所有人都告诉
$ b $对于这个简单的GUI,我必须使用不同的方法,而不是切换




我希望不要无聊你好,请不要这么回答我...


问候,


Manuel

解决方案



Manuel w死记硬背:

请原谅我开始讨论。
我不是一个巨魔,我喜欢OOP架构。
但是我是C ++新手,有时我很难完全理解OOP的改进。请原谅我可怜的英语。
我希望这足以解释我的问题。

一般来说,OOP比程序更好,但在我正在研究的情况下,<我不确定。这是我怀疑的一个例子。

我正在编写一个简单的基于GUI的GUI。
我有一些按钮,必须调用一些功能。

我认为:

- 按钮小部件有一个身份。
- 创建按钮时分配身份;例如:
新按钮(foo)指定ID =" foo"创建按钮。
- 按钮小部件有一个布尔按钮.mouseOver。
- 我使用GLUT:当按下鼠标时,执行回调。这个
回调检查,对于所有按钮,如果mouseOver为真。
- 如果mouseOver为true,则按钮返回他的ID

程序代码非常简单(它''' s伪代码):

------------------------------------- -----
//声明一些函数

myfunction1(string)
myfunction2(int)
myfunction3(string,int)

声明一些按钮:

新按钮(foo1);
新按钮(foo2);
新按钮(" foo3") ;

// GLUT鼠标回调函数

鼠标{
每个小部件的字符串ID
如果widget.mouseOver = = true:
switch(widget.getID())
case(" foo1"):
myfunction1(widget.getname())
case(" foo2") :
myfunction1(widget.getvalue())
case(" foo3"):
myfunction1(widget.getname(),widget.getvalue())
} /> ----------- -------------------------------

这就是全部。
<我看到了什么:
- 它很灵活:我可以用我想要的任何功能链接一个按钮,没有数量和参数类型的限制。
- 它''即使对于新手也很容易阅读。
- 它不需要复杂的模式,指针,避免记忆缺乏或悬挂参考的可能性。

然而,我知道,这不是完全的OOP。所以一些编程人员,C ++专家,告诉我应该使用functionoids。所以我从这里开始:

http://www.parashift.com/c++-faq-lit...html#faq-33.10

但我不知道不明白为什么功能块(在这种情况下)更好。
我没有完全遵循它们,但是看起来,为了使用它们,我必须编写很多额外的代码(非常对我而言很复杂。

1)我必须用纯虚方法编写基类
2)我必须为每个函数编写一个派生类(所以如果我已经100
函数,我必须写100个派生类?)3)如何删除一个由functionoids创建的指针?也许对你来说(C ++的大师)这很容易,但是对于一个新手/中级编码器来说,这意味着很多困难,难以调试,可能的内存缺乏或悬挂
引用。 br />
请注意,在这两种情况下,我必须编写相同数量的函数。
但在第二种情况下,写一个简单的函数是不够的,但
它'需要将它插入到functionoid模式中......所以我几乎可以确定在第二种情况下写入的行会增加。

然而,疑问不仅仅是关于复杂性的增加,但关于functionoids的优点(总是在这个特殊情况下);
再次,我承认还没有完全理解functionoids,但似乎优点是关于可以传递的参数:

"然后你使用那​​个指针传递剩余的args,并且
系统神奇地采用你的原始args(冻干), />将它们与任何局部变量相结合在冷冻干燥之前计算的功能,将所有这些与新传递的
args相结合,并继续执行功能,当它冻结时停止运行。

这听起来不错!但我有程序性方法需要的所有灵活性:按钮可以调用任何类型的函数,任何类型和参数数量,将局部变量与小部件变量混合...

最后,我引用Thinking in C ++,vol.2第10章:
-------------------- -----------------------------------
了解继承和多态是一个挑战你可能会开始过分重视这些技巧。我们看到许多过于复杂的设计(我们自己包含的)来自于继承放纵 - 例如,许多多重继承
设计通过坚持继承在任何地方使用而发展。极限编程中的一个指导原则是做最简单的事情,它可能会起作用。一个似乎想要继承的设计通常可以通过使用组合来大大简化,而且你也会发现结果更灵活,因为你将通过研究一些本章中的设计模式。所以,在思考一个设计时,问问自己:这可能更简单吗?使用
组合?我真的需要继承这里,还有什么能买我?"
------------------------------ -------------------------

所以,最后一个问题是:在这个SPECIFIC案例中,OOP真的是真的程序更好吗?

请记住,我只是一个新手,也许在这种情况下我看不到OOP的优点因为我的小知识C ++,或者可能存在一种更好的OOP方式,不需要复杂的功能,我不知道
因为我的无知。
再次,我不是巨魔......只有我想要深刻理解,因为所有人都告诉我,对于这个简单的图形用户界面,我必须使用不同的方法,而不是切换基于。

我希望不要厌烦你,拜托,不要这么回答我......

问候,

Manuel



没问题手动,但你需要找到更合适的usenet组

处理你的que - 你仍然可以在这里发布c ++相关的

问题,我们很乐意回答并与你讨论;我在你的帖子里找不到
。对于oo设计 - 看看

comp.object。


同样,我们通常不为其他人做作业而只是帮助

特定语言障碍。我相信你只能找到资源

,其中你的任务可以完成一定的付款。

祝你好运

< br>

puzzlecracker写道:

Manuel写道:

请原谅我开始讨论。


[..过度引用删除..]

问候,

Manuel



没问题手册,但[..]




我发现两个错误。错误输入OP的名称和

过度引用被认为基本上是一个关闭 -

主题的帖子。不过,您的回复非常重要,而且非常有用。保持它。


puzzlecracker写道:


没问题手册,但你需要找到更合适的usenet处理你的问题的小组
- 你仍然可以在这里发布c ++相关的问题,我们很乐意回答并与你讨论;我在你的帖子里找不到任何内容。对于oo设计 - 看看
comp.object。


我认为OOP与OOP密切相关。

特别是在这种情况下(我已经强调了不同的时间它是特定的
case)它是关于使用我已经链接的C ++模式,称为functionoid。


同样,我们通常不会为其他人做家庭作业而只是帮助具有语言特定的障碍。


Eh..eh!

真的我希望这是一个家庭作业...

事实我是C ++新手并不意味着我很年轻......

我的第一台电脑是msx 128 Kb ram ...


我相信你只能找到资源
其中您的任务可以完成特定付款。



付款?

所以我必须解释详情。

自2000年以来,我开发了这个开源项目(免费,免费等):

www.makehuman.org


由于某些原因项目被更改,来自python(我知道)

到C ++。所以我需要学习C ++,用C ++移植python代码(在这种情况下是GUI)




我希望有人能帮助我,因为我'' ve帮助开源社区

年......


问候,


Manuel


Please, excuse me for starting this discussion.
I''m not a troll, and I love the OOP architecture.
However I''m a C++ newbie, and sometimes it''s hard for me to fully
understand the improvement of OOP. Please excuse my poor english too.
I hope it''s sufficient to explain my question.

Generally OOP is better than procedural, but in a case I''m working on,
I''m not sure. This is an example of my doubts.

I''m writing a simple GUI openGL based.
I''ve some buttons, that must call some functions.

PROCEDURAL STYLE

I assume that:

-The button widget has an "identity".
-The identity is assigned when the button is created; example:
new Button("foo") assign the ID = "foo" to button created.
-The button widget has a boolean button.mouseOver.
-I use GLUT: when mouse is pressed, a callback is executed. This
callback check, for all buttons, if mouseOver is true.
-If mouseOver is true, the button return his ID

The procedural code is very simple (it''s pseudo code):

------------------------------------------
//declare some functions

myfunction1(string)
myfunction2(int)
myfunction3(string, int)

//declare some buttons:

new Button("foo1");
new Button("foo2");
new Button("foo3");

//the mouse callback function for GLUT

mouse{
string ID
for each widget:
if widget.mouseOver == true:
switch(widget.getID())
case ("foo1"):
myfunction1(widget.getname())
case ("foo2"):
myfunction1(widget.getvalue())
case ("foo3"):
myfunction1(widget.getname(),widget.getvalue())
}
------------------------------------------

that''s all.

What I see:
- It''s flexible: I can link a button with any function I want, without
limits in number and type of arguments.
- It''s readable and easy, even for a newbie.
- It don''t need complex pattern, pointers, and avoid memory lacks or
dangling references possibilities.
However, I know, it''s not fully OOP. So some coders, experts in C++,
tell me that I should use functionoids. So I''m starting from here:

http://www.parashift.com/c++-faq-lit...html#faq-33.10

but I don''t understand why functionoids (in this case) is better.
I''ve not fully followed them, however seem that, in order to use them, I
must write a lot of extra code (very complex for me) lines.

1) I must write a base class with a pure-virtual method
2) I must write a derived classes for each function (so if I''ve 100
functions, I must write 100 derived classes ?)
3) how to delete the pointers created by a functionoids? Maybe for you
(gurus of C++) this is easy, but for a newbie/medium level coder this
mean a lot of difficults, hard debug, possible memory lacks or dangling
references.

Note that I must write, in both cases, the same number of functions.
But in second case, it''s not sufficient write a simple function, but
it''s needed insert it in the functionoid pattern...so I''m almost sure
that the lines to write is increased in second case.

However, the doubt is not only about the increase of complexity, but
about the advantages of functionoids (alway, in this particular case);
again, I admit to have not fully understand functionoids, but seem that
advantages is about the arguments that is possible to pass:

"Then later you pass the remaining args using that pointer, and the
system magically takes your original args (that were freeze-dried),
combines them with any local variables that the function calculated
prior to being freeze-dried, combines all that with the newly passed
args, and continues the function''s execution where it left off when it
was freeze-dried. "

This sound good! But I''ve all flexibility I need with procedural
approach: the buttons can call any type of function, with any type and
number of parameters, mixing local variable with widgets variables...

To conclude, I quote "Thinking in C++", vol.2 chapter 10:
-------------------------------------------------------
"Understanding inheritance and polymorphism is such a challenge that you
may begin to assign undue importance to these techniques. We see many
over-complicated designs (our own included) that result from
"inheritance indulgence"?- for example, many multiple inheritance
designs evolve by insisting that inheritance be used everywhere.

One of the guidelines in Extreme Programming is "Do the simplest thing
that could possibly work." A design that seems to want inheritance can
often be dramatically simplified by using composition instead, and you
will also discover that the result is more flexible, as you will
understand by studying some of the design patterns in this chapter. So
when pondering a design, ask yourself: "Could this be simpler using
composition? Do I really need inheritance here, and what is it buying me?"
-------------------------------------------------------

So, the final question is: in this SPECIFIC case is really true that OOP
is better of procedural?

Please, remember I''m only a newbie, and maybe I can''t see the advantages
of OOP in this case because my little knowledge of C++, or maybe exist a
better OOP way that don''t require complex functionoids and I don''t know
because my ignorance.
Again, I''m not a troll...only I want understand deeply because all tell
me I must use, for this simple GUI, a different approach, not "switch
based".

I hope to don''t boring you, and please, don''t reply me so hard...

regards,

Manuel

解决方案


Manuel wrote:

Please, excuse me for starting this discussion.
I''m not a troll, and I love the OOP architecture.
However I''m a C++ newbie, and sometimes it''s hard for me to fully
understand the improvement of OOP. Please excuse my poor english too.
I hope it''s sufficient to explain my question.

Generally OOP is better than procedural, but in a case I''m working on,
I''m not sure. This is an example of my doubts.

I''m writing a simple GUI openGL based.
I''ve some buttons, that must call some functions.

PROCEDURAL STYLE

I assume that:

-The button widget has an "identity".
-The identity is assigned when the button is created; example:
new Button("foo") assign the ID = "foo" to button created.
-The button widget has a boolean button.mouseOver.
-I use GLUT: when mouse is pressed, a callback is executed. This
callback check, for all buttons, if mouseOver is true.
-If mouseOver is true, the button return his ID

The procedural code is very simple (it''s pseudo code):

------------------------------------------
//declare some functions

myfunction1(string)
myfunction2(int)
myfunction3(string, int)

//declare some buttons:

new Button("foo1");
new Button("foo2");
new Button("foo3");

//the mouse callback function for GLUT

mouse{
string ID
for each widget:
if widget.mouseOver == true:
switch(widget.getID())
case ("foo1"):
myfunction1(widget.getname())
case ("foo2"):
myfunction1(widget.getvalue())
case ("foo3"):
myfunction1(widget.getname(),widget.getvalue())
}
------------------------------------------

that''s all.

What I see:
- It''s flexible: I can link a button with any function I want, without
limits in number and type of arguments.
- It''s readable and easy, even for a newbie.
- It don''t need complex pattern, pointers, and avoid memory lacks or
dangling references possibilities.
However, I know, it''s not fully OOP. So some coders, experts in C++,
tell me that I should use functionoids. So I''m starting from here:

http://www.parashift.com/c++-faq-lit...html#faq-33.10

but I don''t understand why functionoids (in this case) is better.
I''ve not fully followed them, however seem that, in order to use them, I
must write a lot of extra code (very complex for me) lines.

1) I must write a base class with a pure-virtual method
2) I must write a derived classes for each function (so if I''ve 100
functions, I must write 100 derived classes ?)
3) how to delete the pointers created by a functionoids? Maybe for you
(gurus of C++) this is easy, but for a newbie/medium level coder this
mean a lot of difficults, hard debug, possible memory lacks or dangling
references.

Note that I must write, in both cases, the same number of functions.
But in second case, it''s not sufficient write a simple function, but
it''s needed insert it in the functionoid pattern...so I''m almost sure
that the lines to write is increased in second case.

However, the doubt is not only about the increase of complexity, but
about the advantages of functionoids (alway, in this particular case);
again, I admit to have not fully understand functionoids, but seem that
advantages is about the arguments that is possible to pass:

"Then later you pass the remaining args using that pointer, and the
system magically takes your original args (that were freeze-dried),
combines them with any local variables that the function calculated
prior to being freeze-dried, combines all that with the newly passed
args, and continues the function''s execution where it left off when it
was freeze-dried. "

This sound good! But I''ve all flexibility I need with procedural
approach: the buttons can call any type of function, with any type and
number of parameters, mixing local variable with widgets variables...

To conclude, I quote "Thinking in C++", vol.2 chapter 10:
-------------------------------------------------------
"Understanding inheritance and polymorphism is such a challenge that you
may begin to assign undue importance to these techniques. We see many
over-complicated designs (our own included) that result from
"inheritance indulgence""- for example, many multiple inheritance
designs evolve by insisting that inheritance be used everywhere.

One of the guidelines in Extreme Programming is "Do the simplest thing
that could possibly work." A design that seems to want inheritance can
often be dramatically simplified by using composition instead, and you
will also discover that the result is more flexible, as you will
understand by studying some of the design patterns in this chapter. So
when pondering a design, ask yourself: "Could this be simpler using
composition? Do I really need inheritance here, and what is it buying me?"
-------------------------------------------------------

So, the final question is: in this SPECIFIC case is really true that OOP
is better of procedural?

Please, remember I''m only a newbie, and maybe I can''t see the advantages
of OOP in this case because my little knowledge of C++, or maybe exist a
better OOP way that don''t require complex functionoids and I don''t know
because my ignorance.
Again, I''m not a troll...only I want understand deeply because all tell
me I must use, for this simple GUI, a different approach, not "switch
based".

I hope to don''t boring you, and please, don''t reply me so hard...

regards,

Manuel


No problem Manual, but you need to find more appropriate usenet groups
that deals with your questions - you can still post c++ related
questions here, and we will gladly answer and discuss them with you; I
haven''t found any in your post. For oo design - take a look at
comp.object.

Similarly, we usually do not do homeworks for others but only help with
language specific obstacles. I am sure you can find resourses only
wherein your assignments can be completed for a certain payment.
Good luck


puzzlecracker wrote:

Manuel wrote:

Please, excuse me for starting this discussion.
[..excessive quoting removed..]

regards,

Manuel


No problem Manual, but [..]



Two mistakes, as I see it. Misspelling of the OP''s name and
excessive quoting of what is deemed to be essentially an off-
topic posting. Your reply is to the point, though, and quite
informative. Keep at it.


puzzlecracker wrote:


No problem Manual, but you need to find more appropriate usenet groups
that deals with your questions - you can still post c++ related
questions here, and we will gladly answer and discuss them with you; I
haven''t found any in your post. For oo design - take a look at
comp.object.
I think OOP is strongly related to OOP.
In this case in particular (I''ve underlined various time it''s specific
case) it''s about use of C++ pattern I''ve linked, called functionoid.


Similarly, we usually do not do homeworks for others but only help with
language specific obstacles.
Eh..eh!
Really I wish this was an homework...
The fact I''m C++ newbie not mean I''m young...
my first computer was msx 128 Kb ram...

I am sure you can find resourses only
wherein your assignments can be completed for a certain payment.


Payment?
So I must explain details.
I''ve developed this Open Source project (so free, gratis ect..) since 2000:

www.makehuman.org

for some reason the project is changed, from python (that I know)
to C++. So I need to study C++, to port python code (in this case GUI)
in C++.

I hope anyone want help me as I''ve helped the opensource community for
years...

Regards,

Manuel


这篇关于OOP与程序性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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