Unformatted text preview:

CPS211 Lecture: Design Patterns Last revised November 30, 2009Objectives1. To introduce and illustrate the idea of design patterns2. To introduce some key design patterns students have used or will use:• Decorator (Wrapper/Filter) • Abstraction-Occurrence • Observer• Iterator• Factory• Proxy• Singleton• Adapter3. To demonstrate how several design patterns can be used together to solve a specific problemMaterials1. “Gang of Four” Design Patterns book to show2. Projectable of pp. 6-7 of book (general description of “pattern language”3. Projectable of p. 127-128 (the Singleton pattern)4. Projectable of figures 6.1 and 6.2 from Lethbridge/Langaniere5. Demo and handout of Iterator demo6. Spreadsheet with multiple charts demo7. Projectable of abstract sequence diagram for Observer pattern (p. 295)8. Projectable of simple Observer demo (tic-tac-toe)9. Demo and handout of Observable demo10. Demo and handout of swing program with buttons with different looks and feels11. Projectable of “Gang of Four” book page 17512. JDBCLab.java getCourseIds() method13. Demo of my video store showing multiple patterns use in solving a problemI. IntroductionA. One of the characteristics of an expert in many fields is that the expert has learned to recognize certain patterns that characterize a particular problem or call for a particular approach to a solution.Examples:1. A civil engineer uses certain structural patterns when designing bridges, highways, etc.2. A Medical Doctor recognizes certain patterns of symptoms as indicative of certain diseases.B. In the world of OO software in recent years, there has been a growing recognition of the value of studying design patterns - standard patterns of relationships between objects in a system (or portion of a system) that constitute good solutions to recurring problems.11. Much of this was inspired by a lecture by Christopher Alexander - an architect - given at OOPSLA 1996. Alexander is an architect - and spoke about patterns in relationship to that field - but software engineers have recognized that the same principles apply in other fields like ours.In one of his books, he gave the following definition of a pattern: “Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use the same solution a million times over, without doing it the same way twice.”2. A key book in this regard is the book Design Patterns by Gamma, Helm, Johnson, and Vlissides (known in OO circles as “the Gang of Four”). a) SHOWb) This book classifies the patterns it discusses into three broad categories, which were also discussed as homework exercise m. What are they?ASK3. One important characteristic of the study of patterns is the idea of giving each pattern a name, so that when people talk they can use the name of the pattern and others will know what they are talking. (Unfortunately, some key patterns have more than one name; but at least we don’t have the issue of the same name referring to different patterns!)4. In addition to giving the overall pattern a name, we also give a name to the various objects that participate in the pattern. Here, the name given to each object is mean to describe its role and responsibilities. The object does not have to actually be called by this name - the purpose of the name in the pattern is simply to help us understand how responsibilities are apportioned.EXAMPLE: At the present time, the person who has the role and responsibilities of President of the United States is Barack Obama. We may refer to him as “the president” - but his parents did not name him “President”, they named him Barack!C. In this lecture, we will try to illustrate the concept of design patterns by introducing several examples of design patterns - each of which is a good way to solve a particular kind of problem in a software system. 21. Obviously, one only wants to use the pattern if it meets a real need, of course!2. Most are patterns you have already used - some in CS112; some in labs or your project.D. We will begin with an example you’ve already used, and show how to describe it using “pattern language”. In standard discussions of patterns (including the “Gang of Four” book), it is common to describe patterns in a standard way.PROJECT: pp. 6-7 of “Gang of Four” book(Note: we will discuss patterns from other sources as well - but the “style” of this book is an accepted standard regardless of where the pattern descriptions come from.)II. The Singleton PatternA. We will begin by talking about a pattern that you are already quite familiar with. One of our goals will be to use it as an example of how patterns are described. The singleton pattern is applicable wherever there is a kind of object where it is necessary that there be one and only one copy of this object in existence.B. It belongs, therefore, to the category of creational patterns.C. Project, go over pages 127-128 of the “Gang of Four” bookIII. The Abstraction-Occurence PatternA. One pattern you have been using in your video store project is a pattern called the Abstraction-Occurrence pattern. Can anyone describe this pattern and how it relates to the video store project?ASKB. In terms of the three categories of pattern we discussed earlier, to which category would this pattern belong?ASKStructural3C. This pattern actually doesn’t appear in the “Gang of Four” book. The author of one book that does discuss it introduces this pattern this way: “Often in a domain model you will find a set of related objects that we will call occurrences; the members of such a set share common information but also differ from each other in important ways.Examples: ASKD. This particular situation is one to which it turns out there are both good and bad solutions. The idea is to represent the various occurrences in some way that avoids duplicating the common information.1. In general, the clean solution is to use two classes - an « abstraction » class and an « occurrence » class.PROJECT: Figure 6.1 of Lethbridge/Langaniere2. The author of this book also notes that there are a number of possible “antipatterns” (or poor solutions.) Let’s look at several and discuss why each is not a good way to solve the problem.PROJECT: Figure 6.2. of Lethbridge/LanganiereDiscuss why each “solution” is not gooda) This example is undesirable


View Full Document

Gordon CPS 211 - Design Patterns

Download Design Patterns
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Design Patterns and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Design Patterns 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?