1 / 16

Patterns

Patterns. Outline. Iterators The Pattern Concept The OBSERVER Pattern Layout Managers and the STRATEGY Pattern Components, Containers, and the COMPOSITE Pattern How to Recognize Patterns Putting Patterns to Work. Design patterns. history

Download Presentation

Patterns

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. Patterns

  2. Outline • Iterators • The Pattern Concept • The OBSERVER Pattern • Layout Managers and the STRATEGY Pattern • Components, Containers, and the COMPOSITE Pattern How to Recognize Patterns Putting Patterns to Work

  3. Design patterns • history • the concept of a "pattern" was first expressed in Christopher Alexander's work A Pattern Language in 1977 (2543 patterns) • in 1990 a group called the Gang of Four or "GoF" (Gamma, Helm, Johnson, Vlissides) compile a catalog of design patterns • design pattern: a solution to a common software problem in a context • example: Iterator patternThe Iterator pattern defines an interface that declares methods for sequentially accessing the objects in a collection.

  4. List Iterators LinkedList<String> list = . . . ;ListIterator<String> iterator = list.listIterator(); while (iterator.hasNext()){String current = iterator.next();. . .} Why iterators?

  5. Classical Data structure method • Traverse links directly • Link currentLink = list.head;while (currentLink != null){Object current = currentLink.data;currentLink = currentLink.next;} • Exposes implementation • Error-prone

  6. High level view of data strcutures • Queue • Array with random access • List???

  7. List with cursor • for (list.reset(); list.hasNext(); list.next()){Object x = list.get();. . .} • Disadvantage: Only one cursor per list • Iterator is superior concept

  8. Pattern Concept • History: Architectural Patterns • Christopher Alexander • Each pattern has • a short name • a brief description of the context • a lengthy description of the problem • a prescription for the solution

  9. Short Passages Pattern

  10. Short Passages Pattern • Context "...Long, sterile corridors set the scene for everything bad about modern architecture..." • Problem A lengthy description of the problem, including • a depressing picture • issues of light and furniture • research about patient anxiety in hospitals • research that suggests that corridors over 50 ft are considered uncomfortable

  11. Short Passages Pattern • Keep passages short. Make them as much like rooms as possible, with carpets or wood on the floor, furniture, bookshelves, beautiful windows. Make them generous in shape and always give them plenty of light; the best corridors and passages of all are those which have windows along an entire wall.

  12. Iterator Pattern • Context • An aggregate object contains element objects • Clients need access to the element objects • The aggregate object should not expose its internal structure • Multiple clients may want independent access

  13. Iterator Pattern • Define an iterator that fetches one element at a time Each iterator object keeps track of the position of the next element If there are several aggregate/iterator variations, it is best if the aggregate and iterator classes realize common interface types.

  14. Names in pattern are examples • Names differ in each occurrence of pattern

  15. More about patterns • A pattern describes a recurring software structure • is abstract from concrete design elements such as problem domain, programming language • identifies classes that play a role in the solution to a problem, describes their collaborations and responsibilities • lists implementation trade-offs • patterns are not code or designs; must be instantiated/applied • the software engineer is required to: • evaluate trade-offs and impact of using a pattern in the system at hand • make design and implementation decision how best to apply the pattern, perhaps modify it slightly • implement the pattern in code and combine it with other patterns

More Related