将对象分割成最基本的部分

发布于 2024-09-30 06:56:52 字数 291 浏览 2 评论 0原文

不确定标题是否表达了我在这里想说的内容。

在面向对象中设计时,我是否应该将对象分成最具体的区域 - 因此,如果我有一个处理创建对象的工厂对象,但后来我遇到了一种为另一个目的创建对象的方法,即使它们可能是相同的对象是否值得创建一个单独的fcatory或只是添加到现有的。

我最担心的是用大量的东西来增加类,或者分割对象并将我的项目稀释成大量的类。

有什么帮助吗?

编辑:

我想在旁注/子主题中,我想找出您应该在程序中使用的粒度级别。有点,你应该降到多低?

Not sure if the title captures what I'm trying to say here.

When designing in OO should I be splitting my objects up into their most specific areas - so if I have a factory object that deals with creating objects but later on i come across a way of creating objects for another purpose even though they may be the same objects is it worth creating a seperate fcatory or just add to the exsiting.

My biggest worry is bulking up classes with tons of stuff, or splitting objects and diluting my projects into a sea of classes.

Any help?

EDIT:

I guess on a side note/sub topic part of me wants to find out the level of granularity you should use in a program. Kind of, how low should you go?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

泪意 2024-10-07 06:56:52

我最担心的是用大量的东西来增加课程,或者
分裂物体并稀释我的
项目进入类的海洋

观点,即使在任何规模合理的项目中,也很难在前面得到正确的解决方案,特别是因为实际上,在大多数情况下,需求本身会随着时间的推移而发展。
这就是“重构”的用武之地。您根据您在任何给定点所知道的知识进行设计,并且尽量不要对您认为系统可能发展的方向做出太多的信念飞跃。

鉴于您知道您现在正在构建什么,您可以设计您的类,尝试尽可能地利用 OO 概念 - 例如封装/多态性。正如其他人也指出的那样,这本身是非常难以实现的,而这正是设计 OO 系统方面的经验以及该领域的知识真正可以派上用场的地方。

根据您所知道的进行设计 -->构建它 -->回顾一下 -->重构它 -->重新设计-->它一直持续着..

My biggest worry is bulking up classes with tons of stuff, or
splitting objects and diluting my
projects into a sea of classes

This is a very valid point and in any even reasonably sized project, extremely difficult to get right up front especially because realistically, requirements themselves evolve over time in most cases.
This is where "Refactoring" come in. You design based on what you know at any given point and try not too make too many leaps of faith as to what you think the system MAY evolve to.

Given that you know what you are building right now, you design your classes trying to make the best possible use of OO concepts - eg encapsulation / polymorphism. This is itself, like others have noted as well, can be notoriously difficult to achieve and thats where experience, both in designing OO systems as well as knowledge of the domain can really come in handy.

Design based on what you know --> Build It --> Review it --> Refactor it --> Re-design --> and it goes on and on..

濫情▎り 2024-10-07 06:56:52

找到正确的细节级别和职责是 OOP 设计如此困难的原因。我们可以帮助您解决特定案例,但无法解决一般性问题。如果有算法或严格的方法论来解决这个问题,那么每个人都可以成为 OOP 设计师。

Finding the correct level of detail and responsibility is what makes OOP design so difficult. We can help you with a specific case but not with anything this general. If there were algorithms or strict methodologies of how to solve this, everyone could be an OOP designer.

和我恋爱吧 2024-10-07 06:56:52

我喜欢用一个经验法则来决定“现在是不是太大了?”是“我能简明地解释一下它的目的吗?”如果你开始不得不引入警告和大量狡猾的词语来解释你的设计组件的功能(无论是类、成员变量、方法还是其他什么),这可能是一个很好的迹象,表明它变得太复杂并且应该分开。

A rule of thumb I like for deciding "is this getting too big now?" is "can I explain the purpose of it concisely?" If you start having to introduce caveats and lots of weasel words to explain the functions of a component of your design (be it class, member variable, method or whatever) it might be a good indicator that it's getting too complex and should be split up.

无名指的心愿 2024-10-07 06:56:52

在您的具体情况下,如果您已经有一个工厂对象,那么 DRY 原则(不要重复自己)会说创建另一个做同样事情的工厂是一个坏主意。

这是您面临的实际问题吗?或者只是担心您的代码将来会如何增长?

In your specific case, if you already have a factory object then the DRY Principle (Don't Repeat Yourself) would say that it's a bad idea to create another factory that does the same thing.

Is this an actual problem that you face? Or merely a fear about how your code might grow in the future?

╰沐子 2024-10-07 06:56:52

如果您使用相同类型的对象来解决截然不同的问题,那么您可能需要重新设计该类以专注于关注点分离。如果您需要更具体的答案,您将需要提供需要此功能的类类型的示例。


我可能用词不当
问:我想我不会重复
我自己只是一个例子
在哪里放置代码,可能是
添加到现有工厂
创建用于导出的设计对象
数据到 Excel 电子表格。上
另一方面我可以看到它也可以
有自己的进口工厂
excel 数据。两家工厂都会
产生相同的对象,但内部
工作原理完全不同。 –

如果您不执行或计划执行任何类抽象(子类化或使用接口),您可能根本不需要使用工厂模式。工厂模式通常最适合提供基类类型的对象或实现特定接口的对象。

If you are using the same type of object to solve drastically different problems then you may need to redesign the class to focus on seperation of concerns. If you need a more specific answer, you will need to provide an example of a type of class that would need this functionality.


I might have worded things badly in
the Q. I guess I wouldnt be repeating
myself its just more of a case of
where to put the code, it could be
added to an exsiting factory that
creates design objects for exporing
data to excel spreadsheets. On the
other hand I could see it could also
have its own factory for importing
excel data. Both factories would
produce the same objects but the inner
workings are completely different. –

If you aren't doing or plan on doing any class abstraction (subclassing or using interfaces) you may not need to use the factory pattern at all. The factory pattern is generally best suited for supplying objects of a base class type or that implement a specific interface.

兮颜 2024-10-07 06:56:52

两者
工厂会生产同样的
对象,但内部运作是
完全不同。

不确定我是否理解正确,但这听起来像是 AbstractFactory 模式的候选者。

Both
factories would produce the same
objects but the inner workings are
completely different.

Not sure if I've understood you correctly, but this sounds like a candidate for the AbstractFactory pattern.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文