Discuss / Java / 使用Abstract Factory模式实现HtmlDocument和WordDocument(创建html\word不齐全,仅演示如何使用这个设计模式)

使用Abstract Factory模式实现HtmlDocument和WordDocument(创建html\word不齐全,仅演示如何使用这个设计模式)

Topic source

净净一隅

#1 Created at ... [Delete] [Delete and Lock User]

package com;

import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.time.LocalDateTime;

public class FastHtmlDocument implements HtmlDocument {
private String text = "";

public FastHtmlDocument(String text) {
this.text = text;
}

@Override
public String toHtml() {
System.out.println(text);
StringBuilder sb=new StringBuilder();
sb.append("<html>");
sb.append("<head>");
sb.append("<title>");
sb.append("htmltitle");
sb.append(System.currentTimeMillis());
sb.append("</title>");
sb.append("</head>");
sb.append("<!-- this is a html created by Fast provider,at "\+ LocalDateTime.now()+ "-->");
sb.append("<body>");
sb.append(this.text);
sb.append("</body>");
sb.append("</html>");
return sb.toString();
}

@Override
public void save(Path path) throws IOException {
byte[] buffer=toHtml().getBytes(StandardCharsets.UTF_8);
try(OutputStream out= new FileOutputStream(path.toAbsolutePath().toString())) {
out.write(buffer);
}
System.out.println("使用Fast供应商的html产品,保存到" + path.toAbsolutePath());
}
}

package com;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;

public class FastWordDocument implements WordDocument{
private String text="";
public FastWordDocument(String text){
this.text=text;
}

@Override
public XWPFDocument toDocx() throws IOException {
XWPFDocument doc = new XWPFDocument(); //创建word文件
XWPFParagraph p1 = doc.createParagraph(); //创建段落
XWPFRun r1 = p1.createRun(); //创建段落文本
r1.setText(this.text); //设置文本
r1.setBold(true);
r1.setColor("000000");
r1.setFontSize(20);
p1=doc.createParagraph();
r1=p1.createRun();
r1.setText("this is a word created by Fast provider");
r1.setItalic(true);
r1.setColor("000000");
r1.setFontSize(16);
return doc;
}

@Override
public void save(Path path) throws IOException {
try( FileOutputStream out = new FileOutputStream(path.toAbsolutePath().toString()))
{//创建输出流
XWPFDocument doc=toDocx();
doc.write(out); //输出
System.out.println("使用Fast供应商的word产品,保存到"+path.toAbsolutePath());
}
}
}

package com;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.time.LocalDateTime;

public class GoodHtmlDocument implements HtmlDocument{
private String text="";
public GoodHtmlDocument(String text){
this.text=text;
}
@Override
public String toHtml() {
System.out.println(text);
StringBuilder sb=new StringBuilder();
sb.append("<html>");
sb.append("<head>");
sb.append("<title>");
sb.append("html title");
sb.append(System.currentTimeMillis());
sb.append("</title>");
sb.append("</head>");
sb.append("<!-- this is a html created by Good provider,at "\+ LocalDateTime.now()+ "-->");
sb.append("<body>");
sb.append(text);
sb.append("</body>");
sb.append("</html>");
return sb.toString();
}

@Override
public void save(Path path) throws IOException {
byte[] buffer=toHtml().getBytes(StandardCharsets.UTF_8);
try(OutputStream out= new FileOutputStream(path.toAbsolutePath().toString())) {
out.write(buffer);
}
System.out.println("使用Good供应商的html产品,保存到" + path.toAbsolutePath());
}
}

package com;

import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;

public class GoodWordDocument implements WordDocument{
private String text;
public GoodWordDocument(String text){
this.text=text;
}

@Override
public XWPFDocument toDocx() {
XWPFDocument doc = new XWPFDocument(); //创建word文件
XWPFParagraph p1 = doc.createParagraph(); //创建段落
XWPFRun r1 = p1.createRun(); //创建段落文本
r1.setText(this.text); //设置文本
r1.setBold(true);
r1.setColor("000000");
r1.setFontSize(18);
p1=doc.createParagraph();
r1=p1.createRun();
r1.setText("this is a word created by Good provider");
r1.setUnderline(UnderlinePatterns.DASH);
r1.setItalic(true);
r1.setColor("000000");
r1.setFontSize(13);
return doc;
}

@Override
public void save(Path path) throws IOException {
try( FileOutputStream out = new FileOutputStream(path.toAbsolutePath().toString()))
{//创建输出流
XWPFDocument doc=toDocx();
doc.write(out); //输出
System.out.println("使用Good供应商的word产品,保存到"+path.toAbsolutePath());
}
}
}

package com;

import java.io.IOException;
import java.nio.file.Path;

public interface HtmlDocument {
public String toHtml();
public void save(Path path) throws IOException;
}

package com;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import java.io.IOException;
import java.nio.file.Path;

public interface WordDocument {
XWPFDocument toDocx() throws IOException;
void save(Path path) throws IOException;
}

净净一隅

#2 Created at ... [Delete] [Delete and Lock User]

package com;
public interface AbstractFactory {
HtmlDocument createHtml(String text);
WordDocument createWord(String text);

public static AbstractFactory createFactory(String factoryName){
if("fast".equalsIgnoreCase(factoryName)){
return new FastFactory();
}
if("good".equalsIgnoreCase(factoryName)){
return new GoodFactory();
}
return null;
}
}

package com;
import java.nio.file.Path;
public class FastFactory implements AbstractFactory{
@Override
public HtmlDocument createHtml(String text) {
return new FastHtmlDocument(text);
}
@Override
public WordDocument createWord(String text) {
return new FastWordDocument(text);
}
}

package com;
public class GoodFactory implements AbstractFactory{
@Override
public HtmlDocument createHtml(String text) {
return new GoodHtmlDocument(text);
}
@Override
public WordDocument createWord(String text) {
return new GoodWordDocument(text);
}
}

pom.xml

<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.0.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>5.0.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-examples</artifactId> <version>5.0.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-excelant</artifactId> <version>5.0.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.0.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>ooxml-schemas</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>org.apache.xmlbeans</groupId> <artifactId>xmlbeans</artifactId> <version>5.0.1</version> </dependency>

package com;
import java.io.IOException;
import java.nio.file.Paths;
public class Application {
public static void main(String[] args) throws IOException {
/*
try
{System.out.println(LocalDateFactory.fromInt(99991031));}
catch (IllegalArgumentException e){
e.printStackTrace();
}*/
/*
AbstractFactory ff=new GoodFactory();
HtmlDocument fh=ff.createHtml("html ss");
fh.toHtml();
fh.save(Paths.get(".","fast.html"));
WordDocument wd=ff.createWord("word aaa");
wd.save(Paths.get(".","fast.docx"));
*/
AbstractFactory af=AbstractFactory.createFactory("fast");
HtmlDocument hd=af.createHtml("<a href=\"https://www.liaoxuefeng.com\">This is a link</a>");
hd.save(Paths.get(".","fast"+"_"+System.currentTimeMillis()+".html"));
WordDocument wd=af.createWord("word word word");
wd.save(Paths.get(".","fast"+"_"+System.currentTimeMillis()+".docx"));

af=AbstractFactory.createFactory("good");
hd=af.createHtml("<p>This is a html created by Good provider.</p><h1>welcome!</h1>");
hd.save(Paths.get(".","good_"+System.currentTimeMillis()+".html"));
wd=af.createWord("word ssss");
wd.save(Paths.get(".","good"+"_"+System.currentTimeMillis()+".docx"));

}
}

🌙

#3 Created at ... [Delete] [Delete and Lock User]

这么死板么?换个产品不可以?


  • 1

Reply