4-Creational Design Patterns.ppt

56 392 1
4-Creational Design Patterns.ppt

Đ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

Design Pattern 1 Design Pattern Design Pattern 2 Design Patterns  “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 this solution a million times over, without ever doing it the same way twice.” Christopher Alexander, A Pattern Language: Towns/Buildings/Construction, 1977  “A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object oriented design.” Erich Gamma et al., Design Patterns: Elements of Reusable Object-Oriented Software Design Pattern 3 Benefits of design patterns  Capture the knowledge of experienced developers  Publicly available “repository”  Newcomers can learn them and apply them to their design  Yield a better structure of the software (modularity, extendibility)  Common pattern language  Facilitate discussions between programmers and managers Design Pattern 4 Organizing the Catalog Purpose Creational Structural Behavioral Scope Class Factory Method Adapter (class) Interpreter Template Method Object Abstract Factory Builder Prototype Singleton Adapter (object) Bridge Composite Decorator Flyweight Facade Proxy Chain of Responsibility Command Iterator Mediator Memento Observer State Strategy Visitor Scope: domain over which a pattern applies Purpose: reflects what a pattern does Design Pattern 5 Creational design patterns Creational  Abstract Factory  Factory Method  Builder  Prototype  Singleton  Goal:  Put more flexibility into the instantiation process  How:  Through inheritance or delegation  What:  Defer parts of object creation Design Pattern 6 Structural design patterns Structural  Adapter  Bridge  Composite  Decorator  Façade  Flyweight  Proxy  Goal:  Compose software elements into bigger structures  How:  Through inheritance (static binding) or composition (flexibility) Design Pattern 7 Behavioral design patterns Behavioral  Chain of Responsibility  Command  Interpreter  Iterator  Mediator  Memento  Observer  State  Strategy  Template Method  Visitor  Deal with:  Algorithms  Assignment of responsibilities between objects  Communication between objects  How:  Through inheritance or composition Design Pattern 8  Collaborations  how participants cooperate to carry out their responsibilities  Consequences  the results of application, benets, liabilities  Implementation  implementation pitfalls, hints, or techniques, plus any language- dependent issues  Sample Code  sample implementations  Known Uses  examples drawn from existing systems  Related Patterns  discussion of other patterns that relate to this one  Pattern name and classification  Intent  description and purpose of pattern  Also Known As  other names  Motivation  motivating scenario demonstrating pattern's use  Applicability  circumstances in which pattern applies  Structure  graphical representation of the pattern using UML  Participants  participating classes and/or objects and their responsibilities Description of a design pattern Design Pattern 9 Creational Patterns Design Pattern 10 Creational Patterns: Maze Game Case Study  Maze as a set of rooms. A room knows its neighbors; possible neighbors are another room, a wall, or a door to another room.  The following diagram shows only the parts of these classes that are important for creating a maze. [...]... configured with one of multiple families of products  A family of related product objects is designed to be used together, and you need to enforce this constraint  You want to provide a class library of products, and you want to reveal just their interfaces, not their implementations Design Pattern 12 Structure Design Pattern 13 Consequences  flexibility: isolates clients from implementation classes... AbstractFactory interface and all the classes that depend on it  A more flexible but less safe design is to add a parameter to operations that create objects:  could be a identifier, or anything else that specifies the kind of product to be created Design Pattern 15 Creating The Maze with Abstract Factory Design Pattern 16 Solving the Maze problem // default implementation needed, thus // abstract... new BombedMazeFactory(); game.createMaze(bFactory); Design Pattern 19 Factory method Pattern Design Pattern 20 Factory Method Motivation  Application class is responsible for creation and management of Documents  Problem: class knows WHEN a new document should be created  Application class doesn’t know WHAT KIND of document to create  Application Design Pattern 21 Solution  Application defines a... factory method to create multiple kinds of objects  factory methods takes a parameter: a kind of object to create  all products have to share a Product interface Design Pattern 27 Solving the Maze problem… Design Pattern 28 Builder Pattern Design Pattern 29 The Builder pattern – Motivation  A reader for the RTF (Rich Text Format) document exchange format should be able to convert RTF to many text formats:... for creating and assembling a complex object and puts it behind an abstract interface Design Pattern 30 Motivation Design Pattern 31 Builder pattern  Intent   Separate the construction of a complex object from its representation so that the same construction process can create different representations Structure Design Pattern 32 Builder pattern – Applicability Use the Builder pattern when  The... helper subclasses, but do not know which helper subclass is the delegate Design Pattern 23 Factory Method - Structure declares the factory method, or define a default implementation of the factory method defines the interface of objects the factory method creates overrides the factory method to return an instance of a ConcreteProduct Design Pattern 24 Factory Method Consequences  Advantage  Concrete... parallel class hierarchies  see  next slide for example Potential disadvantage  clients might have to subclass the Creator class just to create a particular ConcreteProduct object Design Pattern 25 Connecting Parallel Hierarchies Design Pattern 26 Factory Method Implementation  Two major varieties declares ABSTRACT factory method, ConcreteCreator implements it  Creator defines a default implementation... control over the construction process  the Builder constructs the product step-by-step under the director’s control Design Pattern 34 The Maze - Builder public void void void Maze } interface MazeBuilder { buildMaze(); buildRoom(int room); buildDoor(int roomFrom, int roomTo); getMaze(); Design Pattern 35 MazeGame: CreateMaze method public class MazeGame { public Maze createMaze(MazeBuilder builder) {...Abstract Factory Pattern Design Pattern 11 Abstract Factory Pattern  Intent   Provide an interface for creating families of related or dependent objects without specifying their concrete classes Applicability  When clients... makeMaze() { return new Maze(); } public Wall makeWall() { return new Wall(); } public Room makeRoom(int n) { return new Room(n); } public Door makeDoor(Room r1, Room r2) { return new Door(r1, r2); } } Design Pattern 17 Solving the Maze problem public class MazeGame { public Maze createMaze(MazeFactory factory) { Maze aMaze = factory.makeMaze(); Room r1 = factory.makeRoom(1); Room r2 = factory.makeRoom(2); . Design Pattern 1 Design Pattern Design Pattern 2 Design Patterns  “Each pattern describes a problem which occurs over and. and/or objects and their responsibilities Description of a design pattern Design Pattern 9 Creational Patterns Design Pattern 10 Creational Patterns: Maze Game Case Study  Maze as a set of rooms object oriented design. ” Erich Gamma et al., Design Patterns: Elements of Reusable Object-Oriented Software Design Pattern 3 Benefits of design patterns  Capture the knowledge of experienced developers  Publicly

Ngày đăng: 16/07/2014, 04:00

Mục lục

  • Benefits of design patterns

  • Description of a design pattern

  • Creational Patterns: Maze Game Case Study

  • Creating The Maze with Abstract Factory

  • Factory Method - Structure

  • Solving the Maze problem…

  • The Builder pattern – Motivation

  • Builder pattern - Consequences

  • The Maze - Builder

  • SimpleMazeBuilder (con't)

  • Solving the Maze Problem

  • clone() method: Shallow copy

  • clone() method: Deep copy

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

Tài liệu liên quan