My favorites | Sign in
Project Home Wiki Issues Source
Search
for
AnnotationsMixinExample  
Updated Jul 9, 2011 by ZombiesA...@gmail.com

JavaDude Tools->Annotations->Annotations examples

Mixin Example

Note: I'm not a fan of the "I" prefix on interfaces, but I'm using it here to make it more clear which types are interfaces vs classes.

Suppose we have the following three interfaces:

package sample;

import java.util.List;

public interface IFlightAgent {
	List<IFlight> getFlight();
	void reserve(IFlight flight);
}

public interface ICarAgent {
	List<ICar> getCars();
	void reserve(ICar car);
}

public interface IHotelAgent {
	List<IHotel> getHotels();
	void reserve(IHotel hotel);
}

each with a corresponding implementation (FlightAgentImpl, CarAgentImpl, HotelAgentImpl), and we'd like to create a single aggregate TravelAgent that "mixes in" all of the function of these three.

You can specify the following:

package sample;

import com.javadude.annotation.Bean;
import com.javadude.annotation.Delegate;

@Bean(delegates = {
    @Delegate(type = IHotelAgent.class,
              property = "hotelAgent",
              instantiateAs = HotelAgentImpl.class),
    @Delegate(type = ICarAgent.class,
              property = "carAgent",
              instantiateAs = CarAgentImpl.class),
    @Delegate(type = IFlightAgent.class,
              property = "flightAgent",
              instantiateAs = FlightAgentImpl.class)
    }
)
public class TravelAgent extends TravelAgentGen
    implements IHotelAgent, ICarAgent, IFlightAgent
{

}

This will generate TravelAgentGen that creates instances of the "impl" classes and delegation methods for each of the specified interfaces:

// CODE GENERATED BY JAVADUDE BEAN ANNOTATION PROCESSOR 
// -- DO NOT EDIT  -  THIS CODE WILL BE REGENERATED! --
package sample;

@javax.annotation.Generated(
    value = "com.javadude.annotation.processors.BeanAnnotationProcessor", 
    date = "Thu Aug 14 23:25:48 EDT 2008", 
    comments = "CODE GENERATED BY JAVADUDE BEAN ANNOTATION PROCESSOR; DO NOT EDIT! THIS CODE WILL BE REGENERATED!")
public abstract class TravelAgentGen  {
    private sample.IHotelAgent hotelAgent_;
    private sample.ICarAgent carAgent_;
    private sample.IFlightAgent flightAgent_;
    public TravelAgentGen() {
        ;
        hotelAgent_ = new sample.HotelAgentImpl();
        carAgent_ = new sample.CarAgentImpl();
        flightAgent_ = new sample.FlightAgentImpl();
    }


    public java.util.List<sample.IHotel> getHotels() {
        return hotelAgent_.getHotels();
    }
    public void reserve(sample.IHotel hotel) {
        hotelAgent_.reserve(hotel);
    }
    public java.util.List<sample.ICar> getCars() {
        return carAgent_.getCars();
    }
    public void reserve(sample.ICar car) {
        carAgent_.reserve(car);
    }
    public java.util.List<sample.IFlight> getFlight() {
        return flightAgent_.getFlight();
    }
    public void reserve(sample.IFlight flight) {
        flightAgent_.reserve(flight);
    }
    @Override
    public java.lang.String toString() {
        return getClass().getName() + '[' + paramString() + ']';
    }
    protected java.lang.String paramString() {
        return "";
    }
}

JavaDude Tools->Annotations->Annotations examples


Sign in to add a comment
Powered by Google Project Hosting