GithubHelp home page GithubHelp logo

bender's Introduction

Bender

Bender is a framework to apply the visitor design pattern to abstract source entities.
The default project contains the implementation for Java beans.

To apply the visitor design pattern you only need to write a visitor class for the desired abstract source.

Bender Beans

Visitor design pattern for Java beans.

For the following Java bean:

public class Person {

    private String name;
    private int age;
    private List<Person> parents;

    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
}

You can write a visitor like:

public class PersonVisitor implements BenderVisitor {

    @Bender("this")
    public void visitPerson(@Bender("name") String name, @Bender("this.age") int age) {
        // Do something...
    }

    @Bender("this.parents")
    public PersonVisitor visitParents(@Bender("length") int parentsLength) {
        if (parentsLength > 0) {
              return new PersonVisitor();
        }
        return null;
    }

    @Override
    public void visitEnd() {
        // Do something...
    }
}

Note that method parameters are annotated relative to the method target property.
Visitors can be stacked to accept the method target property simply returning a new visitor in that method. If this property is a primitive array or an instance of Iterable interface, each object will accept the new visitor instance.

Bender XML

Visitor design pattern for XML streams.

public class PersonXmlVisitor implements BenderVisitor {
    
    @BenderXml("/person")
    public void visitPerson(@BenderXml("name") String name, @BenderXml("age") int age) {
        // Do something...
    }
    
    @BenderXml("person/parent")
    public void visitParent(@BenderXml("name") String name, @BenderXml("age") int age) {
        // Do something...
    }

    @Override
    public void visitEnd() {
        // Do something...
    }
}

To use the XML or any other bender source you can see the following code snippet:

BenderSession session = new BenderSession();
BenderXmlSource source = new BenderXmlSource(stream);
source.accept(new PersonXmlVisitor());

For detailed info and advanced features see the wiki pages.

bender's People

Contributors

ctr0 avatar

Watchers

 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.