
How languages affect design patterns A comparison of Object-Oriented and Functional design patterns Victor Nascimento Bakke Thesis submitted for the degree of Master in Informatics: Programming and Systems Architecture 60 credits Department of Informatics Faculty of mathematics and natural sciences UNIVERSITY OF OSLO Spring 2021 How languages affect design patterns A comparison of Object-Oriented and Functional design patterns Victor Nascimento Bakke © 2021 Victor Nascimento Bakke How languages affect design patterns http://www.duo.uio.no/ Printed: Reprosentralen, University of Oslo Abstract Software design patterns are a common tool to solving a common set of problems, and are widely used in object-oriented programming. Likewise, functional programming has its own set of patterns and techniques that solve common problems in the functional programming space. In this study, we have examined eight design patterns from Gang of Four’s seminal work Design patterns: Elements of Reusable Object-Oriented Software, and how they apply to functional programming, which functional programming patterns can be used to implement these design patterns, and how applicable are they to an functional programming language. We implemented these patterns in the object-oriented programming language Java and the functional programming language Haskell. From this study, we found that while some design patterns are applicable to functional programming, some were not, and only a few are reasonably useful in an functional programming context. i ii Contents I Introduction1 1 Introduction3 1.1 Introduction to design patterns.................3 1.2 Motivation............................4 1.3 Problem Statement........................4 1.4 Goal................................5 1.5 Approach.............................5 1.6 Work Done............................5 1.7 Evaluation.............................5 1.8 Results...............................5 1.9 Conclusion............................6 1.10 Limitations............................6 1.11 Outline..............................6 II Background9 2 Programming paradigms 11 2.1 Introduction to programming paradigms............ 11 2.2 Object-Oriented Programming................. 12 2.3 Functional Programming..................... 12 2.4 Summary of programming paradigms.............. 14 3 Languages 15 3.1 Introduction to languages.................... 15 3.2 Static typing........................... 15 3.3 Java................................ 16 3.3.1 Inheritance........................ 16 3.3.2 Generics.......................... 17 3.4 Haskell............................... 18 3.4.1 Types........................... 20 3.4.2 Type classes........................ 22 3.4.3 Example data type.................... 22 3.4.4 Operators......................... 24 3.4.5 Purity........................... 25 3.4.6 Laziness in brief..................... 26 3.4.7 Language extensions................... 26 iii 3.5 Differences between the langauges............... 29 3.5.1 Objects vs. records.................... 29 3.5.2 Inheritance vs. subtyping................ 29 3.5.3 Lambda expressions vs. higher-order functions.... 30 3.5.4 Imperative style..................... 31 3.5.5 Design patterns vs. type classes etc........... 32 3.5.6 Eager vs. lazy...................... 32 3.6 Summary of languages...................... 32 4 Design Patterns 33 4.1 Introduction to design patterns................. 33 4.2 Object-Oriented Design Patterns................ 33 4.2.1 Abstract Factory..................... 33 4.2.2 Adapter.......................... 34 4.2.3 Command......................... 34 4.2.4 Composite......................... 34 4.2.5 Decorator......................... 35 4.2.6 Iterator.......................... 35 4.2.7 Prototype......................... 35 4.2.8 Strategy.......................... 35 4.3 Functional Patterns....................... 36 4.3.1 Newtype and smart constructor............. 36 4.3.2 Defunctionalization.................... 36 4.3.3 Type classes........................ 37 4.4 Summary of Design Patterns.................. 43 III Method 45 5 Methodology 47 5.1 Introduction to methodology.................. 47 5.2 Cases............................... 47 5.3 Metrics.............................. 47 5.3.1 Introduction....................... 47 5.3.2 Example.......................... 48 5.3.3 Source Lines of code................... 50 5.3.4 Cyclomatic complexity.................. 52 5.3.5 Execution time...................... 52 5.3.6 Cyclomatic complexity density............. 53 5.3.7 Code............................ 53 5.4 Summary of methodology.................... 53 IV Cases 55 6 Cases 57 6.1 Introduction to cases....................... 57 6.2 Abstract Factory......................... 57 iv 6.2.1 Abstract Factory case.................. 57 6.2.2 Implementations for Abstract Factory......... 58 6.2.3 Metrics for Abstract Factory implementations..... 59 6.2.4 Comparison of Abstract Factory implementations... 62 6.3 Adapter.............................. 63 6.3.1 Adapter case....................... 63 6.3.2 Implementations for Adapter.............. 63 6.3.3 Metrics for Adapter implementations.......... 65 6.3.4 Comparison of Adapter implementations........ 69 6.4 Command............................. 70 6.4.1 Command case...................... 70 6.4.2 Implementations for Command............. 70 6.4.3 Metrics for Command implementations........ 70 6.4.4 Comparison of Command implementations...... 75 6.5 Composite............................. 76 6.5.1 Composite case...................... 76 6.5.2 Implementations for Composite............. 77 6.5.3 Metrics for Composite implementations........ 78 6.5.4 Comparison of Composite implementations...... 81 6.6 Decorator............................. 82 6.6.1 Decorator case...................... 82 6.6.2 Implementations for Decorator............. 82 6.6.3 Metrics for Decorator implementations......... 84 6.6.4 Comparison of Decorator implementations....... 88 6.7 Iterator.............................. 88 6.7.1 Iterator case....................... 89 6.7.2 Implementations for Iterator.............. 89 6.7.3 Metrics for Iterator implementations.......... 90 6.7.4 Comparison of Iterator implementations........ 92 6.8 Prototype............................. 93 6.8.1 Prototype case...................... 93 6.8.2 Implementations for Prototype............. 93 6.8.3 Metrics for Prototype implementations......... 95 6.8.4 Comparison of Prototype implementations....... 98 6.9 Strategy.............................. 98 6.9.1 Strategy case....................... 99 6.9.2 Implementations for Strategy.............. 99 6.9.3 Metrics for Strategy implementations......... 100 6.9.4 Comparison of Strategy implementations....... 103 6.10 Summary of cases......................... 103 V Discussion and conclusion 105 7 Analysis 107 7.1 Introduction............................ 107 7.2 Logical source lines of code summary.............. 107 7.3 Physical source lines of code summary............. 108 v 7.4 Cyclomatic complexity metric.................. 108 7.5 Execution time summary.................... 109 7.6 Cyclomatic complexity density summary............ 110 7.7 Summary of analysis....................... 110 8 Discussion 111 8.1 Introduction to discussion.................... 111 8.2 Case evaluation.......................... 111 8.2.1 Introduction....................... 111 8.2.2 Case differences...................... 111 8.2.3 Case bias......................... 112 8.3 Applicability........................... 113 8.3.1 Applicability of Abstract Factory............ 113 8.3.2 Applicability of Adapter................. 113 8.3.3 Applicability of Command................ 113 8.3.4 Applicability of Composite............... 114 8.3.5 Applicability of Decorator................ 114 8.3.6 Applicability of Iterator................. 114 8.3.7 Applicability of Prototype................ 115 8.3.8 Applicability of Strategy................. 115 8.4 Code size............................. 115 8.5 Complexity............................ 116 8.6 Discussion of limitations..................... 116 8.6.1 Master’s thesis...................... 116 8.6.2 Functional programming bias.............. 116 8.6.3 Code quality....................... 116 8.6.4 Subjectivity........................ 117 8.6.5 Inadequate metrics.................... 117 8.6.6 Small programming paradigm scope.......... 117 8.6.7 Standard library pollution................ 117 8.7 Summary of discussion...................... 117 9 Conclusion 119 9.1 Introduction to conclusion.................... 119 9.2 Highlights............................. 119 9.3 Summary of conclusion...................... 120 10 Future work 121 10.1 Introduction to future work................... 121 10.2 More languages.......................... 121 10.3 More design patterns....................... 121 10.4 Better metrics........................... 121 10.5 Summary of future work..................... 121 Bibliography 123 vi List of Figures 3.1 Function application operator example............. 24 3.2 Function composition operator example............ 25 6.1 UML class diagram for the Abstract Factory case....... 58 6.2 UML class diagram for the Adapter case............ 64 6.3 UML class diagram for the Command case........... 71 6.4 UML class diagram for the Composite case........... 77 6.5 UML class diagram for the Decorator case........... 83 6.6 UML class diagram for the Iterator case............. 89 6.7 UML class diagram for the Prototype case........... 94 6.8 UML class diagram for the Strategy case...........
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages140 Page
-
File Size-