A way to implement partial classes in java

僤鯓⒐⒋嵵緔 提交于 2019-11-27 21:31:53

问题


I have an interface that I want to implement in separate classes after doing a quick google search apparently, Java doesn't have partial classes. Is there a way that I can do this or am I stuck throwing all of my code into one class?

Basically, I am trying to write a service. Some of the service methods really belong in their own class and seem kind of illogical in the same class. Here is an example of what I am trying to do.

package com.upmc.esdm.messaging.epcd.service;
import java.util.List;

import javax.ejb.Remote;

import com.upmc.esdm.messaging.epcd13jpa.entities.EmailDomainTrust;


@Remote
public interface MessagingInterfaceRemote {
public List<EmailDomainTrust> getEmailDomains();

    public int upDateQualityTable();

    public string isLogial();

    public byte[] ToWritePartialClassesInJava();
}

I would normally have partial classes in C# and I would put similar methods that return similar values into one partial class (or maybe classes that update a record in one class). How would I implement this? Or should I just put all the method implementations into one class?

Thanks.


回答1:


There is nothing like partial classes in Java. You can achieve many of the same benefits using aggregation, delegation, and abstract base classes.

(I have caved to peer pressure and eliminated the “thankfully” remark that has generated so much heat in the comments. Evidently that little aside seems to have earned me four downvotes, despite being irrelevant to the answer.)




回答2:


Aspectj can be the answer to C#/.net partial class feature! Spring Roo is one of the typical development framework using aspectj to divide a class functionalities into several different files.




回答3:


Before going that route, you might want to consider the Builder design pattern. That allows your service to aggregate implementations that are implemented in separate classes. No need for a pre-compiler, and very clean from an OO perspective.

Each component class has a specific, narrow responsibility, with the service class enlisting the services of it's component classes to implement just the responsibility of the service.

If you really want to (not recommended), you can use a pre-processor to assemble parts of a class prior to compilation.

The only scenario where a pre-processor might make sense is if some of your implementation is code-generated and other parts are hand-coded. A straightforward approach might be to #include the externally defined class fragments in your main .java file and run a C pre-processor over the file before compilation.




回答4:


You can declare methods trough multiple interfaces and then let your concrete classes implement multiple interfaces.

More over, using java.lang.Proxy you can assemble your service from multiple interfaces and delegate actual method calls to a separate implementation.




回答5:


I hate to bring up an old thread, but thought I would bring a new perspective in respect to the given answers. Aspect Oriented programming may better achieve what you're trying to implement cross-cutting concerns.

You're looking for a robust associative solution where you can parse out the implementing details to different controlling methods. The Spring framework is very prominent in this space and often is associated with micro-services these days.



来源:https://stackoverflow.com/questions/9876339/a-way-to-implement-partial-classes-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!