Last active
December 22, 2016 23:38
-
-
Save yateam/84bc55cf66feff8445cb34e8086ad102 to your computer and use it in GitHub Desktop.
Html Builder pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package template; | |
public class ContentBuilder extends ElementBuilder { | |
private String content; | |
public ContentBuilder(String content) { | |
this.content = content; | |
} | |
@Override | |
public String build() { | |
return content; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package template; | |
import java.util.function.Consumer; | |
public class DivBuilder extends ElementBuilder { | |
public DivBuilder addDiv(Consumer<DivBuilder> consumer) { | |
DivBuilder divBuilder = new DivBuilder(); | |
builders.add(divBuilder); | |
consumer.accept(divBuilder); | |
return this; | |
} | |
public DivBuilder addTable(Consumer<TableBuilder> consumer) { | |
TableBuilder tableBuilder = new TableBuilder(); | |
builders.add(tableBuilder); | |
consumer.accept(tableBuilder); | |
return this; | |
} | |
public DivBuilder addContent(String content) { | |
builders.add(new ContentBuilder(content)); | |
return this; | |
} | |
@Override | |
public String build() { | |
return "<div>" + super.build() + "</div>"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package template; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public abstract class ElementBuilder { | |
protected List<ElementBuilder> builders = new ArrayList<>(); | |
private Map<String, String> properties = new HashMap<>(); | |
public void addProperty(String name, String value) { | |
properties.put(name, value); | |
} | |
public String build() { | |
StringBuilder b = new StringBuilder(); | |
builders.forEach(builder -> b.append(builder.build())); | |
return b.toString(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package template; | |
import java.util.function.Consumer; | |
public class HtmlBuilder extends ElementBuilder { | |
public HtmlBuilder addDiv(Consumer<DivBuilder> consumer) { | |
DivBuilder divBuilder = new DivBuilder(); | |
builders.add(divBuilder); | |
consumer.accept(divBuilder); | |
return this; | |
} | |
public HtmlBuilder addTable(Consumer<TableBuilder> consumer) { | |
TableBuilder tableBuilder = new TableBuilder(); | |
builders.add(tableBuilder); | |
consumer.accept(tableBuilder); | |
return this; | |
} | |
public String build() { | |
StringBuilder b = new StringBuilder("<html><body>"); | |
b.append(super.build()); | |
b.append("</body><html>"); | |
return b.toString(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<div> | |
<div>TEST_CONTENT</div> | |
</div> | |
<table> | |
<tr> | |
<td>cell1</td> | |
</tr> | |
<tr> | |
<td>cell2</td> | |
</tr> | |
<tr> | |
<td>cell3</td> | |
</tr> | |
</table> | |
</body> | |
<html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package template; | |
import java.util.function.Consumer; | |
public class TableBuilder extends ElementBuilder { | |
public TableBuilder addRow(Consumer<TableRowBuilder> consumer) { | |
TableRowBuilder builder = new TableRowBuilder(); | |
builders.add(builder); | |
consumer.accept(builder); | |
return this; | |
} | |
@Override | |
public String build() { | |
return "<table>" + super.build() + "</table>"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package template; | |
import java.util.function.Consumer; | |
public class TableCellBuilder extends ElementBuilder { | |
public TableCellBuilder addDiv(Consumer<DivBuilder> consumer) { | |
DivBuilder divBuilder = new DivBuilder(); | |
builders.add(divBuilder); | |
consumer.accept(divBuilder); | |
return this; | |
} | |
public TableCellBuilder addTable(Consumer<TableBuilder> consumer) { | |
TableBuilder tableBuilder = new TableBuilder(); | |
builders.add(tableBuilder); | |
consumer.accept(tableBuilder); | |
return this; | |
} | |
public TableCellBuilder addContent(String content) { | |
builders.add(new ContentBuilder(content)); | |
return this; | |
} | |
@Override | |
public String build() { | |
return "<td>" + super.build() + "</td>"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package template; | |
import java.util.function.Consumer; | |
public class TableRowBuilder extends ElementBuilder { | |
public TableRowBuilder addCell(Consumer<TableCellBuilder> consumer) { | |
TableCellBuilder builder = new TableCellBuilder(); | |
builders.add(builder); | |
consumer.accept(builder); | |
return this; | |
} | |
@Override | |
public String build() { | |
return "<tr>" + super.build() + "</tr>"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import template.HtmlBuilder; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class TestClass { | |
public static void main(String[] args) { | |
List<String> cells = new ArrayList<>(); | |
cells.add("cell1"); | |
cells.add("cell2"); | |
cells.add("cell3"); | |
String result = new HtmlBuilder() | |
.addDiv(divBuilder -> { | |
divBuilder.addDiv(divBuilder1 -> { | |
divBuilder1.addContent("TEST_CONTENT"); | |
}); | |
}) | |
.addTable(tableBuilder -> { | |
cells.forEach(cellText -> { | |
tableBuilder.addRow(tableRowBuilder -> { | |
tableRowBuilder.addCell(tableCellBuilder -> { | |
tableCellBuilder.addContent(cellText); | |
}); | |
}); | |
}); | |
}) | |
.build(); | |
System.out.println(result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment