1 / 99

Unit 1 General Introduction

Unit 1 General Introduction. Summary prepared by Kirk Scott. This general introduction covers two chapters in the book: Chapter 1: Introduction Chapter 26: Introducing Extensions. Chapter 1 covers general information about the contents and organization of the book

shalin
Download Presentation

Unit 1 General Introduction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unit 1General Introduction Summary prepared by Kirk Scott

  2. This general introduction covers two chapters in the book: • Chapter 1: Introduction • Chapter 26: Introducing Extensions

  3. Chapter 1 covers general information about the contents and organization of the book • In addition to mentioning this, I outline the organization of the course, which differs from the book • Chapter 26 talks about over-arching object-oriented design principles which will be exemplified by the patterns that will be covered

  4. Design Patterns in JavaChapter 1Introduction Summary prepared by Kirk Scott

  5. Intended Audience, Coverage, etc. • The classic book, Design Patterns, was written by Gamma, Helm, Johnson, and Vlissides • This book covers the same set of 23 patterns • The book is intended for software programmers/developers who know Java and want to improve their skills as designers

  6. What is a Pattern? • A pattern is a way of doing something • It can be thought of as a technique for accomplishing some goal • This book dwells on the intent of patterns as an organizing characteristic • The chapters have been reorganized for the purposes of this course to emphasize common structure rather than common intent

  7. Where do Patterns Come From? • Software development is repetitive: Quite often, different programmers have to solve the same problem • Experienced programmers have compared notes and discovered that they arrived at common solutions to the same problem • Over time, these common solutions have been documented as the best known approach to solving a given problem

  8. Why Design Patterns? • Observe that in school, you learn to program first and learn about design later • When you compare your solution to a problem with someone else’s solution, you may realize that the other solution is simpler, more efficient, or more desirable in some other way • In practice, it is helpful to have knowledge of design before trying to code a solution to a problem

  9. Why Design Patterns? Cont’d. • Design patterns are one step up from straight code writing • They represent recognized software solutions in terms of (small) collections of classes related in a certain way and containing certain sets of methods • It is important to keep in mind that the implementation is not the pattern—a pattern is the abstract design which embodies the intent

  10. Why Design Patterns? Cont’d. • Design patterns have been developed most extensively in the object-oriented realm • The authors point out that there are currently over 100 documented design patterns • The 23 covered in the book have been chosen for historic reasons • They are certainly worth knowing, whether they are absolutely the most important

  11. Why Design Patterns? Cont’d. • In particular, whether the most important or not, the 23 chosen patterns provide a broad introduction to and illustration of what patterns are and what they’re good for • The authors assert that if a person wants to become a good Java programmer, it is necessary to study design patterns • Design patterns are the first step out of the swamp of syntax that an introduction to programming is

  12. Why Java? • Java is a consolidation language • This means that it is a kitchen sink language in the footsteps of C and C++, and for this reason, if no other, it is popular • Object-oriented programming is based on classes and instances, and this paradigm is currently the most popular approach to implementing graphical user interfaces, for example • Java’s characteristics make it likely that future popular languages will follow in Java’s footsteps

  13. UML • Implementations of patterns are done in code • However, the pattern itself consists of the relationships among classes and so on • UML is one of several notations for diagramming object-oriented designs • It is probably the most popular one currently • The point is that some abstract representation of patterns is desirable, rather than appealing always to the code of a particular implementation

  14. Challenges • The book contains questions • These may be short answer questions • They may also be programming questions based on the book’s example code • This course will not give these challenges as assignments • Instead, the challenges and the authors’ solutions will be examined to see what can be gained from them

  15. The Organization of the Book • This book organizes the patterns according to the intent which the authors ascribe to them • This raises the question of how to classify intent • The authors suggest that categories of patterns can be recognized according to how the patterns make it possible to go beyond the basic characteristics built into the structure of Java

  16. The Organization of This Book, cont’d. • The authors use as an initial example the idea of interfaces • Suppose you have a class/method which requires an object of a class which implements a given interface • Suppose you have an object of a class which does not implement the required interface • Is there a way of applying the method defined in the interface to an object of a class that doesn’t implement the interface?

  17. The Organization of This Book, cont’d. • The obvious answer is to rewrite the class to implement the interface • If that is not possible, there is an alternative • There is a design pattern known as the Adapter pattern (not to be confused with adapter classes in Java) which accomplishes this goal • The Adapter pattern, and others with similar intents, are classified in an intent category known as Interfaces • In the authors’ scheme, all of these patterns belong together

  18. The Organization of This Book, cont’d. • The book classifies patterns into these five categories based on intent: • Interfaces • Responsibility • Construction • Operations • Extensions

  19. The Organization of This Book, cont’d. • The book, as a consequence is organized in the same way • The authors admit that various patterns may fit more than one category • In such cases, the pattern is given in detail in the first category where it fits, and is mentioned briefly in subsequent sections • The classification of patterns is not an absolute, but some classification is useful and necessary, and is a starting point for suggesting alternative classifications

  20. The Organization of This Book, cont’d. • What follows is a complete preview of the contents of the book • Intent Category: Interfaces. • Patterns: Adapter, Façade, Composite, Bridge • Intent Category: Responsibility. • Patterns: Singleton, Observer, Mediator, Proxy, Chain of Responsibility, Flyweight

  21. The Organization of This Book, cont’d. • Intent Category: Construction. • Patterns: Builder, Factory Method, Abstract Factory, Prototype, Memento • Intent Category: Operations. • Patterns: Template Method, State, Strategy, Command, Interpreter • Intent Category: Extensions. • Patterns: Decorator, Iterator, Visitor

  22. The Organization of the Course • In this course the patterns will be organized and presented by structural similarity, not by intent • The outline of the course is given on the following overheads

  23. Structural Category: Construction and Cloning. • Patterns: Singleton, Prototype, Builder, Proxy • Structural Category: Generic Interfaces and Encapsulation. • Patterns: Mediator, Façade, Adapter, Flyweight, Decorator, Command, State

  24. Structural Category: Java Interfaces. • Patterns: Template, Iterator, Strategy • Structural Category: Dynamic Binding and Polymorphism. • Patterns: Factory Method, Abstract Factory, Bridge • Structural Category: Threads. • Patterns: Strictly speaking, this is not a pattern

  25. Structural Category: Composite and Related Patterns. • Patterns: Composite, Chain of Responsibility, Interpreter, Visitor • Structural Category: Model-View-Controller Related Patterns. • Patterns: Observer, Memento

  26. Welcome to Oozinoz! • The book’s examples are based on an integrated code base which is collectively known as Oozinoz (oohs and ahs), which deals with software needed for the production of fireworks • The authors’ examples will be presented an explained

  27. Principles of Object-Oriented Design • For people interested in the topic of patterns, the book cites this Web site: www.c2.com • There is no need to go there for the purposes of this course • From that page there is a link to a Wiki with a collection of patterns that goes beyond the ones covered in the book

  28. Summary • This is the book’s summary of the chapter, verbatim: • “Patterns are distillations of accumulated wisdom that provides a standard jargon, naming the concepts that experienced practitioners apply. • The patterns in the classic book Design Patterns are among the most useful class-level patterns and are certainly worth learning.

  29. Summary, cont’d. • This book explains the same patterns as those documented in Design Patterns but uses Java and its libraries for its examples and challenges. • By working through the challenges in this book, you will learn to recognize and apply an important part of the wisdom of the software community.”

  30. Design Patterns in JavaPart VExtension PatternsChapter 26Introducing Extensions Summary prepared by Kirk Scott

  31. The Introduction Before the Introduction • In the book’s overall scheme, the purpose of this chapter is to introduce extension patterns • The chapter also reviews some patterns that have been introduced already, assuming you’ve gone through the earlier chapters • Because this course is organized differently, these two purposes are not important

  32. The chapter also discusses some high-level object-oriented design principles • These are important • This is why I put this chapter at the beginning • These high-level concepts build on object-oriented ideas that were introduced in CS 202

  33. Object-Oriented Constructs and Principles • These are terms that you encountered in CS 202 are relevant to this discussion: • Encapsulation • Interfaces • Polymorphism • Dynamic binding

  34. The book will introduce these 4 ideas, which are related to the problem of arriving at a good object-oriented design: • Responsibility • Coupling • The Liskov Substitution Principle • The Law of Demeter

  35. Responsibility • In simple terms, responsibility means that classes/objects should be self-contained • Their definitions should contain all of the instance variables and methods needed in order to manipulate them fully and accomplish desired goals with them

  36. This is the norm, known as distributed responsibility • Responsibility can also be usefully concentrated in some cases • Various design patterns will illustrate both distributed and concentrated responsibility

  37. Coupling • There is a related concept, known as coupling • Ideally, objects are independent of each other • This generally desirable condition is known as loose coupling • Loose coupling might be thought of as meaning fewer connections • This would mean fewer places where you had to worry about a change in one object propagating to another

  38. Inevitably, sometimes a change in one object will trigger a change in another • As usual, there are also cases where tighter coupling is desirable • Various design patterns will illustrate both loose and tight coupling

  39. The Liskov Substitution Principle and the Law of Demeter • This chapter adds two major items to the list of object-oriented design principles: • The Liskov Substitution Principle • The Law of Demeter

  40. These principles are interesting in their own right • They draw together some important insights that apply to object-oriented design in general • As the design patterns are introduced, at various points it may become apparent that the patterns, in general, are bounded by these principles • There may also be places where a given pattern can be seen to push the boundaries of one of the principles in order to achieve a particular goal.

  41. Extending Code and the Design Principles • You rarely write code in a vacuum • You have an existing code base and your task is to add new features, classes, applications to it • Extension means programming within the context of given code

  42. Not only is it important to have rules of thumb to help you write good code from scratch • As a programmer, extending a code base, it would be helpful to have some rules of thumb to decide whether or not the code you are adding is any good

  43. The LiskovSubsitution Principle (LSP) • One of the aspects of object-orientation that should be familiar to you is the following: • A subclass is in an “is-a-kind-of” relationship with its superclass • In general, a subclass is a more specific kind of the superclass • At the very least, it has the same set of instance variables, since instance variables can’t (or shouldn’t) be overridden

  44. The subclass is made more specific by having more instance variables • It is also made more specific by adding or overriding methods, if necessary • In Java, it’s always syntactically possible to have a superclass reference to a subclass object

  45. The superclass reference won’t have access to the instance variables and unique methods of the subclass • However, the superclass reference will have access to any methods inherited by the subclass • It will also have access to any methods overridden in the subclass

  46. The ability to have superclass references is important when defining method parameters in an inheritance hierarchy • A method defined in a superclass with a superclass explicit parameter doesn’t have to be overridden in the subclass, with the explicit parameter re-typed to the subclass • It can simply be usedwith a subclass explicit parameter

  47. The book states that subclasses should be logical, consistent extensions of their superclasses • Trying to define the meaning of logical and consistent leads to the Liskov Substitution Principle • The book paraphrases the principle as follows: • “An instance of a class should function as an instance of its superclass.”

  48. Another way of putting this is, “Anything you can do to a superclass object, you can do to a subclass object.” • Java expects this and supports it syntactically • The real question is whether or not the internal logic of classes for a given problem domain agree with this • The book gives two problematic examples which are shown on the next overhead

  49. The Machine/UnloadBuffer example illustrates questions that arise in the problem domain • Superficially, you might want to class UnLoadBuffers with Machines • Without knowing how machines and buffers are designed, it’s not clear whether this is a correct design

More Related