java 접기
한개의 폴더에서 실행됩니다.
Builder.java
public abstract class Builder {
public abstract void makeTitle (String title );
public abstract void makeString (String str );
public abstract void makeItems (String [] items );
public abstract void close ();
public abstract String getResult ();
}
메소드들은 문서에 개별항목을 추가하는 함수입니다.
Director.java
public class Director {
private Builder builder ;
public Director (Builder builder ){
this .builder = builder;
}
public void constructor (){
builder .makeTitle ("Greeting" );
builder .makeString ("아침과 낮에" );
builder .makeItems (new String []{
"좋은 아침입니다." ,
"안녕하세요."
});
builder .makeString ("밤에" );
builder .makeItems (new String []{
"안녕하세요" ,
"안녕히 주무세요" ,
"안녕히 계세요"
});
builder .close ();
}
}
여기서 Builder객체를 생성자에서 받고 있는데 추상 클래스인 Builder 클래스를 받는 이유는 Builder객체를 상속하고 있으면 어떤 객체를 받던지 교체하여 사용할 수 있기 때문입니다. 한마디로 Director클래스는 Builder 클래스의 하위 클래스를 모르고 있습니다. 모르기 때문에 Builder를 상속하고 있다면 교체하여서 사용할 수 있습니다.
TextBuilder.java
public class TextBuilder extends Builder {
StringBuilder strBuilder = new StringBuilder ();
public void makeTitle (String title ){
strBuilder .append ("========================================= \n " );
strBuilder .append (title+ " \n " );
strBuilder .append (" \n " );
}
public void makeString (String str ){
strBuilder .append (str+ " \n " );
strBuilder .append (" \n " );
}
public void makeItems (String [] items ){
for (int i = 0 ; i < items .length ; i++ ){
strBuilder .append (items[i]+ " \n " );
}
strBuilder .append (" \n " );
}
public void close (){
strBuilder .append ("========================================= \n " );
}
public String getResult (){
return strBuilder .toString ();
}
}
HtmlBuilder.java
public class HtmlBuilder extends Builder {
StringBuilder builder = new StringBuilder ();
public void makeTitle (String title ){
builder .append ("<h1>" + title+ "</h1> \n " );
}
public void makeString (String str ){
builder .append ("<p>" + str + "</p> \n " );
}
public void makeItems (String [] items ){
builder .append ("<ul>" );
for (String str : items)
builder .append ("<li>" + str+ "</li> \n " );
builder .append ("</ul>" );
}
public void close (){
}
public String getResult (){
return builder .toString ();
}
}
ConcreteBuilder의 객체를 Director에 등록시켜 만들어 놓은 메소드에 맞게 constructor에 실행됩니다.
Main.java
import java.io. * ;
public class Main {
public static void main (String [] args ) throws Exception {
BufferedReader bfr = new BufferedReader (new InputStreamReader (System .in ));
String text = bfr .readLine ();
if (! isHtml (text)){
TextBuilder textBuilder = new TextBuilder ();
printResult (textBuilder);
}else {
HtmlBuilder htmlBuilder = new HtmlBuilder ();
printResult (htmlBuilder);
}
}
public static void printResult (Builder builder ){
Director director = new Director (builder);
director .constructor ();
String result = builder .getResult ();
System .out .println (result);
}
public static Boolean isHtml (String html ){
return html .equals ("html" );
}
}
여기서 볼 중요한 점은 main함수는 Builder클래스의 존재를 모릅니다. 그냥 Director의 constructor를 실행함으로서 내부에서는 정확히 어떻게 돌아가는지 모르게 됩다.
상속을 통해서 각 클래스 사이에서 얕은 연결을 통해서 다형성을 만들 수 있게됩니다.
java 접기 접기
Builder.py
import abcclass Builder: __metaclass__ = abc.ABCMeta def setTitle (self , title): pass def setText (self , text): pass def setItems (self , items): pass def close (self ): pass def getResult (self ):
pass
Director.py
class Director: def __init__ (self , builder): self .builder = builder def constructor (self ): self .builder.setTitle("Greet" ) self .builder.setText("아침에는" ) self .builder.setItems(["goodMorning" , "좋은 아침입니다." , "좋아" ]) self .builder.setText("저녁에는" ) self .builder.setItems(["goodEvening" , "멋진 저녁입니다." , "좋아" ]) self .builder.close()HtmlDcoument.py
from Builder import Builderclass HtmlDocuments(Builder): def __init__ (self ): self .str = "" def setTitle (self , title): self .str += "<h1>" +title+"</h1> \n " def setText (self , text): self .str += "<p>" + text + "</p> \n " def setItems (self , items): self .str+="<ul>" for i, item in enumerate (items): self .str += "<li>" +str (i)+". " +item+"</li> \n " self .str += "</ul> \n " def close (self ): super ().close() def getResult (self ): return self .strTextDocument.py
from Builder import Builderclass TextDocuments(Builder): def __init (self ): self .str = "" def setTitle (self , title): self .str+="----------------------------" self .str += title+" \n " def setText (self , text): self .str += text + " \n " def setItems (self , items): for i, item in enumerate (items): self .str += "<li>" +i+". " +item+"</li> \n " def close (self ): super ().close() def getResult (self ): return self .strMain.py
from Director import Directorfrom HtmlDocuments import HtmlDocumentsfrom TextDocuments import TextDocumentsdef isHtml (order): return order == "html" def makeDocumeents (documents): director = Director(documents) director.constructor() print (documents.getResult())if __name__ == "__main__" : order = input () if isHtml(order): htmlDocuments = HtmlDocuments() makeDocumeents(htmlDocuments) else : TextDocuments = TextDocuments() makeDocumeents(TextDocuments)
접기