using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BuilderPattern
{
abstract class Builder
{
public abstract void makeTitle(String title);
public abstract void makeString(String str);
public abstract void makeItems(String[] items);
public abstract Object getResult();
}
class Director
{
private Builder builder;
public Director(Builder builder)
{
this.builder = builder;
}
public Object construct()
{
builder.makeTitle("Hello");
builder.makeString("Hi");
builder.makeItems(new string[] {
"Good Morning",
"Good Afternooon",
"Good Evening"
});
return builder.getResult();
}
}
class TextBuilder : Builder
{
private String _BL = "==========";
private String _LF = "\n";
private String _StartPointSquare = "▣";
private String _StartPointArrow = "→";
private String _FileName;
private String _FileExtension = ".txt";
private StreamWriter _StreamWriter;
private StringBuilder builder = new StringBuilder();
public override void makeTitle(string title)
{
_FileName = title + _FileExtension;
try
{
_StreamWriter = new StreamWriter(_FileName);
_StreamWriter.WriteLine(_BL);
_StreamWriter.WriteLine(title);
}
catch (IOException e)
{
e.StackTrace.ToString();
}
finally
{
//omitted
}
}
public override void makeString(string str)
{
_StreamWriter.Write(_StartPointSquare);
_StreamWriter.WriteLine(str);
}
public override void makeItems(string[] items)
{
foreach (String temp in items) _StreamWriter.WriteLine(_StartPointArrow + temp);
_StreamWriter.WriteLine(_BL);
}
public override object getResult()
{
_StreamWriter.Close();
return _FileName;
}
}
class HTMLBuilder : Builder
{
private String[] _ElementPrefix = { "<html>", "<head>", "<title>", "<body>", "<h1>", "<p>", "<ul>", "<li>" };
private String[] _ElementSuffix = { "</html>", "</head>", "</title>", "</body>", "</h1>", "</p>", "</ul>", "</li>" };
private String _FileName;
private String _FileExtension = ".html";
private StreamWriter _StreamWriter;
public override void makeTitle(string title)
{
_FileName = title + _FileExtension;
try
{
_StreamWriter = new StreamWriter(_FileName);
}
catch (IOException e)
{
e.StackTrace.ToString();
}
finally
{
//omitted
}
_StreamWriter.Write(_ElementPrefix[0] + _ElementPrefix[1] + _ElementPrefix[2]);
_StreamWriter.Write(title);
_StreamWriter.WriteLine(_ElementSuffix[2] + _ElementSuffix[1] + _ElementSuffix[3]);
_StreamWriter.Write(_ElementPrefix[4]);
_StreamWriter.Write(title);
_StreamWriter.WriteLine(_ElementSuffix[4]);
}
public override void makeString(string str)
{
_StreamWriter.Write(_ElementPrefix[5]);
_StreamWriter.Write(str);
_StreamWriter.WriteLine(_ElementSuffix[5]);
}
public override void makeItems(string[] items)
{
_StreamWriter.Write(_ElementPrefix[6]);
foreach (String temp in items) _StreamWriter.Write(temp);
_StreamWriter.WriteLine(_ElementSuffix[6]);
}
public override object getResult()
{
_StreamWriter.WriteLine(_ElementSuffix[3] + _ElementSuffix[0]);
_StreamWriter.Close();
return _FileName;
}
}
class BuilderPattern
{
public static void Main(String[] argv)
{
if (argv.Length != 1)
{
usage();
Environment.Exit(0);
}
if (argv[0].Equals("plain"))
{
Director director = new Director(new TextBuilder());
String result = (String)director.construct();
Console.WriteLine(result + " 생성");
}
else if (argv[0].Equals("html"))
{
Director director = new Director(new HTMLBuilder());
String result = (String)director.construct();
Console.WriteLine(result + " 생성");
}
else
{
usage();
Environment.Exit(0);
}
}
public static void usage()
{
Console.WriteLine("Usage : Main Plain.");
Console.WriteLine("Usage : Main HTML.");
}
}
}