简体   繁体   中英

A way to implement partial classes in java

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.

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.)

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM