Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 47 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
47
Dung lượng
390 KB
Nội dung
Design Pattern 1 Structural design patterns Design Pattern 2 Structural design patterns Adapter Bridge Composite Decorator Façade Flyweight Proxy Design Pattern 3 The Adapter Pattern Putting a Square Peg in a Round Hole! Design Pattern 4 What is Adapters Real world is full of them! Design Pattern 5 Object oriented adapters Scenario: you have an existing software system that you need to work a new vendor library into, but the new vendor designed their interfaces differently than the last vendor. What to do? Write a class that adapts the new vendor interface into the one you’re expecting. Your Existing System Vendor Class Their interface doesn’t match the one you’ve written your code against. Not going to work! Design Pattern 6 Object oriented adapters Your Existing System Vendor Class Adapter The adapter implements the interface your classes expect Your Existing System Vendor Class Adapter No code changes New code No code changes And talks to the vendor interface to service your requests Design Pattern 7 The Adapter Pattern - Intent The client sees only the Target interface. The Adapter implements the target interface All requests get delegated to the Adaptee Adapter is composed with the Adaptee Full of good OO design principles: - Use of object composition - Pattern binds the client to an interface and not an implementation Full of good OO design principles: - Use of object composition - Pattern binds the client to an interface and not an implementation The Adapter Pattern converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces. Object Adapter Design Pattern 8 Object and Class Adapters There are two types of Adapters Object Adapter : use composition to adaptive the adaptee. Class Adapter : use inheritance. Class Adapter Design Pattern 9 Applicability Use the Adapter pattern when want to use an existing class, and its interface does not match the one you need. want to create a reusable class that cooperates with unrelated or unforeseen classes that don't necessarily have compatible interfaces. Class and object adapters have different trade- offs. A class adapter won't work when we want to adapt a class and all its subclasses. An object adapter lets a single Adapter work with the Adaptee itself and all of its subclasses (if any). Design Pattern 10 Example Turkey has a incompatible interface with Duck. We’d like to use some Turkey as Duck Target interface Adaptee [...]... program Design Pattern 27 Motivating Example First idea for design: since the type of drawing program is told at the time of instantiating rectangles, we could have two types of rectangles, one that uses DP1 and other that uses DP2 A straightforward design using inheritance, solves the problem … but requirements change – Also Circles must be supported Design Pattern 28 Motivating Example The design. .. Adaptee implements the interface of the Target, and get a reference to adaptee Design Pattern 11 Using two-way adapters to provide transparency Design Pattern 12 Example: Adapting an Enumeration to an Iterator Target interface Adaptee interface We are making the Enumeration in your old code look like Iterator for your new code Design Pattern 13 Example: Adapting an Enumeration to an Iterator A class implementing... programs, there are V1Drawing (based on DP1) and V2Drawing (based on DP2) Design Pattern 31 The Motivating Example By representing the variability with derived classes we get Design Pattern 32 The Motivating Example Now we have separated Shape abstraction from Drawing implementation in a way that the two can vary independently Design Pattern 33 Basic Aspects of Bridge Pattern Intent Decouple an... of the façade Design Pattern 19 The Facade Pattern – Applicability Use the Facade pattern when want to provide a simple interface to a complex subsystem decouple the subsystem from the dependencies of clients and other subsystems, thereby promoting subsystem independence and portability want to layer your subsystems Use a facade to define an entry point to each subsystem level Design Pattern... the façade with its subsystem and use delegation to perform the work of the façade You can implement more than one façade for a subsystem A façade "wraps" a set of objects to simplify! Design Pattern 25 Bridge Pattern Design Pattern 26 Motivating Example Write a program that draw rectangles with either of the two drawing programs, DP1 or DP2 When instantiating a rectangle it is known which drawing... Class adapters require multiple inheritance An adapter wraps an object to change its interface, a decorator wraps an object to add new behaviors and responsibilities Design Pattern 15 The Façade Pattern Simplify, simplify, simplify! Design Pattern 16 The Facade Pattern – Key Features Problem Need to use only a subset of a complex system Or you need to interact with the system in a particular... Intent: Provide a unified interface to a set of interfaces in a subsystem Facade defines a higher-level interface that makes the subsystem easier to use Design Pattern 17 The Facade Pattern – Key Features Structure Facade Client subsystem classes Design Pattern 18 The Facade Pattern – Participants Facade knows which subsystem classes are responsible for a request delegates client requests to... and their implementations should be independently extensible by subclassing Hide the implementation of an abstraction completely from clients Design Pattern 34 Structure defines the abstraction's interface maintains a reference to an object of type Implementor Design Pattern 35 Implementation Only one Implementor not necessary to create an abstract implementor class degenerate, but useful due... look like Iterator for your new code Design Pattern 13 Example: Adapting an Enumeration to an Iterator A class implementing the Enumeration interface is the adaptee EnumerationIterat or is the Adapter Design Pattern 14 Summary When you need to use an existing class and its interface is not the one you need, use an adapter An adapter changes an interface into one a client expects Implementing an... facade to define an entry point to each subsystem level Design Pattern 20 Example - Home Sweet Home Theater That’s a lot of classes, a lot of interactions, and a big set of interfaces to learn and use Design Pattern 21 Watching a Movie the Hard Way! 1 Turn on the popcorn popper 2 Start the popper popping 3 Dim the lights 4 Put the screen down 5 Turn the projector on 6 Set the projector input to DVD . Design Pattern 1 Structural design patterns Design Pattern 2 Structural design patterns Adapter Bridge Composite Decorator Façade Flyweight Proxy Design Pattern 3 The. Pattern 3 The Adapter Pattern Putting a Square Peg in a Round Hole! Design Pattern 4 What is Adapters Real world is full of them! Design Pattern 5 Object oriented adapters Scenario: you have. with the Adaptee Full of good OO design principles: - Use of object composition - Pattern binds the client to an interface and not an implementation Full of good OO design principles: - Use of object