SlideShare ist ein Scribd-Unternehmen logo
1 von 40
Downloaden Sie, um offline zu lesen
루비디자인패턴:템플릿패턴

김대권
propellerheaven@gmail.com
템플릿패턴
시나리오
레포트생성기
HTMLReport
간단한월례보고서생성기
제목과본문필요
HTML포맷만잘출력
Report클래스
classReport
definitialize
@title='MonthlyReport'
@text=['Thingsaregoing','really,reallywell.']
end
defoutput_report
puts('html')
puts('head')
puts(title#{@title}/title)
puts('/head')
puts('body')
@text.eachdo|line|
puts(p#{line}/p)
end
puts('/body')
puts('/html')
end
end
Report클래스(cont.)
report=Report.new
report.output_report
하지만현실은…
다양한출력형식?
이렇게도해야하고
저렇게도해야하고
나중엔그렇게도해야할지도모르는
월례리포트생성기
HTML포맷
Text포맷
Report클래스
일단은고치고보자
수정된Report클래스
defoutput_report(format)
ifformat==:plain
puts(***#{@title}***)
elsifformat==:html
puts('html')
puts('head')
puts(title#{@title}/title)
puts('/head')
puts('body')
else
raiseUnknownformat:#{format}
end
@text.eachdo|line|
ifformat==:plain
puts(line)
else
puts(p#{line}/p)
end
end
ifformat==:html
puts('/body')
puts('/html')
end
회고
if문을사용한분기
모든처리를outputreport에서하고있음
뭔가이게아닌것같은데…모르겠다
…
다시월례리포트생성기
HTML로보내주세요.
Text로도보내주세요.
RTF가필요해요.
PostScript포맷이필요해요.
…
템플릿패턴
레포트생성과정
1. Outputsanyheaderinformationrequiredbythespecificformat.
2. Outputthetitle.
3. Outputeachlineoftheactualreport.
4. Outputanytrailingstuffrequiredbytheformat.
outputreportMethod
defoutput_report
output_start
output_head
output_body_start
output_body
output_body_end
output_end
end
AbstractMethod
defoutput_start
raise'Calledabstractmethod:output_start'
end
defoutput_head
raise'Calledabstractmethod:output_head'
end
defoutput_body_start
raise'Calledabstractmethod:output_body_start'
end
defoutput_line(line)
raise'Calledabstractmethod:output_line'
end
defoutput_body_end
raise'Calledabstractmethod:output_body_end'
end
defoutput_end
raise'Calledabstractmethod:output_end'
end
HTMLReport
classHTMLReportReport
defoutput_start
puts('html')
end
defoutput_head
puts('head')
puts(title#{@title}/title)
puts('/head')
end
defoutput_body_start
puts('body')
end
defoutput_line(line)
puts(p#{line}/p)
end
defoutput_body_end
puts('/body')
end
defoutput_end
puts('/html')
TextReport
classPlainTextReportReport
defoutput_start
end
defoutput_head
puts(****#{@title}****)
puts
end
defoutput_body_start
end
defoutput_line(line)
putsline
end
defoutput_body_end
end
defoutput_end
end
end
Report생성하기
report=HTMLReport.new
report.output_report
report=PlainTextReport.new
report.output_report
템플릿패턴클래스다이어그램
HookMethods
상속클래스에서오버라이드해서사용할수도있음
기본기능을제공
Report클래스
classReport
...
defoutput_start
end
defoutput_body_start
end
...
defoutput_body_end
end
defoutput_end
end
end
PlainTextReport
outputbodystart와outputbodyend는빈메소드
HTML레포트에서body태그가들어가는메소드
PlainTextReport
classPlainTextReportReport
defoutput_start
end
defoutput_head
puts(****#{@title}****)
puts
end
defoutput_line(line)
puts(line)
end
defoutput_end
end
end
DuckTyping
오리같이생겼고,
꽥꽥대면
오리
정적타입언어의비용
1. 타입선언
2. 프로그램이시스템에강하게묶이는문제
emptyMethod예제
Java예제
publicclassEmpty
{
publicbooleanisEmpty(Strings)
{
returns.length()==0;
}
}
Ruby예제
defempty?(s)
s.length==0
end
RubyvsJava
DuckTyping에서는length가있는모든클래스에서사용가능
UnitTest
테스트는어느언어에서건필수
xUnit
Setup
classEmptyTestTest::Unit::TestCase
defsetup
@empty_string=''
@one_char_string='X'
@long_string='TheraininSpain'
@empty_array=[]
@one_element_array=[1]
@long_array=[1,2,3,4,5,6]
end
...
end
Test
classEmptyTestTest::Unit::TestCase
...
deftest_empty_on_strings
assertempty?(@empty_string)
assert!empty?(@one_char_string)
assert!empty?(@long_string)
end
deftest_empty_on_arrays
assertempty?(@empty_array)
assert!empty?(@one_element_array)
assert!empty?(@long_array)
end
end
함정
하나에서부터시작하기
이예제에서는HTML에서시작하기
모든경우를커버하려고하지말것
TemplatesintheWild
Webrick서버
classHelloServerWEBrick::GenericServer
defrun(socket)
socket.print('HelloTCP/IPworld')
end
end
s=HelloServer.new(:Port=2000)
trap(INT){s.shutdown}
s.start
RubyinitializeMethod
Thankyou

Weitere ähnliche Inhalte

Mehr von Daegwon Kim

Mehr von Daegwon Kim (11)

Docker Casual Talk #2 - Dockerizing newrelic-sysmond
Docker Casual Talk #2 - Dockerizing newrelic-sysmondDocker Casual Talk #2 - Dockerizing newrelic-sysmond
Docker Casual Talk #2 - Dockerizing newrelic-sysmond
 
Devfair kubernetes 101
Devfair kubernetes 101Devfair kubernetes 101
Devfair kubernetes 101
 
Docker introduction
Docker introductionDocker introduction
Docker introduction
 
도커(Docker) 메트릭스 & 로그 수집
도커(Docker) 메트릭스 & 로그 수집도커(Docker) 메트릭스 & 로그 수집
도커(Docker) 메트릭스 & 로그 수집
 
커맨드 라인 도구 활용하기 - zsh + oh-my-zsh, tmux, peco
커맨드 라인 도구 활용하기 - zsh + oh-my-zsh, tmux, peco커맨드 라인 도구 활용하기 - zsh + oh-my-zsh, tmux, peco
커맨드 라인 도구 활용하기 - zsh + oh-my-zsh, tmux, peco
 
Translation memory
Translation memoryTranslation memory
Translation memory
 
이미지 기반의 배포 패러다임 Immutable infrastructure
이미지 기반의 배포 패러다임 Immutable infrastructure이미지 기반의 배포 패러다임 Immutable infrastructure
이미지 기반의 배포 패러다임 Immutable infrastructure
 
Visualization and data mapping
Visualization and data mappingVisualization and data mapping
Visualization and data mapping
 
Movie explorer - Moplo! Introduction
Movie explorer - Moplo! IntroductionMovie explorer - Moplo! Introduction
Movie explorer - Moplo! Introduction
 
Ruby codemetric automation server - Putne Introduction
Ruby codemetric automation server - Putne IntroductionRuby codemetric automation server - Putne Introduction
Ruby codemetric automation server - Putne Introduction
 
Ruby for biginner - Don't be suprised
Ruby for biginner - Don't be suprisedRuby for biginner - Don't be suprised
Ruby for biginner - Don't be suprised
 

Design pattern chapter_3_template_pattern