简体   繁体   中英

how to create generic java class that extends class and implements interface?

i am using DDD in my android project structure, in some domains, i need to create a new folder named "behavior" to put all screen behavior in this folder, like so "public class profileBehavior { .. }"

project print: http://tinypic.com/view.php?pic=r1hppi&s=8#.VPDdSlPF_rc

with the passage of time and the improvement aiming reuse, I'm thinking of creating a "genericBehavior" with the common methods instead, something like:

public class GenericBehavior {

private static Context context;
private static View view;

public GenericBehavior(Context context) {
    context = context;
}
public GenericBehavior(View view) {
    view = view;
}

public static GenericBehavior with(final Context context) {


    return new GenericBehavior(context);
}

public GenericBehavior where(final View view) {
    return new GenericBehavior(view);
}

and in the profileBehavior class, i hope to reuse the with and where methods to set a profileBehavior context and view

like:

public class ProfileBehavior extends GenericBehavior {


public ProfileBehavior(Context context) {
    super(context);
}

and in the profileFragment i hope to use like:

ProfileBehavior
            .with(getActivity())
            .where(rootView)
            .listenAttachOptions()
            .doScrollStuff();

I reading about the type params and interfaces, but i realy confused about that. in my case, its better to duplicate methods in behaviors or have a solutions for this?

Yes you can do that, though you would want modify or append instance variables not replace static ones, and return this not a new instance or at least an instance that wraps the old one return new GenericBehavior(context, this); ... GenericBehavior should probably be an interface ProfileBehavior implements so you can have ProfileBehavior extend some other class you want to act on.

There are three ways (that I could think of) doing it:

  1. by creating a base class (can be abstract) and implement where() and with() in this base class and extend it.
  2. If you're using Java 8 you get default methods (you can have a default implementation of methods in an interface)
  3. Composition : you can create an object that implements where() and with() (a behavior "base" object) and have any other instance of a "behavior" class hold an instance of such base-object as a private member. Each such class will delegate every where() and with() calls to this base-object.

An example of implementation of #3:

class BaseBehavior implement Behavior {
    public Behavior with(final Context context) {
        return new BaseBehavior(context);
    }

    public Behavior where(final View view) {
        return new BaseBehavior(view);
    }
}

class AnotherBehavior implement Behavior {
    BaseBehavior base;    

    AnotherBehavior(BaseBehavior base) {
        this.base = base;
    }

    public Behavior with(final Context context) {
        return base.with(context);
    }

    public Behavior where(final View view) {
        return base.with(view);
    }        
}

In general, composition is preferred over inheritance (so if you're not using Java 8 - it's better to go with #3 than with #1) because of many reasons, if to name a few: easier to debug and maintain, the code becomes less decoupled and has a simpler hierarchy and you can inherit (extend) only one class.

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