Pharo by Example pptx

352 417 1
Pharo by Example pptx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Pharo by Example Andrew P. Black Stéphane Ducasse Oscar Nierstrasz Damien Pollet with Damien Cassou and Marcus Denker Version of 2009-10-28 ii This book is available as a free download from http://PharoByExample.org. Copyright © 2007, 2008, 2009 by Andrew P. Black, Stéphane Ducasse, Oscar Nierstrasz and Damien Pollet. The contents of this book are protected under Creative Commons Attribution-ShareAlike 3.0 Unported license. You are free: to Share — to copy, distribute and transmit the work to Remix — to adapt the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license. • For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page: creativecommons.org/licenses/by-sa/3.0/ • Any of the above conditions can be waived if you get permission from the copyright holder. • Nothing in this license impairs or restricts the author’s moral rights. Your fair dealing and other rights are in no way affected by the above. This is a human-readable summary of the Legal Code (the full license): creativecommons.org/licenses/by-sa/3.0/legalcode Published by Square Bracket Associates, Switzerland. http://SquareBracketAssociates.org ISBN 978-3-9523341-4-0 First Edition, October, 2009. Cover art by Samuel Morello. Contents Preface ix I Getting Started 1 A quick tour of Pharo 3 1.1 Getting started . . . . . . . . . . . . . . . . . . 3 1.2 The World menu . . . . . . . . . . . . . . . . . 7 1.3 Sending messages. . . . . . . . . . . . . . . . . 8 1.4 Saving, quitting and restarting a Pharo session . . . . . . 9 1.5 Workspaces and Transcripts . . . . . . . . . . . . . 11 1.6 Keyboard shortcuts . . . . . . . . . . . . . . . . 12 1.7 The Class Browser . . . . . . . . . . . . . . . . 15 1.8 Finding classes . . . . . . . . . . . . . . . . . . 16 1.9 Finding methods . . . . . . . . . . . . . . . . . 18 1.10 Defining a new method. . . . . . . . . . . . . . . 20 1.11 Chapter summary. . . . . . . . . . . . . . . . . 25 2 A first application 27 2.1 The Lights Out game. . . . . . . . . . . . . . . . 27 2.2 Creating a new Package . . . . . . . . . . . . . . 28 2.3 Defining the class LOCell . . . . . . . . . . . . . . 29 2.4 Adding methods to a class. . . . . . . . . . . . . . 31 2.5 Inspecting an object . . . . . . . . . . . . . . . . 33 2.6 Defining the class LOGame . . . . . . . . . . . . . 34 2.7 Organizing methods into protocols . . . . . . . . . . 37 iv Contents 2.8 Let’s try our code . . . . . . . . . . . . . . . . . 40 2.9 Saving and sharing Smalltalk code. . . . . . . . . . . 43 2.10 Chapter summary. . . . . . . . . . . . . . . . . 47 3 Syntax in a nutshell 49 3.1 Syntactic elements . . . . . . . . . . . . . . . . 49 3.2 Pseudo-variables . . . . . . . . . . . . . . . . . 52 3.3 Message sends . . . . . . . . . . . . . . . . . . 53 3.4 Method syntax . . . . . . . . . . . . . . . . . . 54 3.5 Block syntax . . . . . . . . . . . . . . . . . . . 55 3.6 Conditionals and loops in a nutshell . . . . . . . . . . 56 3.7 Primitives and pragmas . . . . . . . . . . . . . . 58 3.8 Chapter summary. . . . . . . . . . . . . . . . . 58 4 Understanding message syntax 61 4.1 Identifying messages . . . . . . . . . . . . . . . 61 4.2 Three kinds of messages . . . . . . . . . . . . . . 63 4.3 Message composition . . . . . . . . . . . . . . . 65 4.4 Hints for identifying keyword messages . . . . . . . . 72 4.5 Expression sequences . . . . . . . . . . . . . . . 73 4.6 Cascaded messages . . . . . . . . . . . . . . . . 74 4.7 Chapter summary. . . . . . . . . . . . . . . . . 74 II Developing in Pharo 5 The Smalltalk object model 79 5.1 The rules of the model . . . . . . . . . . . . . . . 79 5.2 Everything is an Object . . . . . . . . . . . . . . . 79 5.3 Every object is an instance of a class . . . . . . . . . . 80 5.4 Every class has a superclass . . . . . . . . . . . . . 87 5.5 Everything happens by sending messages . . . . . . . . 91 5.6 Method lookup follows the inheritance chain . . . . . . . 92 5.7 Shared variables . . . . . . . . . . . . . . . . . 98 5.8 Chapter summary. . . . . . . . . . . . . . . . . 103 v 6 The Pharo programming environment 105 6.1 Overview . . . . . . . . . . . . . . . . . . . . 106 6.2 The Browser . . . . . . . . . . . . . . . . . . . 107 6.3 Monticello . . . . . . . . . . . . . . . . . . . 119 6.4 The Inspector and the Explorer . . . . . . . . . . . . 126 6.5 The Debugger . . . . . . . . . . . . . . . . . . 128 6.6 The Process Browser . . . . . . . . . . . . . . . . 137 6.7 Finding methods . . . . . . . . . . . . . . . . . 138 6.8 Change sets and the Change Sorter . . . . . . . . . . 138 6.9 The File List Browser . . . . . . . . . . . . . . . 141 6.10 In Smalltalk, you can’t lose code . . . . . . . . . . . 143 6.11 Chapter summary. . . . . . . . . . . . . . . . . 144 7 SUnit 147 7.1 Introduction . . . . . . . . . . . . . . . . . . . 147 7.2 Why testing is important . . . . . . . . . . . . . . 148 7.3 What makes a good test? . . . . . . . . . . . . . . 149 7.4 SUnit by example . . . . . . . . . . . . . . . . . 150 7.5 The SUnit cook book . . . . . . . . . . . . . . . . 154 7.6 The SUnit framework . . . . . . . . . . . . . . . 155 7.7 Advanced features of SUnit . . . . . . . . . . . . . 158 7.8 The implementation of SUnit. . . . . . . . . . . . . 159 7.9 Some advice on testing . . . . . . . . . . . . . . . 162 7.10 Chapter summary. . . . . . . . . . . . . . . . . 163 8 Basic Classes 165 8.1 Object . . . . . . . . . . . . . . . . . . . . . 165 8.2 Numbers . . . . . . . . . . . . . . . . . . . . 174 8.3 Characters . . . . . . . . . . . . . . . . . . . 177 8.4 Strings . . . . . . . . . . . . . . . . . . . . . 178 8.5 Booleans . . . . . . . . . . . . . . . . . . . . 179 8.6 Chapter summary. . . . . . . . . . . . . . . . . 181 vi Contents 9 Collections 183 9.1 Introduction . . . . . . . . . . . . . . . . . . . 183 9.2 The varieties of collections. . . . . . . . . . . . . . 184 9.3 Implementations of collections . . . . . . . . . . . . 186 9.4 Examples of key classes. . . . . . . . . . . . . . . 188 9.5 Collection iterators . . . . . . . . . . . . . . . . 197 9.6 Some hints for using collections. . . . . . . . . . . . 201 9.7 Chapter summary. . . . . . . . . . . . . . . . . 202 10 Streams 205 10.1 Two sequences of elements . . . . . . . . . . . . . 205 10.2 Streams vs. collections . . . . . . . . . . . . . . . 206 10.3 Streaming over collections. . . . . . . . . . . . . . 207 10.4 Using streams for file access . . . . . . . . . . . . . 215 10.5 Chapter summary. . . . . . . . . . . . . . . . . 217 11 Morphic 219 11.1 The history of Morphic . . . . . . . . . . . . . . . 219 11.2 Manipulating morphs . . . . . . . . . . . . . . . 221 11.3 Composing morphs . . . . . . . . . . . . . . . . 222 11.4 Creating and drawing your own morphs . . . . . . . . 222 11.5 Interaction and animation . . . . . . . . . . . . . . 226 11.6 Interactors . . . . . . . . . . . . . . . . . . . 229 11.7 Drag-and-drop . . . . . . . . . . . . . . . . . . 230 11.8 A complete example . . . . . . . . . . . . . . . . 232 11.9 More about the canvas . . . . . . . . . . . . . . . 236 11.10 Chapter summary. . . . . . . . . . . . . . . . . 237 12 Seaside by Example 239 12.1 Why do we need Seaside? . . . . . . . . . . . . . . 239 12.2 Getting started . . . . . . . . . . . . . . . . . . 240 12.3 Seaside components . . . . . . . . . . . . . . . . 244 12.4 Rendering XHTML . . . . . . . . . . . . . . . . 248 12.5 CSS: Cascading style sheets . . . . . . . . . . . . . 254 vii 12.6 Managing control flow . . . . . . . . . . . . . . . 256 12.7 A complete tutorial example . . . . . . . . . . . . . 263 12.8 A quick look at AJAX . . . . . . . . . . . . . . . 269 12.9 Chapter summary. . . . . . . . . . . . . . . . . 272 III Advanced Pharo 13 Classes and metaclasses 277 13.1 Rules for classes and metaclasses . . . . . . . . . . . 277 13.2 Revisiting the Smalltalk object model . . . . . . . . . . 278 13.3 Every class is an instance of a metaclass . . . . . . . . . 280 13.4 The metaclass hierarchy parallels the class hierarchy . . . . 281 13.5 Every metaclass Inherits from Class and Behavior . . . . . . 283 13.6 Every metaclass is an instance of Metaclass . . . . . . . . 286 13.7 The metaclass of Metaclass is an Instance of Metaclass . . . . 286 13.8 Chapter summary. . . . . . . . . . . . . . . . . 288 14 Reflection 289 14.1 Introspection . . . . . . . . . . . . . . . . . . 290 14.2 Browsing code . . . . . . . . . . . . . . . . . . 294 14.3 Classes, method dictionaries and methods . . . . . . . . 297 14.4 Browsing environments . . . . . . . . . . . . . . 299 14.5 Accessing the run-time context . . . . . . . . . . . . 300 14.6 Intercepting messages not understood . . . . . . . . . 303 14.7 Objects as method wrappers . . . . . . . . . . . . . 307 14.8 Pragmas . . . . . . . . . . . . . . . . . . . . 310 14.9 Chapter summary. . . . . . . . . . . . . . . . . 311 IV Appendices A Frequently Asked Questions 317 A.1 Getting started . . . . . . . . . . . . . . . . . . 317 A.2 Collections . . . . . . . . . . . . . . . . . . . 317 A.3 Browsing the system . . . . . . . . . . . . . . . . 318 A.4 Using Monticello and SqueakSource . . . . . . . . . . 320 viii Contents A.5 Tools . . . . . . . . . . . . . . . . . . . . . 321 A.6 Regular expressions and parsing . . . . . . . . . . . 321 Bibliography 323 Index 324 Preface What is Pharo? Pharo is a modern, open source, fully-featured implementation of the Smalltalk programming language and environment. Pharo is derived from Squeak 1 , a re-implementation of the classic Smalltalk-80 system. Whereas Squeak was developed mainly as a platform for developing experimental educational software, Pharo strives to offer a lean, open-source platform for professional software development, and a robust and stable platform for research and development into dynamic languages and environments. Pharo serves as the reference implementation for the Seaside web development framework. Pharo resolves some licensing issues with Squeak. Unlike previous ver- sions of Squeak, the Pharo core contains only code that has been contributed under the MIT license. The Pharo project started in March 2008 as a fork of Squeak 3.9, and the first 1.0 beta version was released on July 31, 2009. Although Pharo removes many packages from Squeak, it also includes numerous features that are optional in Squeak. For example, true type fonts are bundled into Pharo. Pharo also includes support for true block closures. The user interfaces has been simplified and revised. Pharo is highly portable — even its virtual machine is written entirely in Smalltalk, making it easy to debug, analyze, and change. Pharo is the vehicle for a wide range of innovative projects from multimedia applications and educational platforms to commercial web development environments. There is an important aspect behind Pharo: Pharo should not just be a copy of the past but really reinvent Smalltalk. Big-bang approaches rarely succeed. Pharo will really favor evolutionary and incremental changes. We want to 1 Dan Ingalls et al., Back to the Future: The Story of Squeak, a Practical Smalltalk Written in Itself. In Proceedings of the 12th ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications (OOPSLA’97). ACM Press, November 1997  URL: http: //www.cosc.canterbury.ac.nz/~wolfgang/cosc205/squeak.html  . x Preface be able to experiment with important new features or libraries. Evolution means that Pharo accepts mistakes and is not aiming for the next perfect solution in one big step — even if we would love it. Pharo will favor small incremental changes but a multitude of them. The success of Pharo depends on the contributions of its community. Who should read this book? This book is based on Squeak by Example 2 , an open-source introduction to Squeak. The book has been liberally adapted and revised to reflect the differ- ences between Pharo and Squeak. This book presents the various aspects of Pharo, starting with the basics, and proceeding to more advanced topics. This book will not teach you how to program. The reader should have some familiarity with programming languages. Some background with object-oriented programming would be helpful. This book will introduce the Pharo programming environment, the lan- guage and the associated tools. You will be exposed to common idioms and practices, but the focus is on the technology, not on object-oriented de- sign. Wherever possible, we will show you lots of examples. (We have been inspired by Alec Sharp’s excellent book on Smalltalk 3 .) There are numerous other books on Smalltalk freely available on the web but none of these focuses specifically on Pharo. See for example: http: //stephane.ducasse.free.fr/FreeBooks.html A word of advice Do not be frustrated by parts of Smalltalk that you do not immediately understand. You do not have to know everything! Alan Knight expresses this principle as follows 4 : 2 http://SqueakByExample.org 3 Alec Sharp, Smalltalk by Example. McGraw-Hill, 1997  URL: http://stephane.ducasse.free.fr/ FreeBooks/ByExample/  . 4 http://www.surfscranton.com/architecture/KnightsPrinciples.htm [...]... grow! For more details, visit http://PharoByExample.org The Pharo community The Pharo community is friendly and active Here is a short list of resources that you may find useful: • http://www .pharo- project.org is the main web site of Pharo • http://www.squeaksource.com is the equivalent of SourceForge for Pharo projects Many optional packages for Pharo live here Examples and exercises We make use of... command line.) Once Pharo is running, you should see a single large window, possibly containing some open workspace windows (see Figure 1.2), and it’s not 1 Pharo is derived from Squeak 3.9, and presently shares the VM with Squeak 2 http://PharoByExample.org Getting started 5 Figure 1.2: A fresh http://PharoByExample.org image obvious how to proceed! You might notice a menu bar, but Pharo mainly makes... too), and a changes file, which contains a log of all of the changes to the source code of the system In Figure 1.1, these files are called pharo. image and pharo. changes Download and install Pharo on your computer We recommend that you use the image provided on the Pharo by Example web page.2 Most of the introductory material in this book will work with any version, so if you already have one installed, you... the location where the original image and changes files were, you will find two new files called “myPharo.image” and 10 A quick tour of Pharo Figure 1.6: A BouncingAtomsMorph Figure 1.7: The save as dialogue “myPharo.changes” that represent the working state of the Pharo image at the moment before you told Pharo to Save and quit If you wish, you can move these two files anywhere that you like on your... copy or link to the virtual machine and the sources file Start up Pharo from the newly created “myPharo.image” file Now you should find yourself in precisely the state you were when you quit Pharo The BouncingAtomsMorph is there again and the atoms continue to bounce from where they were when you quit When you start Pharo for the first time, the Pharo virtual machine loads the image file that you provide This... answer all of these questions by giving an example: When we send the message shout to the string “Don’t panic” the result should be “DON’T PANIC!” To make this example into something that the system can use, we turn it into a test method: Method 1.1: A test for a shout method testShout self assert: ('Don''t panic' shout = 'DON''T PANIC!') 9 Kent Beck, Test Driven Development: By Example Addison-Wesley, 2003,... many examples as possible In particular, there are many examples that show a fragment of code which can be evaluated We xii Preface use the symbol −→ to indicate the result that you obtain when you select an expression and print it : 3+4 −→ 7 "if you select 3+4 and 'print it', you will see 7" In case you want to play in Pharo with these code snippets, you can download a plain text file with all the example. .. buttons A twobutton mouse works quite well with Pharo, but if you have only a singlebutton mouse, you should seriously consider buying a two-button mouse with a clickable scroll wheel: it will make working with Pharo much more pleasant 6 A quick tour of Pharo (a) The world menu (b) The contextual menu (c) The morphic halo Figure 1.3: The world menu (brought up by clicking), a contextual menu (action-clicking),... Macintosh without a second mouse button, you can simulate one by holding down the ⌘ key while clicking the mouse However, if you are going to be using Pharo at all often, we recommend investing in a mouse with at least two buttons You can configure your mouse to work the way you want by using the preferences of your operating system and mouse driver Pharo has some preferences for customising the mouse and... the various command keys 1.2 The World menu Click again on the Pharo background You will see the World menu again Most Pharo menus are not modal; you can leave them on the screen for as long as you wish by clicking the push pin icon in the top-right corner Do this The world menu provides you a simple means to access many of the tools that Pharo offers Have a closer look at the World and Tools menus . with Squeak. 2 http://PharoByExample.org Getting started 5 Figure 1.2: A fresh http://PharoByExample.org image. obvious how to proceed! You might notice a menu bar, but Pharo mainly makes use. details, visit http://PharoByExample.org. The Pharo community The Pharo community is friendly and active. Here is a short list of resources that you may find useful: • http://www .pharo- project.org. 1.1, these files are called pharo. image and pharo. changes. Download and install Pharo on your computer. We recommend that you use the image provided on the Pharo by Example web page. 2 Most of

Ngày đăng: 29/03/2014, 07:20

Từ khóa liên quan

Mục lục

  • Preface

  • Getting Started

    • A quick tour of Pharo

      • Getting started

      • The World menu

      • Sending messages

      • Saving, quitting and restarting a Pharo session

      • Workspaces and Transcripts

      • Keyboard shortcuts

      • The Class Browser

      • Finding classes

      • Finding methods

      • Defining a new method

      • Chapter summary

      • A first application

        • The Lights Out game

        • Creating a new Package

        • Defining the class LOCell

        • Adding methods to a class

        • Inspecting an object

        • Defining the class LOGame

        • Organizing methods into protocols

        • Let's try our code

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan