OO类设计问题(逻辑布局)...... [英] OO class design question (logic placement)...

查看:51
本文介绍了OO类设计问题(逻辑布局)......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的OO设计问题......


我有一个Contract类。用户既可以保存现有合同,也可以用空白合同开始新的合同,填写数据,然后保存

new。合同。


我的合同类中有一个名为保存的方法。这被称为

这个...


dim oContract作为新合同

objContract.Save(me.txtContractNo)


当用户点击保存时合同表格上的图标。


从上面的代码可以看出,合同号是

保存的参数。方法。


我的问题是......代码放在哪里实际上

决定我们是在保存现有合同还是创建一个新的

一个?将它放在处理save

操作的Contract类中,或者应该留给程序员添加逻辑来执行此操作

表单''保存按钮?


我认为最好将此代码放在合同

类中。基本上逻辑是,如果合同号被传递到

,Save方法是空的,那么我们需要获得下一个可用的合同

数并使用#保存合同# 。如果传入的合同号码是

有价值,那么我们只需保存现有的合同。


我认为它最有意义在课堂上的逻辑 -

然后程序员不需要进行任何检查 - 因为它总是由班级保存完成。方法......


这有道理吗?


谢谢Brad

解决方案

" Brad Pears" < br *** @ truenorthloghomes.com写在

新闻:u1 ************** @ TK2MSFTNGP04.phx.gbl:


我的问题是......代码放在哪里实际上

决定我们是在保存现有合约还是创建

新的?将这个放在Contract类中,处理

保存操作,还是留给程序员添加逻辑

来执行此操作。保存按钮?



我会将数据放在联系人对象中。联系对象应该知道它的当前状态(即新的与更新)。


看看LLBLGen Pro - 它' DAL框架就是这样做的。



>

我认为将该逻辑放在

类中是最有意义的 - 然后程序员不需要进行任何检查 - 因为它将总是由类来完成
;保存"方法......


这有道理吗?



对象应该能够在实例化时设置其状态。


在对象实例化时,对象状态我是新的,而且我是b $ b而不是DIRTY。我不是很肮脏,因为我内部没有发生任何变化。


如果我内部(对象)发生了变化,那么我就被标记为DIRTY。


如果来自数据库的数据填充了我,那么对象将是旧的。我将会因为数据库逻辑填充了我而感到很沮丧。我将被标记为

DIRTY。


我还有另一个名为MARKED_FOR_DELETION的州。


1)我的实例(对象),我的状态将是新的。我是

NEW没有来自数据库的数据填充了我,所以当我保存时,我的数据将会被添加/插入到数据库中。


2)在实例化I(对象)时,我是新的而不是DIRTY,但数据已经填充了我的b $ b,所以我将被标记为OLD而不是DIRTY。当我保存
时,我将用我的数据更新数据库。

哦,我的Save方法已被调用。


我的状态是什么(对象)?


1)我很生气在我身上发生了一些变化。如果我不是很肮脏,那么

别对我做任何事情而不理我。


2)我很新又很脏,所以加我数据到数据库。


3)我老了又脏了,所以在数据库中更新我的数据。


嘿,我不是那个。我是MARKED_FOR_DELETION。


1)如果我是NEW和MARKED_FOR_DELETION,不要对我做任何事情(

对象),因为我没有数据需要从数据库中删除。


2)如果我是OLD和MARKED_FOR_DELETION,我在数据库中有数据,所以

删除我的数据。


嘿,我(对象)那些可能是我的状态以及我的Save

方法可能如何工作。


布拉德,


我建议它不像一般表格决定那样真正的OO问题

。制作。


一种方法是从你拥有的其他答案中剔除(尽管不是为了与b'b b中所说的任何内容相矛盾)并且建议你分开了契约对象和契约表格的

概念。


对象理解如何加载自身,保存自己,你甚至可以

(例如)在co上有一个静态方法ntract obect检查

是否已存在此对象的实例。


表单只是从用户获取数据并在表单生成后关闭/>
一直是OK''d。从这里开始,您的代码将查询Contract

表单对象的属性,并将它们传递给契约对象。最后一步是调用

保存合同对象,该对象决定是否需要创建一个新的
对象,更新/覆盖现有对象...


File对象以这种方式工作。有一个打开的对话框,保存对话框,以及

的File对象....当你打开一个文件时,你首先要查询的是什么文件

打开使用文件打开对话框,然后使用文件对象的文件名

属性打开该文件,从中读取等。

第二种方法是简单地使用数据控件构建你的

表格,并使用

gridview,databindernavigator等附带的查看/编辑/保存功能......

这完全取决于应用程序的上下文...

ASP.Net: http://www.ondotnet.com/pub/a/dotnet...spdatactl.html

Windows: http://www.codeproject.com/csharp/Bi。 ..ENDNavCS.asp

" Brad Pears" < br *** @ truenorthloghomes.com写信息

新闻:u1 ************** @ TK2MSFTNGP04.phx.gbl ...


这是一个简单的面向对象设计问题...


我有一个Contract类。用户既可以保存现有合同,也可以用空白合同开始新的合同,填写数据,然后保存

anew合同。


我的合同类中有一个名为保存的方法。这被称为

这个...


dim oContract作为新合同

objContract.Save(me.txtContractNo)


当用户点击保存时合同表格上的图标。


从上面的代码可以看出,合同编号是

的参数保存。方法。


我的问题是......代码放在哪里实际上

决定我们是在保存现有合同还是创建一个新的

一个?将它放在处理保存

操作的Contract类中,或者应该留给程序员添加逻辑来执行此操作

在表单上保存按钮?


我认为最好将此代码放在合同

类中。基本上逻辑是,如果合同号被传递到

,Save方法是空的,那么我们需要获得下一个可用的合同

数并使用#保存合同# 。如果传入的合同号码是

有价值,那么我们只需保存现有的合同。


我认为它最有意义

类中的那个逻辑 - 然后程序员不需要进行任何检查 - 因为它将总是由类保存来完成。方法......


这有意义吗?


感谢Brad



Here is a simple OO design question...

I have a Contract class. The user can either save an existing contract or
they start off fresh with a blank contract, fill in the data and then save a
"new" contract.

I have a method in my contract class called "Save" which is called like
this...

dim oContract as new Contract
objContract.Save(me.txtContractNo)

when the user clicks the ''Save" icon on the contract form.

As you can see from the above code, the contract number is a paramter of the
"Save" method.

My question is this... Where should the code be placed that actually
determines whether we are saving an existing contract or creating a new
one?? WOuld this be placed in the Contract class that is handling the save
operation or should it be left to the programmer to add logic to do this on
the form''s save button?

I am thinking that it would be best to place this code in the Contract
class. Basically the logic is that if the contract number being passed to
the Save method is empty then we need to get the next available contract
number and save the contract using that #. If the contract number being
passed in has a value, then we will simply save the exisiting contract.

I think that it makes the most sense to place that logic within the class -
then the programmer does not need to do any checking - as it will always be
done by the class "Save" method...

Does this makes sense??

Thanks Brad

解决方案

"Brad Pears" <br***@truenorthloghomes.comwrote in
news:u1**************@TK2MSFTNGP04.phx.gbl:

My question is this... Where should the code be placed that actually
determines whether we are saving an existing contract or creating a
new one?? WOuld this be placed in the Contract class that is handling
the save operation or should it be left to the programmer to add logic
to do this on the form''s save button?

I would place the data inside the contact object. The contact object should
be aware of it''s present state (i.e. new vs. update).

Take a look at LLBLGen Pro - it''s DAL framework does this sort of thing.


>
I think that it makes the most sense to place that logic within the
class - then the programmer does not need to do any checking - as it will
always be done by the class "Save" method...

Does this makes sense??

The object should be able to set its state upon instantiate.

Upon the instantiation of the object, the object state is I am NEW, and I am
not DIRTY. I am not DIRTY, because no change has taken place within me.

If a change has taken place within me (the object), then I am marked DIRTY.

I the object will be OLD, if data from the database has populated me. I will
be DIRTY, because the database logic has populated me. I will be marked not
DIRTY.

I have another state called MARKED_FOR_DELETION too.

1) Upon the instantiation of me (the object), my state will be NEW. I am
NEW no data from the database has populated me, so when I am saved, my data
will be added/inserted into the database.

2) Upon instantiation I (the object), I am NEW and not DIRTY, but data has
populated me so I am going to be marked as OLD and not DIRTY. When I am
saved, I am going to update the database with my data.
Oh, my Save method has been invoked.

What is my state me (the object)?

1) I am DIRTY some change as taken place with in me. If I am not DIRTY, then
don''t do anything with me and ignore me.

2) I am NEW and DIRTY, so add my data to the database.

3) I am OLD and DIRTY, so update my data in the database.

Hey, I am none of that. I am MARKED_FOR_DELETION.

1) If I am NEW and MARKED_FOR_DELETION, don''t do anything with me (the
object) as I have no data that needs to be deleted from the database.

2) If I am OLD and MARKED_FOR_DELETION, I have data in the database so
delete my data.

Hey, me (the object) those could possibly be my states and how my Save
method might work.


Brad,

I''d suggest it''s not really an OO question as much as general forms decision
making.

One approach is to vear from the other answers you have (although not to
contradict anything that''s been said) and suggets that you split up the
concept of the Contract object and the Contract form.

The object understands how to load itself, save itself, and you could even
(for example) have a static method on the contract obect that checks whether
an instance of this object already exists.

The form simply gets data from the user and closes down once the form has
been "OK''d". From here, your code would query properties on the Contract
form object, passing them to a contract object. The final step is to call
save on the contract object, which decides whether it needs to create a new
object, update/overwrite to an existing object...

The File object works this way. There''s the open dialog, save dialog, and
the File object.... When you open a file you first query what file is to be
opened using the file open dialog, and then use the file object''s filename
property to open that file, read from it etc.
The second approach would be to simply use the data controls to build your
form, and use the view/edit/save funcationality that comes with the
gridview, databindernavigator and so on...
It all depends the context of the application...
ASP.Net: http://www.ondotnet.com/pub/a/dotnet...spdatactl.html
Windows: http://www.codeproject.com/csharp/Bi...ndingNavCS.asp
"Brad Pears" <br***@truenorthloghomes.comwrote in message
news:u1**************@TK2MSFTNGP04.phx.gbl...

Here is a simple OO design question...

I have a Contract class. The user can either save an existing contract or
they start off fresh with a blank contract, fill in the data and then save
a "new" contract.

I have a method in my contract class called "Save" which is called like
this...

dim oContract as new Contract
objContract.Save(me.txtContractNo)

when the user clicks the ''Save" icon on the contract form.

As you can see from the above code, the contract number is a paramter of
the "Save" method.

My question is this... Where should the code be placed that actually
determines whether we are saving an existing contract or creating a new
one?? WOuld this be placed in the Contract class that is handling the save
operation or should it be left to the programmer to add logic to do this
on the form''s save button?

I am thinking that it would be best to place this code in the Contract
class. Basically the logic is that if the contract number being passed to
the Save method is empty then we need to get the next available contract
number and save the contract using that #. If the contract number being
passed in has a value, then we will simply save the exisiting contract.

I think that it makes the most sense to place that logic within the
class - then the programmer does not need to do any checking - as it will
always be done by the class "Save" method...

Does this makes sense??

Thanks Brad



这篇关于OO类设计问题(逻辑布局)......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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