Given that your POJO objects are from a vendor, I understand your reluctance to add functionality to them, and you obviously don't want code randomly sprinkled around your project.
Sounds like a job for either a set of Decorator subclasses, or as Ken suggests, facades.
I like decorators here because you'd then just refer to your vendor pojo by the name of the decorator class, which would then have the added behavior (isModified, searchByName, etc), all in one place.
To my understanding, it sounds like your POJO models are data only (or you want to keep them that way). Why not create facade objects, like Query, Count or other named functionality grouping, that hides all of the associated mechanics of your manipulation algorithms?
It wouldn't pollute the POJOs which is what it sounds like you want to avoid.
despite the question is at the moment 1 year old, hope it may appear helpful to some other people. So your willing to keep POJOs and behavour separately, so that these statements have place: a) POJOs only have properties and getters/setters, b) the same model manipulation code is reused, c) it's clear which classes are responsible for manipulating the model is widely acknowledged and is being achieved via splitting application to tiers, i.e.:
your POJOs - is plain model, containing fields/properties and just getters/setters;
business logic relating manipulations upon model is placed into Service layer - in your case everything concerning manipulations on e.g. Employee can be allocated in EmployeeService (and thus reused in numerous places);
everything relating persistance goes to DAO layer, e.g. EmployeeDAO;
for exchanging data between layers, e.g. the view and service - DTOs (data transfer objects) can be used.
In case if business logic is put into model beans, that's called domain-driven design - folks ask about it here and there (personally I'm not the admirer of domain driven design).
In a similar situation, we've decided to promote all "functions" (behaviours) as first class citizens of our object model.
So we now have distinct packages, one with POJO only, and one where we have Function objects (aka Processors) aplying functions on these objects.
For instance a Contract object as the POJO, and ContractBilling, ContractFinder, ContractCloser, etc. as processors. Most of them operate on a Contract or list of contracts.
We've found it very efficient and this has forced us to properly document everything in our system (as function have become objects). The architectural benefits of this approach have been awesome and, even though we have much more objects in this design, we have smaller classes much more manageable, understandable and easy to evolve.
I guess for operations such as find*, they belong to service classes; but for 'isModified', a service class won't help, it will have to be in the pojo itself unless of course the modification too happens through a service class. The corresponding service class can then maintain a state of such objects in a collection.
发布评论
评论(5)
鉴于您的 POJO 对象来自供应商,我理解您不愿意向它们添加功能,并且您显然不希望代码随机散布在您的项目中。
听起来像是一组装饰器子类的工作,或者正如肯建议的那样,是外观。
我喜欢这里的装饰器,因为您只需通过装饰器类的名称引用您的供应商 pojo,然后该装饰器类就会具有添加的行为(isModified、searchByName 等),所有这些都集中在一处。
Given that your POJO objects are from a vendor, I understand your reluctance to add functionality to them, and you obviously don't want code randomly sprinkled around your project.
Sounds like a job for either a set of Decorator subclasses, or as Ken suggests, facades.
I like decorators here because you'd then just refer to your vendor pojo by the name of the decorator class, which would then have the added behavior (isModified, searchByName, etc), all in one place.
据我了解,听起来您的 POJO 模型只是数据(或者您希望保持这种状态)。为什么不创建外观对象,例如
Query
、Count
或其他命名功能分组,隐藏操作算法的所有相关机制?它不会污染 POJO,这听起来像是您想要避免的。
To my understanding, it sounds like your POJO models are data only (or you want to keep them that way). Why not create facade objects, like
Query
,Count
or other named functionality grouping, that hides all of the associated mechanics of your manipulation algorithms?It wouldn't pollute the POJOs which is what it sounds like you want to avoid.
尽管这个问题目前已有 1 年历史,但希望它对其他人可能有所帮助。
因此,您愿意将 POJO 和行为分开,以便这些语句具有位置:
a) POJO 仅具有属性和 getter/setter,
b) 重复使用相同的模型操作代码,
c)明确哪些类负责操作模型
被广泛认可并且正在通过将应用程序拆分为层来实现,即:
如果将业务逻辑放入模型 bean 中,则称为领域驱动设计 - 人们会到处询问它(就我个人而言,我不是领域驱动设计的崇拜者)。
希望这有帮助。
despite the question is at the moment 1 year old, hope it may appear helpful to some other people.
So your willing to keep POJOs and behavour separately, so that these statements have place:
a) POJOs only have properties and getters/setters,
b) the same model manipulation code is reused,
c) it's clear which classes are responsible for manipulating the model
is widely acknowledged and is being achieved via splitting application to tiers, i.e.:
In case if business logic is put into model beans, that's called domain-driven design - folks ask about it here and there (personally I'm not the admirer of domain driven design).
Hope, this helps.
在类似的情况下,我们决定将所有“功能”(行为)提升为对象模型的一等公民。
因此,我们现在有不同的包,一个仅包含 POJO,另一个包含函数对象(又名处理器),在这些对象上应用函数。
例如,Contract 对象作为 POJO,ContractBilling、ContractFinder、ContractCloser 等作为处理器。他们中的大多数都是根据合同或合同清单运作的。
我们发现它非常高效,这迫使我们正确记录系统中的所有内容(因为函数已成为对象)。这种方法的架构优势非常巨大,尽管我们在设计中拥有更多的对象,但我们拥有更小的类,更易于管理、理解和易于发展。
In a similar situation, we've decided to promote all "functions" (behaviours) as first class citizens of our object model.
So we now have distinct packages, one with POJO only, and one where we have Function objects (aka Processors) aplying functions on these objects.
For instance a Contract object as the POJO, and ContractBilling, ContractFinder, ContractCloser, etc. as processors. Most of them operate on a Contract or list of contracts.
We've found it very efficient and this has forced us to properly document everything in our system (as function have become objects). The architectural benefits of this approach have been awesome and, even though we have much more objects in this design, we have smaller classes much more manageable, understandable and easy to evolve.
我猜对于诸如find*之类的操作,它们属于服务类;但对于“isModified”,服务类不会有帮助,它必须位于 pojo 本身中,除非修改也通过服务类发生。然后,相应的服务类可以维护集合中此类对象的状态。
I guess for operations such as find*, they belong to service classes; but for 'isModified', a service class won't help, it will have to be in the pojo itself unless of course the modification too happens through a service class. The corresponding service class can then maintain a state of such objects in a collection.