Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Design pattern chapter_3_template_pattern

799 views

Published on

Desingn Pattern in Ruby 3장 Template Pattern 발표
1월 4일(토),
강남 토즈

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Design pattern chapter_3_template_pattern

  1. 1. 루비디자인패턴:템플릿패턴 김대권 propellerheaven@gmail.com
  2. 2. 템플릿패턴
  3. 3. 시나리오 레포트생성기
  4. 4. HTMLReport 간단한월례보고서생성기 제목과본문필요 HTML포맷만잘출력
  5. 5. 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
  6. 6. Report클래스(cont.) report=Report.new report.output_report
  7. 7. 하지만현실은…
  8. 8. 다양한출력형식? 이렇게도해야하고 저렇게도해야하고 나중엔그렇게도해야할지도모르는
  9. 9. 월례리포트생성기 HTML포맷 Text포맷
  10. 10. Report클래스 일단은고치고보자
  11. 11. 수정된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
  12. 12. 회고 if문을사용한분기 모든처리를outputreport에서하고있음 뭔가이게아닌것같은데…모르겠다 …
  13. 13. 다시월례리포트생성기 HTML로보내주세요. Text로도보내주세요. RTF가필요해요. PostScript포맷이필요해요. …
  14. 14. 템플릿패턴
  15. 15. 레포트생성과정 1. Outputsanyheaderinformationrequiredbythespecificformat. 2. Outputthetitle. 3. Outputeachlineoftheactualreport. 4. Outputanytrailingstuffrequiredbytheformat.
  16. 16. outputreportMethod defoutput_report output_start output_head output_body_start output_body output_body_end output_end end
  17. 17. 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
  18. 18. 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')
  19. 19. 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
  20. 20. Report생성하기 report=HTMLReport.new report.output_report report=PlainTextReport.new report.output_report
  21. 21. 템플릿패턴클래스다이어그램
  22. 22. HookMethods 상속클래스에서오버라이드해서사용할수도있음 기본기능을제공
  23. 23. Report클래스 classReport ... defoutput_start end defoutput_body_start end ... defoutput_body_end end defoutput_end end end
  24. 24. PlainTextReport outputbodystart와outputbodyend는빈메소드 HTML레포트에서body태그가들어가는메소드
  25. 25. PlainTextReport classPlainTextReportReport defoutput_start end defoutput_head puts(****#{@title}****) puts end defoutput_line(line) puts(line) end defoutput_end end end
  26. 26. DuckTyping 오리같이생겼고, 꽥꽥대면 오리
  27. 27. 정적타입언어의비용 1. 타입선언 2. 프로그램이시스템에강하게묶이는문제
  28. 28. emptyMethod예제
  29. 29. Java예제 publicclassEmpty { publicbooleanisEmpty(Strings) { returns.length()==0; } }
  30. 30. Ruby예제 defempty?(s) s.length==0 end
  31. 31. RubyvsJava DuckTyping에서는length가있는모든클래스에서사용가능
  32. 32. UnitTest 테스트는어느언어에서건필수 xUnit
  33. 33. 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
  34. 34. 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
  35. 35. 함정 하나에서부터시작하기 이예제에서는HTML에서시작하기 모든경우를커버하려고하지말것
  36. 36. TemplatesintheWild
  37. 37. Webrick서버 classHelloServerWEBrick::GenericServer defrun(socket) socket.print('HelloTCP/IPworld') end end s=HelloServer.new(:Port=2000) trap(INT){s.shutdown} s.start
  38. 38. RubyinitializeMethod
  39. 39. Thankyou

×