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.

닷넷 개발자를 위한 패턴이야기

1,682 views

Published on

GoF 패턴의 원칙과 여러가지 패턴을 소개하고,
패턴들이 하나의 군을 이루어 활용되는 예를 설명한다.

Published in: Technology, Business
  • Be the first to comment

닷넷 개발자를 위한 패턴이야기

  1. 1. Pattern Story (for .NET Developers) YoungSu, Son [email_address] Microsoft MVP Devpia Architecture&Design Sysop Devpia .NET Framework 3.0 Sysop Samsung Electronics Home Solution Group
  2. 2. Agenda <ul><li>Pattern and Model with Etymon. </li></ul><ul><li>GoF 2 Principles. </li></ul><ul><li>Basic Patterns with .NET </li></ul><ul><ul><li>Strategy Pattern </li></ul></ul><ul><ul><li>Template Method Pattern </li></ul></ul><ul><ul><li>Component Configuration Pattern </li></ul></ul><ul><ul><li>Reflection Pattern </li></ul></ul><ul><ul><li>Pipe & Filter Pattern </li></ul></ul><ul><ul><li>Factory Pattern </li></ul></ul><ul><ul><li>Observer Pattern </li></ul></ul><ul><ul><li>Chain-of-Responsibility Pattern </li></ul></ul><ul><li>Relationship between Patterns. </li></ul><ul><li>Introduce to Pattern Explore & Books </li></ul>06/02/09 Devpia A&D EVA
  3. 3. Pattern & Model <ul><li>What’s the Design Patten? </li></ul><ul><li>Dessin </li></ul><ul><li>Pattern is Father! </li></ul><ul><li>The Definition of Design Pattern </li></ul><ul><li>Model is Mother </li></ul>06/02/09 Devpia A&D EVA
  4. 4. What’s the Design Pattern?
  5. 5. Dessin <ul><li>Dessin </li></ul><ul><ul><li>데셍하다 </li></ul></ul><ul><ul><li>살짝 밑그림을 그리는 작업 </li></ul></ul><ul><li>Design </li></ul><ul><ul><li>de = away </li></ul></ul><ul><ul><li>sign = Note </li></ul></ul><ul><li>완성품을 만들기 이전에 살짝 뼈대를 잡는 작업 </li></ul>
  6. 6. Pattern is Father <ul><li>Pattern 의 어원은 </li></ul><ul><li> Father( 아버지 ) 에서 파생 </li></ul><ul><li>비슷한 생김새 , 성격등을 </li></ul><ul><li>가지고 있음을 의미 </li></ul><ul><li>그럼 비슷한 문제들도 </li></ul><ul><li>비슷한 방법 (Pattern) 으로 </li></ul><ul><li>해결 가능 </li></ul>
  7. 7. The Definition of Design Pattern 좋은 S/W 의 밑 그림을 잘 그릴수 있는 지침 / 가이드라인
  8. 8. Model is Mother !!!
  9. 9. GoF 2 Principles <ul><li>Program to an interface , </li></ul><ul><li>not to an implementation </li></ul><ul><li>Favor Object Composition </li></ul><ul><li>over Class Inheritance </li></ul>06/02/09 Devpia A&D EVA
  10. 10. GoF 1 st Principle. Program to an interface, Not to an implementation. <ul><li>When you pick up your rental car.. </li></ul><ul><ul><li>Push the seat all the way back </li></ul></ul><ul><ul><li>Find an NPR station </li></ul></ul><ul><ul><li>Find the exit </li></ul></ul><ul><li>Read the Manual? </li></ul>
  11. 11. Oh, down to lock …
  12. 12. How to use a key …
  13. 13. Oh, you push the PRESS button …
  14. 14. Who actually needs this data?
  15. 15. Why you don ’ t read rental car manuals <ul><li>You know how to drive your car </li></ul><ul><li>All cars work basically the same way </li></ul><ul><li>Your rental car is a car </li></ul><ul><li>Therefore, you can drive your rental car </li></ul><ul><li>That is … </li></ul>The Power of Sameness
  16. 16. GoF 2 nd Principle Favor Object Composition over Class Inheritance Don’t use Inheritance?? Developer Only use Object Composition! A swindler
  17. 17. The Two Towers in Object World. Composition & Inheritance are Two Towers in OO World A Great Architect
  18. 18. Inheritance and Composition 구분 상속 객체 합성 스타일 White Box Black Box 장점 단순하고 사용하기 편리 . 구조 명확 코드 재활용 약한 결합력 동적 바인딩 가능 캡슐화 유지 단점 부모 클래스에 종속성 발생 유연성 감소 클래스 수 폭발적 증가 캡슐화 파괴 구조가 복잡 사용시 유의해야 함 객체 수 증가 UML 구조
  19. 19. Composition versus Inheritance. <ul><li>Interview </li></ul><ul><ul><li>Design Principles from Design Patterns </li></ul></ul><ul><li>Eric Gamma Say </li></ul><ul><ul><li>A common misunderstanding is that composition doesn’t use inheritance at all . </li></ul></ul><ul><li>Refers </li></ul><ul><li>http://www.artima.com/lejava/articles/designprinciplesP.html </li></ul>Eric Gamma
  20. 20. Types of Inheritance. Class Interface1 { ISomething() = 0; } Class Impl { ISomething() { .. do(); .. } } Subtyping Subclassing Interface1 +ISomething() Concrete Class1 +ISomething() Concrete Class2 +ISomething() Impl. +ISomething() Impl Inheritance +ISomething() Impl Inheritance +ISomething() A Great Architect Don’t use Subclassing
  21. 21. GoF 2 nd Principle. <ul><li>1990’s </li></ul><ul><ul><li>Favor Object Composition over Class Inheritance. </li></ul></ul><ul><li>2000’s </li></ul><ul><ul><li>Favor Object Composition (based SubTyping) over SubClassing. </li></ul></ul>
  22. 22. Principles of GoF Pattern 객체합성 상속 (subtyping) SortAlgorithm Sort() BubbleSort Sort() QuickSort Sort() UltraSort Sort() DataArray SortAlgorithm* m_pSort SetAlgorithm(SortAlgorithm* pSort) Sort()
  23. 23. Basic Patterns <ul><li>Core Components of SW Impl. </li></ul><ul><li>Strategy Pattern </li></ul><ul><li>Template Method Pattern </li></ul><ul><li>Component Configurator Pattern </li></ul><ul><li>Reflection Pattern </li></ul><ul><li>Pipe & Filter Pattern </li></ul><ul><li>Factory Pattern </li></ul><ul><li>Observer Pattern </li></ul><ul><li>Chain of Responsibility </li></ul>06/02/09 Devpia A&D EVA
  24. 24. Core Components of S/W Impl. 06/02/09 Devpia A&D EVA You must remember 3 Core Components! Common Part Configurable Part Variable Part Great Architect
  25. 25. Core Components of S/W Impl. 06/02/09 Devpia A&D EVA Configurable Part Variable Part Component Configurator Factory Strategy <<LINKS>> <<CREATES>> Common Part (Modularity) Log Security Transaction AOP
  26. 26. Strategy Pattern <ul><li>Intents </li></ul>06/02/09 Devpia A&D EVA satellite Super Computer Do you think that the same logic use between satellite and super computer?
  27. 27. Strategy Pattern in GoF <ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>//pSort 라는 인터페이스를 선언한다 </li></ul><ul><li>CSort *pSort; </li></ul><ul><li>if (GetSystemInfo() == Low) </li></ul><ul><li>{ </li></ul><ul><li> // 인공위성에서는 Bubble Sorting 사용 </li></ul><ul><li>pSort = new CBubble(); </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>// 슈퍼 컴퓨터에는 Quick Sorting 사용 </li></ul><ul><li>pSort = new CQuick(); </li></ul><ul><li>} </li></ul><ul><li>pSort->Sorting(); </li></ul><ul><li>delete pSort; </li></ul><ul><li>} </li></ul>06/02/09 Devpia A&D EVA CSort +Sorting() CBubble +Sorting() CQuick +Sorting()
  28. 28. Template Method Pattern <ul><li>Intents </li></ul>06/02/09 Devpia A&D EVA ODBC(JDBC) API Application ODBC (JDBC) Driver Manager MSSQL Driver Oracle Driver
  29. 29. Template Method Pattern in GoF 06/02/09 Devpia A&D EVA class CQueryTemplate { public void doQuery() { string dbCommand; dbCommand = FormatConnect(); dbCommand = FormantSelect(sql); } ... } void main() { //pQT 라는 인터페이스를 선언한다 CQueryTemplate *pQT; Sql sql = “select * from AA”; if (GetDBProductInfo() = Oracle) pQT = new COracleQT(); else pQT = new CSqlSvrQT(); pQt->doQuery(sql); delete pQt; } CQueryTemplate +doQuery() #FormatConnect() #FormatSelect() COracleQT #FormatConnect() #FormatSelect() CSqlSvrQT #FormatConnect() #FormatSelect() Inversion of Control (Hollywood Principle)
  30. 30. Component Configurator Pattern <ul><li>Intents </li></ul>06/02/09 Devpia A&D EVA Configuration File HTTP Protocol Text Based Log Component DES RSA XML Based Log Component FTP Protocol When Change Config File Info , Component Configuration (Attribute) dynamically Changes ! Great Architect
  31. 31. Component Configurator in POSA2 06/02/09 Devpia A&D EVA Component +Init() +Fini() +Suspend() +Resume() +Info() Core Component Core Component Component Repository Component Configurator
  32. 32. Component Configurator in POSA2 06/02/09 Devpia A&D EVA
  33. 33. Component Configurator Sample <ul><li><xml version =“1.0”> </li></ul><ul><li><Components> </li></ul><ul><li><!– Protocol 정보 선택 --> </li></ul><ul><li><Protocol> </li></ul><ul><li>HTTP </li></ul><ul><li></Protocol> </li></ul><ul><li><!– Log 정보 선택 --> </li></ul><ul><li><Log> </li></ul><ul><li>XML </li></ul><ul><li></Log> </li></ul><ul><li><!– 보안 알고리즘 선택 --> </li></ul><ul><li><SecuAlgorithm> </li></ul><ul><li>DES </li></ul><ul><li></SecuAltorithm> </li></ul><ul><li></Components> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>CMessage *pMessage = new CMessage(); </li></ul><ul><li>if ( GetComponentInfo(“Sort”) = HTTP ) </li></ul><ul><li> pMessage->Protocol = HTTP; </li></ul><ul><li>else </li></ul><ul><li> pMessage->Protocol = FTP; </li></ul><ul><li>if ( GetComponentInfo(“Log”) = XML ) </li></ul><ul><li> pMessage->Protocol = XML; </li></ul><ul><li>else </li></ul><ul><li> pMessage->Protocol = TEXT; </li></ul><ul><li>... </li></ul><ul><li>pMessage->Send(“Hello”); </li></ul><ul><li>delete pMessage; </li></ul><ul><li>} </li></ul>06/02/09 Devpia A&D EVA Advanced Topic Reflection
  34. 34. Component Configurator Example <ul><li>App.config/Web.config </li></ul>In App.config. <?xml version=&quot;1.0“ encoding=&quot;utf-8&quot; ?> <configuration>      <appSettings>         <add key=&quot;sqlCon&quot; value=&quot; 컨넥션 스트링 정의 &quot;/>      </appSettings> </configuration>  In C# Code. System.Configuration.ConfigurationSetings.AppSetting[“sqlCon”];
  35. 35. Reflection Pattern in POSA1 Component A UserInterface MetaObject A MetaObject B MOP Meta Level Base Level Component B uses uses uses uses modifies modifies provides access to further base-level components further meta-level components retrieves information
  36. 36. Reflection ModuleBuilder AssemblyModule = CreatedAssembly.DefineDynamicModule(&quot;MathModule&quot;,&quot;Math.dll&quot;); TypeBuilder MathType = AssemblyModule.DefineType(&quot;DoMath&quot;, TypeAttributes.Public | TypeAttributes.Class); System.Type [] ParamTypes = new Type[] {typeof(int),typeof(int) }; MethodBuilder SumMethod = MathType.DefineMethod(&quot;Sum&quot;, MethodAttributes.Public, typeof(int), ParamTypes); ParameterBuilder Param1 = SumMethod.DefineParameter(1,ParameterAttributes.In, &quot;num1&quot;); ParameterBuilder Param2 = SumMethod.DefineParameter(1,ParameterAttributes.In, &quot;num2&quot;); . . . MathType.CreateType(); return CreatedAssembly;
  37. 37. Reflection using System; using System.Reflection; ... public class EmitDemoTest { static void Main() { ... System.Type MathType = EmitAssembly.GetType(&quot;DoMath&quot;); object[] Parameters = new object [2]; Parameters[0] = (object) (5); Parameters[1] = (object) (9); object EmitObj = Activator.CreateInstance (MathType,false); object Result = MathType.InvokeMember(&quot;Sum&quot;, BindingFlags.InvokeMethod ,null,EmitObj,Parameters); Console.WriteLine(&quot;Sum of {0}+{1} is {2}&quot;, Parameters[0],Parameters[1],Result.ToString()); Console.ReadLine(); } }
  38. 38. Pipe & Filter Pattern in POSA1 Protocol Handler InputOutput Handler component->svc() io->receive_data() Filter::svc() parse() Filter::svc() perform() Filter::svc() log() pipe io component Protocol Pipeline svc() Read Request svc() Filter svc() Filter svc() parse() Parse Headers svc() parse() Perform Request svc() perform() Log Request svc() log()
  39. 39. Using Pipeline Perform Request Log Request HTTP/ 1.0 200 OK Date: Thu, 09 Oct 01:26:00 GMT Server: Eva/1.0 Last-Modified: Wed, 08 Oct 1997 .. Content-Length : 12345 Content-Type : text/html <HTML> <TITLE> Homepage </TITLE> .. </HTML> any.net - - [09/Oct/1997:01:26:00 -0500] “ GET /~jxh/home.html HTTP/1.0 Read Request GET /~jxh/hoem.html HTTP/1.0 Connection: Keep-Alive User-Agent: Mothra/0.1 Host: any.net Accept: image/gif, image/jpeg, */* Parse Request GET /users/jxh/public_thml/home.html HTTP /1.0 Parse Headers CONNECTION USER_AGENT HOST ACCEPT ACCEPT ACCEPT Keep-Alive Mothra/0.1 any.net image/gif image/jpeg */*
  40. 40. Pipe & Filter in WCF Services Runtime Layer Messaging Layer Behaviors Channels Instancing Behavior Security Channel TCP Transport Channel UDP Transport Channel Cross-Proc Transport Channel Queue Transport Channel HTTP Transport Channel Full Duplex Channel Reliable Messaging Channel Custom Channel Transaction Behavior CLR Type Integration Behavior Throttling Behavior Metadata Behavior Error Handling Behavior Concurrency Behavior Custom Behavior Security Channel HTTP Transport Channel TCP Transport Channel UDP Transport Channel Reliable Messaging Channel Custom Channel Instancing Behavior Custom Behavior Contract To Type Behavior
  41. 41. Factory Pattern <ul><li>Intents </li></ul>06/02/09 Devpia A&D EVA Main Function Based Coding Object Oriented Programming
  42. 42. Factory Pattern (Abstract Factory) <ul><li>Intents </li></ul>06/02/09 Devpia A&D EVA OO Design Advanced OO Design
  43. 43. Factory Pattern in GOF 06/02/09 Devpia A&D EVA void main() { CMessage *pMessage = new CMessage(); if ( GetComponentInfo(“Proto”) = HTTP ) pMessage->Protocol = HTTP; else pMessage->Protocol = FTP; if ( GetComponentInfo(“Log”) = XML ) pMessage->Protocol = XML; else pMessage->Protocol = TEXT; ... pMessage->Send(“Hello”); delete pMessage; } Abstract Factory +CrateProduct() Client Concrete Factory Abstract Product Concrete Product
  44. 44. Factory Pattern in GOF 06/02/09 Devpia A&D EVA class CFactory { CMessage* GetInstance() { CMessage *pMessage = new CMessage(); if ( GetComponentInfo(“Proto”) = HTTP ) pMessage->Protocol = HTTP; else pMessage->Protocol = FTP; if ( GetComponentInfo(“Log”) = XML ) pMessage->Protocol = XML; else pMessage->Protocol = TEXT; ... return pMessage; } } Abstract Factory +CrateProduct() Client Concrete Factory Abstract Product Concrete Product Reminds Component Configurator!!
  45. 45. 3 Foe Patterns 06/02/09 Devpia A&D EVA Configurable Part Variable Part Common Part (Modularity) Log Security Transaction Component Configurator Factory Strategy <<LINKS>> <<CREATES>>
  46. 46. Observer Pattern (Misconception) 06/02/09 Devpia A&D EVA Change Data Misconception!! Or Prejudices ( 편견 !) 1. Observation 2. Notification
  47. 47. Observer is Notifieer!! 06/02/09 Devpia A&D EVA Observer in Observer Pattern is Notifieer
  48. 48. Hollywood Principle 06/02/09 Devpia A&D EVA Applicants Manager Don’t Call us!!
  49. 49. Hollywood Principle 06/02/09 Devpia A&D EVA Applicants Manager Don’t Call us! We Call You!!!
  50. 50. Flow of Observer Pattern 06/02/09 Devpia A&D EVA Data Client (Type1) Client (Type2) Client (Type3) Manager Client (Source) Observer is Notifieer Change 1. Change 2. Change 3. Notification
  51. 51. Observer Pattern in GoF 06/02/09 Devpia A&D EVA Subject +Attach (Observer o) +Detach (Observer o) +Notify() Concrete Subject Observer +Update () Concrete Observer
  52. 52. Publisher-Subscriber Replication DB Publisher Change DB Subscriber DB Subscriber DB Subscriber Reference Reference Reference
  53. 53. With Today’s Patterns Configuration File App.config Web.config Client A Client B Overhead!! File I/O Load Get Info Change Config File Notifiy Handler
  54. 54. With Today’s Patterns Configuration File App.config Web.config Client A Load Get Info Change Config File Notifiy FileSystemWatcher .NotifyFilter Handler A Smart Developer
  55. 55. Chain of Responsibility <ul><li>Intents </li></ul>06/02/09 Devpia A&D EVA
  56. 56. Chain of Responsibility <ul><li>The Last Solution </li></ul><ul><ul><li>바퀴벌레 팩 </li></ul></ul><ul><ul><ul><li>http://ajujoa.com/2460506 </li></ul></ul></ul>06/02/09 Devpia A&D EVA
  57. 57. Chain of Responsibility in GoF 06/02/09 Devpia A&D EVA +successor Client Handler +HandleRequest() Concrete Handler
  58. 58. Chain Of Responsibilty (cont’d) <ul><li>Design Strategy </li></ul><ul><ul><li>The First Handler </li></ul></ul><ul><ul><ul><li>Consider Probability </li></ul></ul></ul><ul><ul><ul><li>In order of Frequency of Use, </li></ul></ul></ul><ul><ul><ul><li>Consist of Handler Sequences </li></ul></ul></ul><ul><ul><li>Role of The Last Handler </li></ul></ul><ul><ul><ul><li>My Parents </li></ul></ul></ul><ul><ul><ul><li>Solve every problems! </li></ul></ul></ul><ul><li>Known Use </li></ul><ul><ul><li>Exception Handling Mechanism </li></ul></ul>06/02/09 Devpia A&D EVA
  59. 59. Review <ul><li>Strategy </li></ul><ul><ul><li>Support Algorithm that Context Considers </li></ul></ul><ul><li>Template Method </li></ul><ul><ul><li>Strategy Pattern + Template </li></ul></ul><ul><li>Component Configurator & Reflection </li></ul><ul><ul><li>Metadata Based Programming </li></ul></ul><ul><li>Pipe & Filter Pattern </li></ul><ul><ul><li>Support Plug-in Functionality </li></ul></ul><ul><li>Observer </li></ul><ul><ul><li>Inversion of Control (IoC) </li></ul></ul><ul><li>Chain of Responsibility </li></ul><ul><ul><li>Exception Handling Mechanism </li></ul></ul>06/02/09 Devpia A&D EVA
  60. 60. Pattern Explore
  61. 61. How to Study Pattern? 06/02/09 Devpia A&D EVA
  62. 62. Architectural Pattern (POSA1) <ul><ul><li>Layer </li></ul></ul><ul><ul><li>Pipe & Filter </li></ul></ul><ul><ul><li>Broker </li></ul></ul><ul><ul><li>Master Slave </li></ul></ul><ul><ul><li>Client – Dispatcher </li></ul></ul><ul><ul><li>Forwarder-Receiver </li></ul></ul><ul><ul><li>Model – View </li></ul></ul><ul><ul><li>Controller </li></ul></ul><ul><ul><li>Presentation –Abstraction - Control </li></ul></ul><ul><ul><li>Reflection </li></ul></ul><ul><ul><li>Counted Pointer </li></ul></ul><ul><ul><li>Blackboard </li></ul></ul>06/02/09 Devpia A&D EVA
  63. 63. Patterns For Concurrent & Networked Object (POSA2) 06/02/09 Devpia A&D EVA
  64. 64. Patterns for Resource Management (POSA3) <ul><li>Resource Acquisition </li></ul><ul><ul><li>Lookup </li></ul></ul><ul><ul><li>Lazy Acquisition </li></ul></ul><ul><ul><li>Eager Acquisition </li></ul></ul><ul><ul><li>Partial Acquisition </li></ul></ul><ul><li>Resource Lifecycle </li></ul><ul><ul><li>Caching </li></ul></ul><ul><ul><li>Pooling </li></ul></ul><ul><ul><li>Coordinator </li></ul></ul><ul><ul><li>Resource Lifecycle Manager </li></ul></ul><ul><li>Resource Release </li></ul><ul><ul><li>Leasing </li></ul></ul><ul><ul><li>Evictor </li></ul></ul>06/02/09 Devpia A&D EVA
  65. 65. Refactoring & Anti-Patterns 06/02/09 Devpia A&D EVA
  66. 66. Various Patterns 06/02/09 Devpia A&D EVA
  67. 67. Various Patterns 06/02/09 Devpia A&D EVA
  68. 68. References <ul><li>Frank Buschmann et al, “ Pattern-Oriented Software Architecture : A System of Patterns ”, Volume 1, Wiley </li></ul><ul><li>Douglas Schmidt et al, “ Pattern-Oriented Software Architecture ”, Volume 2, Wiley </li></ul><ul><li>Eric Gamma et al, “ Design Patterns : Elements of Reusable Object Oriented Software ” , Addison Wesley </li></ul><ul><li>Pipe & Filter Pattern, http://www.codeproject.com/cs/design/PipesAndFilters.asp </li></ul><ul><li>Reflection Pattern, http://www.codeproject.com/csharp/introreflection.asp </li></ul>
  69. 69. References <ul><li>FileSystemWatcher.NotifyFilter </li></ul><ul><li>http://msdn2.microsoft.com/en-us/library/system.io.filesystemwatcher.notifyfilter(VS.80).aspx </li></ul><ul><li>Pattern Explore </li></ul><ul><li>http://www.codeplex.com/Wiki/View.aspx?ProjectName=guidanceExplorer </li></ul><ul><li>Devpia A&D Free Online Lecture </li></ul><ul><li>http://www.devpia.com/NewStudy/LectureList.aspx?ClassA=A&ClassB=05&ClassC=01 </li></ul>
  70. 70. Questions <ul><li>Give me Question </li></ul><ul><li> about Patterns!! </li></ul><ul><li>Devpia Architecture & Design Q&A http://www.devpia.com/Maeul/Contents/List.aspx?BoardID=4&MAEULNo=4 </li></ul>06/02/09 Devpia A&D EVA

×