/java/

Bridge Pattern

2014-09-16 21:24:19

Main.java

import bridge.formatter.JsonFormatter;
import bridge.formatter.JsonUpperCaseFormatter;
import bridge.formatter.XmlFormatter;
import bridge.product.Book;
import bridge.product.Product;
import bridge.product.Toy;

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {

        List

<product> products = new ArrayList&lt;&gt;();

        products.add(new Book(new JsonFormatter()));
        products.add(new Book(new JsonUpperCaseFormatter()));
        products.add(new Toy(new XmlFormatter()));
        products.add(new Book(new XmlFormatter()));
        products.add(new Toy(new JsonFormatter()));

        for (Product product : products) {
            System.out.println(
                    product.getFullDescription()
            );
        }
    }
}

Product.java

package bridge.product;

import bridge.formatter.Formatter;

public abstract class Product {

    protected Formatter description;

    protected Product(Formatter description) {
        this.description = description;
    }

    public abstract String getFullDescription();

}

Book.java

package bridge.product;

import bridge.formatter.Formatter;

public class Book extends Product {

    private String name = "Java";
    private String pages = "159";
    private String author = "Lorem Ipsum";

    public Book(Formatter description) {
        super(description);
    }

    @Override
    public String getFullDescription() {

        String desc = "";

        desc += description.formatRow("Name", name);
        desc += description.formatRow("Pages", pages);
        desc += description.formatRow("Author", author);

        return desc;
    }
}

Toy.java

package bridge.product;

import bridge.formatter.Formatter;

public class Toy extends Product {

    private String name = "Car";
    private String age = "3-16";
    private String type = "plastic";

    public Toy(Formatter description) {
        super(description);
    }

    @Override
    public String getFullDescription() {
        String desc = "";

        desc += description.formatRow("Name", name);
        desc += description.formatRow("Age", age);
        desc += description.formatRow("Type", type);

        return desc;
    }
}

Formatter.java

package bridge.formatter;

public interface Formatter {

    String formatRow(String key, String value);
}

JsonFormatter.java

package bridge.formatter;

public class JsonFormatter implements Formatter {

    @Override
    public String formatRow(String key, String value) {
        return key + ":" + value + ",";
    }
}

JsonUpperCaseFormatter.java

package bridge.formatter;

public class JsonUpperCaseFormatter implements Formatter {

    @Override
    public String formatRow(String key, String value) {
        return key.toUpperCase() + ":" + value + ",";
    }
}

XmlFormatter

package bridge.formatter;

public class XmlFormatter implements Formatter {

    @Override
    public String formatRow(String key, String value) {
        return "&lt;" + key + "&gt;" + value + "<!--&quot; + key + &quot;-->";
    }
}