Git Product home page Git Product logo

platypus's Introduction

Platypus Build Status

Platypus is a Java Mixins library. It is being heavily used in Minium.

A quick example:

public interface Car {
    String drive();
}

public interface Aircraft {
    String fly();
}

public interface Delorean extends Car, Aircraft { }

public class CarImpl implements Car {
    @Override public String drive() {
        return "It can drive";
    }
}

public class AircraftImpl implements Aircraft {
    @Override public String fly() {
        return "It can fly";
    }
}

@Test public void testDelorean() {
	MixinClass<Delorean> deloreanMixinClass = MixinClasses.create(Delorean.class);
	
	Delorean delorean = deloreanMixinClass.newInstance(new AbstractMixinInitializer() {
	    @Override
	    protected void initialize() {
	        implement(Car.class).with(new CarImpl());
	        implement(Aircraft.class).with(new AircraftImpl());
	    }
	});
	
	assertThat(delorean.drive(), equalTo("It can drive"));
	assertThat(delorean.fly(), equalTo("It can fly"));
}

Besides, Mixin interface and its default implementation, Mixin.Impl will allow maximum fun with complex mixins, where you can chain different interface calls using the .as(Class<?>) method.

For instance, check the delorean.as(Aircraft.class).fly() in the adaptation of the previous example:

public interface Car extends Mixin {
    String drive();
}

public interface Aircraft extends Mixin {
    String fly();
}

public class CarImpl extends Mixin implements Car {
    @Override public String drive() {
        return "It can drive";
    }
}

public class AircraftImpl extends Mixin implements Aircraft {
    @Override public String fly() {
        return "It can fly";
    }
}

@Test public void testDeloreanCarWithAircrafCapabilities() {
	MixinClass<Car> deloreanMixinClass = MixinClasses.create(Car.class, Aircraft.class);
	
	Car delorean = deloreanMixinClass.newInstance(new AbstractMixinInitializer() {
	    @Override
	    protected void initialize() {
	        implement(Car.class).with(new CarImpl());
	        implement(Aircraft.class).with(new AircraftImpl());
	    }
	});
	
	assertThat(delorean.drive(), equalTo("It can drive"));
	assertThat(delorean.as(Aircraft.class).fly(), equalTo("It can fly"));
}

platypus's People

Contributors

m4ktub avatar ruifigueira avatar thorin avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.