ebook img

How Programming Languages Affect Design Patterns PDF

138 Pages·2016·5.2 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview How Programming Languages Affect Design Patterns

How Programming Languages Affect Design Patterns A comparative study of programming languages and design patterns Vladislav Georgiev Alfredov Master’s Thesis Autumn 2016 Abstract This thesis is an empirical study of the affects that Language Features, provided by Programming Languages on Design Pattern implementa- tions. The set of Design Patterns is mostly based on the ones described byfirstbooktodocumentDesignPatterns,namelytheGangofFourbook [31]. It examines comparatively two Programming Languages, Java and Python. In terms of comparing Language Features and Design Pattern implementationsinacrossthetwoProgrammingLanguages. Theexamin- ation is based on the Programming Languages’ documentation as well as theliteratureavailablewithregardstoDesignPatternimplementationsin these Programming Languages. Such cross language study, has not been conductedbeforeandthusitisanapproachtothetopicofDesignPatterns fromanovelperspective. Thethesisproposesaclassificationschemewith regards to Language Features in relation to Design Pattern. In addition tothatitproposesacategorizationschemeofLanguageFeatures,thatim- plement Design Patterns in Programming Languages. It also assesses the dependenceofDesignPatternsonspecificLanguageFeatures. This thesis also examines some of the observed effects of Design PatternsonProgrammingLanguages,bycomparativeexaminationoftwo languages. It also catalogs related Language Features’ development over time. LeadingtothedefinitionoftherelationshipbetweenDesignPatterns andProgrammingLanguagesasbidirectional. Andthetheory,explaining the consequences of changes to either Design Patterns or Programming Languages. 2 Contents I Introduction 1 1 Introduction 3 1.1 Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.2 BackgroundandDefinitionsofTherms . . . . . . . . . . . . 4 1.2.1 ProgrammingLanguage . . . . . . . . . . . . . . . . . 4 1.2.2 LanguageFeatures . . . . . . . . . . . . . . . . . . . . 4 1.2.3 DesignPattern . . . . . . . . . . . . . . . . . . . . . . 4 1.2.4 UMLExtension . . . . . . . . . . . . . . . . . . . . . . 5 1.3 Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.4 Goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.5 LanguageChoiceCriteria . . . . . . . . . . . . . . . . . . . . 6 1.6 WorkDone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.7 RelevantLiterature . . . . . . . . . . . . . . . . . . . . . . . . 7 1.8 Outline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.9 Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.10 Contributions . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.11 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2 Background 11 2.1 ResourcesOverview . . . . . . . . . . . . . . . . . . . . . . . 11 2.2 DesignPatternsandLanguageFeaturesScope . . . . . . . . 12 2.3 EvaluationCriteria . . . . . . . . . . . . . . . . . . . . . . . . 12 2.4 DesignPatternsbeingmissingLanguageFeatures . . . . . . 13 2.5 RelatedWork . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.5.1 Annotations . . . . . . . . . . . . . . . . . . . . . . . . 14 2.5.2 Metaprogramming . . . . . . . . . . . . . . . . . . . . 15 2.5.3 Programming Paradigm Affects on Design Pattern Implementations . . . . . . . . . . . . . . . . . . . . . 15 2.5.4 SerializationTechniques . . . . . . . . . . . . . . . . . 16 2.5.5 ModelingvsTypingAsTopicSuggestion . . . . . . . 16 2.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 3 II Analysis 17 3 ProgrammingLanguagesandDesignPatterns 21 3.1 Criteria . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 3.2 Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 3.2.1 Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 3.2.2 Python . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 3.2.3 OtherCandidates . . . . . . . . . . . . . . . . . . . . . 23 3.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 4 Python 25 4.1 PythonEnhancementProposals . . . . . . . . . . . . . . . . . 25 4.2 PythonMagicMethods . . . . . . . . . . . . . . . . . . . . . . 25 4.3 Metaclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 4.3.1 Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 4.3.2 UsingMetaclassestoImplementDesignPatterns . . 28 4.3.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 29 4.4 AbstractBaseClasses . . . . . . . . . . . . . . . . . . . . . . . 29 4.4.1 Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 4.4.2 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 30 4.5 Decorators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 4.5.1 DescriptionandExampleUse . . . . . . . . . . . . . . 31 4.5.2 DecoratorsversusDecoratorDesignPattern . . . . . 33 4.5.3 UseofDecoratorstoImplementDesignPatterns . . . 34 4.5.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 34 4.6 Iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 4.6.1 ImplementationDetails . . . . . . . . . . . . . . . . . 35 4.6.2 WhyusePythonIterators . . . . . . . . . . . . . . . . 36 4.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 4.8 ObjectCopying . . . . . . . . . . . . . . . . . . . . . . . . . . 37 4.8.1 DescriptionandExampleUse . . . . . . . . . . . . . . 37 4.8.2 ProblemsWiththeLanguageFeature . . . . . . . . . 39 4.8.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 40 4.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 5 Java 41 5.1 Cloneable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 5.1.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 43 5.2 Iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 5.2.1 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . 44 5.2.2 ElementsoftheImplementation . . . . . . . . . . . . 44 5.2.3 Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 5.2.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 46 5.3 Observer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 5.3.1 Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 4 5.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 6 Evaluation 49 6.1 BasicDesignPatterns . . . . . . . . . . . . . . . . . . . . . . . 49 6.1.1 OverviewofBasicDesignPatternsVersusLanguage Features . . . . . . . . . . . . . . . . . . . . . . . . . . 49 6.1.2 Interface . . . . . . . . . . . . . . . . . . . . . . . . . . 50 6.1.3 AbstractParentClass . . . . . . . . . . . . . . . . . . . 58 6.1.4 InterfaceComparedtoAbstractParentClass . . . . . 61 6.1.5 AccessorMethodsandPrivateMethods . . . . . . . . 65 6.1.6 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . 70 6.2 Decorator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 6.2.1 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . 72 6.3 Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 6.3.1 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . 75 6.4 Observer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 6.4.1 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . 77 6.5 ObjectDuplication . . . . . . . . . . . . . . . . . . . . . . . . 77 6.5.1 CopyMethodandCopyConstructor . . . . . . . . . 77 6.5.2 ShallowandDeepCopy . . . . . . . . . . . . . . . . . 78 6.5.3 Use of Object Duplication for Design Pattern Imple- mentation . . . . . . . . . . . . . . . . . . . . . . . . . 78 6.5.4 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . 80 6.6 LanguageFeaturesOverview . . . . . . . . . . . . . . . . . . 80 6.7 Resultsandcomparisonsgraphs . . . . . . . . . . . . . . . . 84 6.8 LifeCycleofaDesignPattern . . . . . . . . . . . . . . . . . . 85 6.9 ClassificationofLanguageFeatures . . . . . . . . . . . . . . . 86 6.9.1 Classification . . . . . . . . . . . . . . . . . . . . . . . 87 6.9.2 Invisible . . . . . . . . . . . . . . . . . . . . . . . . . . 87 6.9.3 Partial . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 6.9.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 88 6.10 BidirectionalRelationship . . . . . . . . . . . . . . . . . . . . 89 6.11 TheRippleEffectTheory . . . . . . . . . . . . . . . . . . . . . 89 6.12 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 7 Conclusion 93 Appendices 101 A LanguageFeatures 103 A.0.1 PythonMetaclass . . . . . . . . . . . . . . . . . . . . . 103 A.0.2 PythonDecorators . . . . . . . . . . . . . . . . . . . . 104 A.0.3 PythonCopy . . . . . . . . . . . . . . . . . . . . . . . 111 A.0.4 JavaIterator . . . . . . . . . . . . . . . . . . . . . . . . 112 A.0.5 JavaCloneable . . . . . . . . . . . . . . . . . . . . . . 114 5 A.0.6 JavaObservable . . . . . . . . . . . . . . . . . . . . . . 117 A.0.7 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . 118 6 List of Figures 1.1 UMLdiagramdepictingtheAssociationrelationship. . . . . 5 2.1 Venndiagramofthescopeofthismasterthesiswithrelation toDesignPatternsandLanguageFeatures. . . . . . . . . . . 12 4.1 DiagramofmetaclassesinPython . . . . . . . . . . . . . . . 27 4.2 A demonstration using Metaclasses to construct classes in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 4.3 A demonstration of the equivalent Python syntax to using MetaclassesinPython . . . . . . . . . . . . . . . . . . . . . . 27 4.4 AdemonstrationoftheMetaclasssyntaxinPython . . . . . 27 4.5 An implementation of the Singleton design pattern using metaclassesinPython . . . . . . . . . . . . . . . . . . . . . . 28 4.6 UsingthemetaclassinPython . . . . . . . . . . . . . . . . . . 29 4.7 UsingaMetaclass,byextendingitinPython . . . . . . . . . 29 4.8 Exception thrown, when the subclass has not implemented allabstractmethodsandtriestocreatenewobjectsinPython 30 4.9 ExampleofinstantiatingemptyAbstractBaseClassPython 30 4.10 Pythondecorators . . . . . . . . . . . . . . . . . . . . . . . . . 32 4.11 Functionassignment . . . . . . . . . . . . . . . . . . . . . . . 32 4.12 DecoratorDesignPatternusingpython . . . . . . . . . . . . 34 4.13 PythonDecoratorsfunctionassignment . . . . . . . . . . . . 34 4.14 Pythondecoratordefinitionforsingletonimplementation . . 35 4.15 Comparisonofiteratorusetofor-loopinPython . . . . . . . 37 4.16 AdemonstrationofobjectcopyinginPython . . . . . . . . . 38 4.17 AdemonstrationofoverridingthecopymethodinPython . 38 4.18 Cloneableexampleoverview . . . . . . . . . . . . . . . . . . 39 5.1 Javaoverridingclonemethod . . . . . . . . . . . . . . . . . . 42 5.2 Cloneableexampleoverview . . . . . . . . . . . . . . . . . . 43 5.3 Iteratoruse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 5.4 Equivalentforeachloop . . . . . . . . . . . . . . . . . . . . . 45 6.1 UML diagram of the Interface Design Pattern, and the Java implementation . . . . . . . . . . . . . . . . . . . . . . . . . . 51 6.2 JavaInterfacedefinition . . . . . . . . . . . . . . . . . . . . . 52 7 6.3 JavaInterfaceuse . . . . . . . . . . . . . . . . . . . . . . . . . 53 6.4 UML diagram of the Interface Design Pattern in Python, usingducktyping. . . . . . . . . . . . . . . . . . . . . . . . . 54 6.5 UML diagram of the Interface Design Pattern in Python, usinginheritance. . . . . . . . . . . . . . . . . . . . . . . . . . 55 6.6 InterfaceProviderusingNotImplementedErrorinPython . 55 6.7 Interface Provider using NotImplementedError for init in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 6.8 InterfaceimplementationusingtheabcmoduleinPython . 57 6.9 InterfaceclientimplementationinPython . . . . . . . . . . . 58 6.10 UMLdiagramoftheAbstractParentClassDesignPattern . 59 6.11 An example implementation of the Abstract Parent Class DesignPatterninJava . . . . . . . . . . . . . . . . . . . . . . 59 6.12 An example implementation of the Abstract Parent Class DesignPatterninPython . . . . . . . . . . . . . . . . . . . . . 60 6.13 AnexampleimplementationusingdefaultmethodsinJava . 61 6.14 UML diagram of Interfaces used in conjunction with Ab- stractParentClassesinJava. . . . . . . . . . . . . . . . . . . . 64 6.15 AnexampleimplementationofPrivateMethodsinJava . . . 65 6.16 AnexampleimplementationofAccessorMethodsinJava . . 65 6.17 An example implementation of the Private Method Design PatterninPython . . . . . . . . . . . . . . . . . . . . . . . . . 67 6.18 An example implementation of the Accessor Methods DesignPatternusingpropertyinPython . . . . . . . . . . . 68 6.19 An example of how the implementation of the Accessor Methods Design Pattern using property in Python would restrictaccesstovariables . . . . . . . . . . . . . . . . . . . . 69 6.20 ExtendedUMLdiagramoftheDecoratordesignpattern . . 71 6.21 UMLdiagramofPython’simplementationoftheDecorator designpattern . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 6.22 UMLdiagramoftheIteratordesignpattern . . . . . . . . . . 73 6.23 UML diagram of Java’s implementation of the Iterator designpattern . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 6.24 UML diagram of Python’s implementation of the Iterator designpattern . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 6.25 UMLdiagramoftheObserverDesignPattern . . . . . . . . 76 6.26 UMLdiagramoftheObserverDesignPatterninJava . . . . 76 6.27 UMLdiagramofthePrototypeDesignPattern . . . . . . . . 79 6.28 UMLdiagramofobjectduplicationinJava . . . . . . . . . . 79 6.29 UMLdiagramofthecopymethodsinPython . . . . . . . . . 79 6.30 DesignPatternadoptionpath . . . . . . . . . . . . . . . . . . 86 8

Description:
I want to thank my supervisor Eric Bartley Jul for valuable guidance, encouraging comments and default Oracle implementation [37] and documentation [49]. Also when writing Java or Python, .. for client side scripting, though since the introduction of Node.js (the. JavaScript run-time-environment [
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.