Skip to content

Complex example

mguymon edited this page Jan 2, 2013 · 5 revisions

Example

Creating a Car with a make, manufacturer, mileage, status, a Driver and 4 wheels. The fun stuff happens in Car Blueprint

ModelFactory modelFactory = new ModelFactory();
modelFactory.registerBlueprint( new CarBlueprint() );
modelFactory.registerBlueprint( new WheelBlueprint() );
modelFactory.registerBlueprint( new DriverBlueprint() );
Car car = modelFactory.createModel( Car.class );

Blueprints

Car Blueprint

Blueprint for the Car model. The wheels property is annotated with @MappedList(target = Wheel.class, size = 4). This will set the value of wheels to a List<Wheel> with 4 instances of Wheel (created using the registeredWheelBlueprint). The driver property is annotated with @Mapped. This will set the value of driver to an instance of Driver (created using registered DriverBlueprint).

@Blueprint(Car.class)
public class CarBlueprint {

    @Default
    public String make = "car make";

    @Default
    String manufacturer = "car manufacturer";

    @Default
    Integer milage = 100;

    @Default
    Map status = new HashMap();

    @MappedList(target = Wheel.class, size = 4)
    List<Wheel> wheels;

    @Mapped
    Driver driver;
}

Wheel Blueprint

Wheel Blueprint sets default values for name, size and option.

@Blueprint(Wheel.class)
public class WheelBlueprint {

    @Default
    String name = "tire name";

    @Default
    Integer size = 10;

    @Default
    Object option = new ArrayList();

}

Driver Blueprint

Driver Blueprint sets default values for first name, last name, and age.

@Blueprint(Driver.class)
public class DriverBlueprint {

    @Default
    String firstName = "Steve";

    @Default
    String lastName = 10;

    @Default
    Integer age = 37;
}

Models

Car Model

public class Car {
    private String make;
    private String manufacturer;
    private Integer milage;
    private Map status;
    private List<Wheel> wheels;
    private Driver driver;

    public String getMake() {
        return make;
    }

    public void setMake(String make) {
        this.make = make;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public Integer getMilage() {
    return milage;
    }

    public void setMilage(Integer milage) {
        this.milage = milage;
    }

    public Map getStatus() {
        return status;
    }

    public void setStatus(Map status) {
            this.status = status;
    }

    public List<Wheel> getWheels() {
            return wheels;
    }

    public void setWheels(List<Wheel> wheels) {
            this.wheels = wheels;
    }

    public Driver getDriver() {
            return driver;
    }

    public void setDriver(Driver driver) {
            this.driver = driver;
    }   
}

Wheel Model

public class Wheel {
    public String name;
    public Integer size;
    public Object option;

    public String getName() {
            return name;
    }

    public void setName(String name) {
            this.name = name;
    }

    public Integer getSize() {
            return size;
    }

    public void setSize(Integer size) {
            this.size = size;
    }

    public Object getOption() {
            return option;
    }

    public void setOption(Object option) {  
            this.option = option;
    }
}

Driver Model

public class Driver {
    public String firstName;
    public String lastName;
    public Integer age;
    public Object license;

    public String getFirstName() {
            return firstName;
    }

    public void setFirstName(String firstName) {
            this.firstName = firstName;
    }

    public String getLastName() {
            return lastName;
    }

    public void setLastName(String lastName) {
            this.lastName = lastName;
    }

    public Integer getAge() {
            return size;
    }

    public void setSize(Integer age) {
            this.age = age;
    }

    public Object getLicense() {
            return license;
    }

    public void setLicense(Object license) {
            this.license = license;
    }
}
Something went wrong with that request. Please try again.