728x90
fontFamily Package
package fontFamily;
public interface FontFamily {
public String toString();
}
package fontFamily;
public class Arial implements FontFamily{
public String toString(){
return "Arial";
}
}
package fontFamily;
public class Malgun implements FontFamily{
public String toString(){
return "Malgun";
}
}
fontSize Package
package fontSize;
public interface FontSize {
public String toString();
}
package fontSize;
public class Small implements FontSize {
public String toString(){
return "12px";
}
}
package fontSize;
public class Big implements FontSize{
public String toString(){
return "20px";
}
}
Text
import fontFamily.*;
import fontSize.*;
public abstract class Text {
String content;
FontFamily fontFamily;
FontSize fontSize;
abstract void prepare();
void write(){System.out.println("Writting text");}
void send(){System.out.println("Sending text\n");}
void setContent(String content){
this.content=content;
}
String getContent(){
return content;
}
public String toString(){
StringBuffer display = new StringBuffer();
display.append("----"+content+"----\n");
display.append(fontFamily+"\n");
display.append(fontSize+"\n");
return display.toString();
}
}
public class Hello extends Text{
TextFactory textFactory;
public Hello(TextFactory textFactory){this.textFactory = textFactory;}
void prepare(){
fontFamily=textFactory.createFontFamily();
fontSize=textFactory.createFontSize();
}
}
public class Bye extends Text{
TextFactory textFactory;
public Bye(TextFactory textFactory){this.textFactory=textFactory;}
public void prepare(){
fontFamily=textFactory.createFontFamily();
fontSize=textFactory.createFontSize();
}
}
TextFactory
import fontFamily.*;
import fontSize.*;
public interface TextFactory {
public FontFamily createFontFamily();
public FontSize createFontSize();
}
import fontFamily.*;
import fontSize.*;
public class ATextFactory implements TextFactory{
public FontFamily createFontFamily(){return new Malgun();}
public FontSize createFontSize(){return new Small();}
}
import fontFamily.*;
import fontSize.*;
public class BTextFactory implements TextFactory{
public FontFamily createFontFamily(){return new Arial();}
public FontSize createFontSize(){return new Big();}
}
TextStore
public abstract class TextStore {
public abstract Text createText(String type);
public Text orderText(String type){
Text text = createText(type);
System.out.println("Making a "+text.getContent()+" text card\n");
text.prepare();
text.write();
text.send();
return text;
}
}
public class ATextStore extends TextStore{
public Text createText(String type){
Text text = null;
ATextFactory aTextFactory = new ATextFactory();
if(type.equals("Hello")){
text = new Hello(aTextFactory);
text.setContent("Hello");
}else if(type.equals("Bye")){
text = new Bye(aTextFactory);
text.setContent("Bye");
}
return text;
}
}
public class BTextStore extends TextStore{
public Text createText(String type){
Text text = null;
BTextFactory bTextFactory = new BTextFactory();
if(type.equals("Hello")){
text = new Hello(bTextFactory);
text.setContent("Hello");
}else if(type.equals("Bye")){
text = new Bye(bTextFactory);
text.setContent("Bye");
}
return text;
}
}
Main
public class Main {
public static void main(String[] args){
ATextStore aTextStore = new ATextStore();
Text text = aTextStore.orderText("Hello");
System.out.println("We ordered Hello card");
System.out.println(text);
}
}
<실행결과>
Making a Hello text card
Writting text
Sending text
We ordered Hello card
----Hello----
Malgun
12px
728x90
'기타 > Java' 카테고리의 다른 글
[Design Pattern]Command Pattern (0) | 2021.11.16 |
---|---|
[Java]Design Pattern-Singleton (0) | 2021.11.03 |
[Java]Design Pattern-Decorator Pattern (0) | 2021.10.28 |
[Java]Design Pattern-Simple Factory (0) | 2021.10.28 |
[Java]Design Pattern-Factory Method (0) | 2021.10.25 |