本文介绍了面向对象设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般的问题是:

向原始对象添加功能是否是正确的设计,以便对象可以照顾好自己



最近的例子:

我有一个单例logMngr。 logMngr将不同类型的记录保存到不同的Azure表。接收记录,排队为BaseRecord,然后使用threading.Timer将其保存到具有不同线程的Db。我们有2个想法:



1.BaseRecord有一个抽象方法saveToDb,每个记录都知道如何将自己保存到Db(表名,键...)保存到Db需要使用DAL单例来做到这一点。也许原始类型具有如此复杂的功能并不是一个好主意,但代码非常面向对象



2.使用:



if(记录是xRecord){logMngr.SavexRecordToDb}



,然后它不是一个开 - 关代码。我们不希望有更多记录种类。



任何见解?也许比两者都好一点?



谢谢你,Tal

The question in general is:
Is it a right design to add functionality to primitive objects, so that the object can "take care of himself"

Recent example:
I have a singleton logMngr. The logMngr saves records of different types to different Azure Tables. records are received, queued as BaseRecord, and then saved to Db with different thread using threading.Timer. We have 2 ideas:

1.The BaseRecord has an abstract method"saveToDb", and each record knows how to save himself to Db(the table name, the keys...) save to Db needs to use DAL singleton to do that. Maybe it is not a good idea for primitive type to have such a complex functionality, But the code is very "object oriented"

2. use:

if (record is xRecord){logMngr.SavexRecordToDb}

, and then it is not an open - close code. We do not expect to have more record kinds.

Any insights? Maybe a better idea than both?

Thank you, Tal

推荐答案


这篇关于面向对象设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 16:17