我应该如何将大型类和ated肿类拆分为较小的类? [英] How should I split large and bloated classes into smaller ones?

查看:89
本文介绍了我应该如何将大型类和ated肿类拆分为较小的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的经理类,我认为它做的太多了,但是我不确定如何将其划分为更多的逻辑单元。

I have a large 'Manager' class which I think is doing too much but I am unsure on how to divide it into more logical units.

一般来讲,该类基本上由以下方法组成:

Generally speaking the class basically consists of the following methods:


class FooBarManager
{
  GetFooEntities();
  AddFooEntity(..);
  UpdateFooEntity(..);
  SubmitFooEntity(..);
  GetFooTypes();
  GetBarEntities();
}

Manager类是我的业务逻辑的一部分,并且在数据访问中包含另一个 Manager类的实例

The Manager class is part of my business logic and constains an instance of another "Manager" class on the data access level which contains all CRUD operations for all entities.

我有来自数据访问层的不同实体,因此在Manager类之外有一个转换器来转换数据

I have different entities coming from the data access layer and therefore have a converter in place outside of the Manager class to convert data entities to business entities.

使用经理类的原因是,当我进行单元测试时,我希望能够模拟出每个经理类。现在,每个管理器类的位置都超过1000,并且每个包含40-50个方法。我认为它们非常膨胀,并且将所有数据访问逻辑都放在一个类中很尴尬。我应该怎么做?

The reason for the manager classes was that I wanted to be able to mock out each of the "Manager" classes when I do unittesting. Each of the manager classes is now over 1000 loc and contain 40-50 methods each. I consider them to be quite bloated and find it awkward to put all of the data access logic into a single class. What should I be doing differently?

如何拆分它们,应该使用任何特定的设计模式吗?

How would I go about splitting them and is there any specific design-pattern should I be using?

推荐答案

您的 FooBarManager 看起来很像上帝对象反模式。

Your FooBarManager looks a lot like a God Object anti pattern.

在像您这样的情况下,请考虑研究企业应用程序体系结构模式。乍一看,您似乎想创建一个数据映射器。但是请考虑使用活动记录之类的替代方案,这些替代品可能足以满足您的需求。

In a situation like yours, consider delving into Patterns of Enterprise Application Architecture, by Martin Fowler. At first sight, it looks like you want to create a Data Mapper. But consider alternatives like Active Records, that might be enough for your needs.

也可以考虑使用 ORM库/软件适用于您的平台。在没有充分理由的情况下建立自己的解决方案只会使您面临许多已经或多或少已被这些工具解决的问题。

Also consider using an ORM library/software for your platform. Building your own without a good reason will only confront you to the many problems that have already been more or less solved by these tools.

这篇关于我应该如何将大型类和ated肿类拆分为较小的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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