I'm looking for the name of the design pattern I just crafted and whether or not it's a good one.
So basically I was looking for a way to hold a main behavior among subclasses of an Actor class, divided in sub-behaviors that would differ based on which subinterfaces the subclass implements.
Here's a simplified example to make my point easier to understand...
An Actor Class :
public abstract class Actor implements FirstSubBehavior, SecondSubBehavior/*, MoreBehaviors...*/{
public final void mainBehavior(){
doFirstSubBehavior();
doSecondSubBehavior();
/* ... */
//doNthSubBehavior();
System.out.println();
}
}
A FirstSubBehavior Interface :
public interface FirstSubBehavior{
public void doFirstSubBehavior();
public interface FirstWay extends FirstSubBehavior{
public default void doFirstSubBehavior(){
System.out.println("I'm doing the 1st sub-behavior using the 1st way.");
}
}
public interface SecondWay extends FirstSubBehavior{
public default void doFirstSubBehavior(){
System.out.println("I'm doing the 1st sub-behavior using the 2nd way.");
}
}
/* ... */
public interface NthWay extends FirstSubBehavior{
public default void doFirstSubBehavior(){
System.out.println("I'm doing the 1st sub-behavior using the Nth way.");
}
}
}
A SecondSubBehavior Interface :
public interface SecondSubBehavior{
public void doSecondSubBehavior();
public interface FirstWay extends SecondSubBehavior{
public default void doSecondSubBehavior(){
System.out.println("I'm doing the 2nd sub-behavior using the 1st way.");
}
}
public interface SecondWay extends SecondSubBehavior{
public default void doSecondSubBehavior(){
System.out.println("I'm doing the 2nd sub-behavior using the 2nd way.");
}
}
/* ... */
public interface NthWay extends SecondSubBehavior{
public default void doSecondSubBehavior(){
System.out.println("I'm doing the 2nd sub-behavior using the Nth way.");
}
}
}
And a Main Class to test it all :
public class Main{
public static void main(String[] args){
class Actor1 extends Actor implements FirstSubBehavior.FirstWay, SecondSubBehavior.FirstWay {}
new Actor1().mainBehavior();
class Actor2 extends Actor implements FirstSubBehavior.FirstWay, SecondSubBehavior.NthWay {}
new Actor2().mainBehavior();
}
}
This produces the following output :
I'm doing the 1st sub-behavior using the 1st way.
I'm doing the 2nd sub-behavior using the 1st way.
I'm doing the 1st sub-behavior using the 1st way.
I'm doing the 2nd sub-behavior using the Nth way.
So the main is important, in there you can see how I want to use my Actor class : I want to code ONCE the main behavior of all my subclasses, but then being able to make it differ based on which default sub-behavior it implements.
Hope I was clear, I can reformulate if it's not.
Aucun commentaire:
Enregistrer un commentaire