问题描述
对于可以获取String并输出HTML的小型系统,什么样的设计模式是合适的?
这是一个小例子:
public String makeStrong(String in){
return< strong> + in +< / strong>;
}
当然,它需要建模一些层次结构,以便它可以适合< ul>
和< ol>
及其子代。我正在考虑装饰器,但复合模式听起来也不错。我应该怎么考虑
使用 AutoCloseable
界面,一个简约的方法将如下:
程序打印
< body>< strong>一些文本< / strong>< ol>< li> a< / li>< li> b< / li>< li> c< / li>< / ol>< / body> code>
pre>
public class Main {
public static void main(String args []){
页面page = new Page();
writeBody(page);
pages.show();
}
static void writeBody(页面){
try(BodyTag bodyTag = new BodyTag(page)){
writeText(page);
writeList(page);
}
}
static void writeText(页面){
try(StrongTag strongTag = new StrongTag(page)){
strongTag.append 一些文字);
}
}
static void writeList(页面){
try(ListTag list = new ListTag(page)){
//扭曲(char c = 97; c< 100; ++ c){
appendElem(page,String.valueOf(c)))的元素
的方式写a,b和c;
}
}
}
static void appendElem(页面,字符串elem){
try(ListElem li = new ListElem(page)){
li.append(elem);
}
}
}
class Page {
StringBuilder content = new StringBuilder();
void append(String text){
content.append(text);
}
void show(){
System.out.println(content);
}
}
抽象类标签实现AutoCloseable {
字符串名称;
页面内容;
标签(页面parent,String tagName){
name = tagName;
content = parent;
content.append(<);
content.append(name);
content.append(>);
}
public void append(String text){
content.append(text);
}
@Override
final public void close(){
content.append(< /);
content.append(name);
content.append(>);
}
}
final class StrongTag extends Tag {
public StrongTag(Page parent){
super(parent,strong );
}
}
final class BodyTag extends Tag {
public BodyTag(Page parent){
super(parent,body );
}
}
最终类ListTag扩展标签{
public ListTag(Page parent){
super(parent,ol );
}
}
final class ListElem extends Tag {
public ListElem(Page parent){
super(parent,li );
}
}
绝对不是笨蛋,但为了简单的使用,它可能是一个好的开始。
For a small system that can get a String and output an HTML, what design pattern would be suitable?
This is a small example:
public String makeStrong(String in) {
return "<strong>" + in + "</strong>";
}
Of course it needs to model some hierarchical structure so that it can fit <ul>
and <ol>
and their children. I'm thinking of Decorator but Composite pattern sounds good too. What should I consider?
A minimalistic approach would be the following, using the AutoCloseable
interface:
The program prints<body><strong> some text </strong><ol><li>a</li><li>b</li><li>c</li></ol></body>
Online demo
public class Main {
public static void main(String args[]) {
Page page = new Page();
writeBody(page);
page.show();
}
static void writeBody(Page page) {
try (BodyTag bodyTag = new BodyTag(page)) {
writeText(page);
writeList(page);
}
}
static void writeText(Page page) {
try (StrongTag strongTag = new StrongTag(page)) {
strongTag.append(" some text ");
}
}
static void writeList(Page page) {
try (ListTag list = new ListTag(page)) {
// A twisted way to write a, b and c as elements
for (char c=97; c<100; ++c) {
appendElem(page, String.valueOf(c));
}
}
}
static void appendElem(Page page, String elem) {
try (ListElem li = new ListElem(page)) {
li.append(elem);
}
}
}
class Page {
StringBuilder content = new StringBuilder();
void append(String text) {
content.append(text);
}
void show() {
System.out.println(content);
}
}
abstract class Tag implements AutoCloseable {
String name;
Page content;
Tag(Page parent, String tagName) {
name = tagName;
content = parent;
content.append("<");
content.append(name);
content.append(">");
}
public void append(String text) {
content.append(text);
}
@Override
final public void close() {
content.append("</");
content.append(name);
content.append(">");
}
}
final class StrongTag extends Tag {
public StrongTag(Page parent) {
super(parent, "strong");
}
}
final class BodyTag extends Tag {
public BodyTag(Page parent) {
super(parent, "body");
}
}
final class ListTag extends Tag {
public ListTag(Page parent) {
super(parent, "ol");
}
}
final class ListElem extends Tag {
public ListElem(Page parent) {
super(parent, "li");
}
}
Definitely not idiot-proof but for simple uses, it might serve as a good start.
这篇关于用于生成HTML标签的设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!