java 접기
파일 구조
Class Diagram
//기능의 클래스 계층
Display.java
public class Display { private DisplayImpl displayImpl ; public Display (DisplayImpl displayImpl){ this . displayImpl = displayImpl ; } public void open (){ displayImpl .rawOpen() ; } public void print (){ displayImpl .rawPrint() ; } public void close (){ displayImpl .rawClose() ; } //template Pattern public void display (){ open() ; print() ; close() ; } }
이를 상속하고 있는 하위 계층
public class CountDisplay extends Display{ public CountDisplay (DisplayImpl display) { super (display); } //template Pattern public void countPrint (int count){ open(); for (int i=0 ; i <count; i++){ print(); } close(); } }
//구현의 클래스 계층
public abstract class DisplayImpl { public abstract void rawOpen (); public abstract void rawClose (); public abstract void rawPrint (); }
public class StringDisplayImpl extends DisplayImpl{ String names ; int limit ; public StringDisplayImpl (String names){ this .names = names; limit = names.getBytes().length ; } @Override public void rawOpen () { printLine(); } @Override public void rawPrint () { System.out .println("|" + names + "|" ); } @Override public void rawClose () { printLine(); } private void printLine (){ System.out .print("+" ); for (int i =0 ; i < limit ; i++) System.out .print("-" ); System.out .println("+" ); } }
Main.java
public class Main { public static void main (String[] args){ Display d1= new Display(new StringDisplayImpl("hi hi Bridge" )); Display d2 = new Display(new StringDisplayImpl("hi hi hi Bridge 2" )); CountDisplay countDisplay = new CountDisplay(new StringDisplayImpl("hello Harry Porter" )); d1.display(); d2.display(); countDisplay.display(); countDisplay.countPrint(5 ); } }//template Pattern이 곳곳에 사용되었다. 구현되어 있지 않은 것을 호출해주고 하위 클래스에 구현해준다음 사용되는 클래스에 생성장에 보내주어서 직접적으로 하위클래스에 구현한 코드가 실행될 수 있게 되었습니다.
Result
+------------+
|hi hi Bridge|
+------------+
+-----------------+
|hi hi hi Bridge 2|
+-----------------+
+------------------+
|hello Harry Porter|
+------------------+
+------------------+
|hello Harry Porter|
|hello Harry Porter|
|hello Harry Porter|
|hello Harry Porter|
|hello Harry Porter|
+------------------+
java 접기 Python 접기
파일 구조
Display.py
class Display: def __init__ (self , impl_display): self .impl_display = impl_display def basic_open (self ): self .impl_display.rawOpen() def basic_print (self ): self .impl_display.rawPrint() def basic_close (self ): self .impl_display.rawClose() def display (self ): self .basic_open() self .basic_print() self .basic_close()
countDisplay.py
from Display import Displayclass CountDisplay(Display): def __init__ (self , impl_display): super ().__init__ (impl_display) def countDisplay (self , count): self .basic_open() for i in range (0 , count): self .basic_print() self .basic_close()DisplayImpl
import abcclass Displayimpl: __metaclass__ = abc.ABCMeta def rawOpen (self ): pass def rawPrint (self ): pass def rawClose (self ): pass StringDisplayImpl
from Displayimpl import Displayimplclass StringDisplayimpl(Displayimpl): def __init__ (self , str): self .str = str self .limit = len (str) def rawOpen (self ): self .printLine() def rawPrint (self ): print ("|" +self .str+"|" ) def rawClose (self ): self .printLine() def printLine (self ): print ("+" , end ="" ) for i in range (self .limit): print ("-" , end ="" ) print ("+" )main.py
from StringDisplayimpl import StringDisplayimplfrom Display import Displayfrom CountDisplay import CountDisplayif __name__ == "__main__" : d1 = Display(StringDisplayimpl("hi hi Python Bridge" )) d1.display() d2 = Display(StringDisplayimpl("end Game......" )) d2.display() d3 = CountDisplay(StringDisplayimpl("it's End Game....." )) d3.display() d3.countDisplay(5 )result
Python 접기