Design Patterns for Concurrency
Total Page:16
File Type:pdf, Size:1020Kb
Assessing Design Patterns for Concurrency Fikre Leguesse Supervisor: Dr. Adrian Francalanza Department of Computer Science & Artificial Intelligence University of Malta May 2009 Submitted in partial fulfillment of the requirements for the degree of B.Sc. I.T. (Hons.) FACULTY OF INFORMATION AND COMMUNICATION TECHNOLOGY Declaration Plagiarism is defined as “the unacknowledged use, as one’s own work, of work of another person, whether or not such work has been published” (Regulations Governing Conduct at Examinations, 1997, Regulation 1 (viii), University of Malta). I the undersigned, declare that the Final Year Project report submitted is my work, except where acknowledged and referenced. I understand that the penalties for making a false declaration may include, but are not limited to, loss of marks; cancellation of examination results; enforced suspension of studies; or expulsion from the degree programme. Work submitted without this signed declaration will not be corrected, and will be given zero marks. (N.B. If the assignment is meant to be submitted anonymously, please sign this form and submit it to the Departmental Officer separately from the assignment). _____________________________ ____________________________ Student Name Signature ________________ ________________________________________________ Course Code Title of work submitted _________________ Date Assessing Design Patterns for Concurrency Fikre Leguesse B.Sc. I.T. (Hons) May 2009 Abstract In this report we address the lack of design patterns in the message passing concurrency setting by adapting existing design patterns from the object-oriented paradigm to Erlang, a language based on the message passing concurrency setting. Over the years, the object-oriented community has been reaping the benefits associated with the adoption of design patterns, such as design reusa- bility, design flexibility, and a common vocabulary for the articulation of design ideas. The ob- ject-oriented community has been a primary adopter of design patterns, and design patterns are often considered to belong exclusively to this paradigm. In this report we argue that is not the case. We attempt to show that the benefits associated with design patterns can also be achieved in a message passing concurrency setting by implementing a number of existing concurrency related patterns using Erlang. We then integrate these patterns in the design and implementation of a peer-to-peer file sharing application that serves as a case study in the analysis of the applicability and feasibility in the adoption of design patterns to this paradigm. i Acknowledgements I would like to thank my supervisor, Dr. Adrian Francalanza who guided me throughout the project. I am truly grateful for the tireless effort and dedication with which he has given himself to all my needs in the accomplishment of this task, and for the interest he has shown in my work. I also appreciate the constant encouragement and support that he has always shown. I would also like to thank my family for their moral support throughout this challenging year. It would not have been as pleasant as it turned out to be without them by my side. ii Table of Contents Assessing Design Patterns for Concurrency ..................................................................................... i 1. Introduction .............................................................................................................................. 1 1.1 Aims and Objectives ........................................................................................................ 1 1.2 Approach and Project Methodologies ............................................................................. 2 1.3 Dissertation Overview ..................................................................................................... 3 2. Background .............................................................................................................................. 5 2.1 Design Patterns ................................................................................................................ 6 2.1.1 Brief History .............................................................................................................. 6 2.1.2 Structure..................................................................................................................... 7 2.1.3 Benefits Associated with Design Patterns ................................................................. 8 2.1.4 Design Patterns for Concurrency ............................................................................... 9 2.2 General OO Concepts ...................................................................................................... 9 2.2.1 Objects and Classes ................................................................................................. 10 2.2.2 Encapsulation........................................................................................................... 10 2.2.3 Inheritance ............................................................................................................... 10 2.2.4 Polymorphism .......................................................................................................... 11 2.2.5 Object Oriented Concepts in Design Patterns ......................................................... 11 2.3 Concurrency ................................................................................................................... 11 2.3.1 Introduction ............................................................................................................. 12 2.3.2 Concurrency Models ................................................................................................ 13 2.4 Erlang ............................................................................................................................. 14 2.4.1 Introduction ............................................................................................................. 15 iii 2.4.2 History and Philosophy ............................................................................................ 15 2.4.3 Programming style ................................................................................................... 16 2.4.4 Sequential Erlang ..................................................................................................... 16 2.4.5 Data Types and Pattern Matching ............................................................................ 17 2.4.6 Concurrent Erlang .................................................................................................... 18 2.4.7 Tail-Recursion ......................................................................................................... 19 2.4.8 Maintaining state ..................................................................................................... 20 2.4.9 Erlang Behaviours ................................................................................................... 21 2.5 Object-Oriented concepts in Erlang ............................................................................... 22 2.5.1 Comparing Erlang to OOP ...................................................................................... 22 2.5.2 Conclusion ............................................................................................................... 25 2.6 Peer-to-Peer ................................................................................................................... 26 2.6.1 Introduction ............................................................................................................. 26 2.6.2 BitTorrent Protocol .................................................................................................. 27 2.7 Conclusion ..................................................................................................................... 28 3. Design Patterns: Design and Implementation as Erlang behaviours ...................................... 30 3.1 Active Object ................................................................................................................. 30 3.1.1 Intent ........................................................................................................................ 31 3.1.2 Context .................................................................................................................... 31 3.1.3 Structure and Dynamics ........................................................................................... 31 3.1.4 Behaviour Implementation ...................................................................................... 33 3.1.5 Usage ....................................................................................................................... 36 3.1.6 Conclusion ............................................................................................................... 37 3.2 Acceptor-Connector ....................................................................................................... 38 3.2.1 Intent ........................................................................................................................ 38 iv 3.2.2 Context .................................................................................................................... 38 3.2.3 Structure & Dynamics ............................................................................................. 38 3.2.4 Behaviour Implementation .....................................................................................