Скачать презентацию Design Patterns Contents Terminology in object-oriented design Скачать презентацию Design Patterns Contents Terminology in object-oriented design

f82f38bf8959dd305b8f3dd2edcb1139.ppt

  • Количество слайдов: 33

Design Patterns Contents • Terminology in object-oriented design • Mechanisms for reuse • Designing Design Patterns Contents • Terminology in object-oriented design • Mechanisms for reuse • Designing for change • Categorizing and selecting design patterns Reference: Design Patterns; Gamma, Helm, Johnson, Vlissides

Terminology 1. Object Interface a. Signature of a method 2. Toolkits 3. Frameworks 4. Terminology 1. Object Interface a. Signature of a method 2. Toolkits 3. Frameworks 4. Design Patterns

Object Interface Class diagram Name attributes methods Every method in the class has its Object Interface Class diagram Name attributes methods Every method in the class has its own unique signature return_type name (parameter types) The set of all signatures defined by an object’s methods or operations is called the object interface. This interface characterizes the complete set of requests that can be sent to the object.

Specifying Object Interfaces Signature of a method: • name Class. A • parameters int Specifying Object Interfaces Signature of a method: • name Class. A • parameters int metod 1(int param); • return type void resize( ); Interface Class. B int metod 1(int param); void resize( ); Possible message recipients

Implementation of an Interface in C++ Interface provided by an abstract class Concrete realizations Implementation of an Interface in C++ Interface provided by an abstract class Concrete realizations receive the messages Circle Shape virtual double area( ) = 0; virtual void draw( ) = 0; virtual void resize(int scale) = 0; double area( ) ; void draw( ) ; void resize(int scale) ; Rectangle void draw( ) ; double area( ) ; void resize(int scale) ;

Class Hierarchy Shape Rectangle Square Circle Messages directed to a Shape object can be Class Hierarchy Shape Rectangle Square Circle Messages directed to a Shape object can be received by instances of any of these concrete classes.

Toolkits A toolkit is a set of related and reusable classes designed to provide Toolkits A toolkit is a set of related and reusable classes designed to provide general-purpose functionality. – Gamma et al. Examples: Collection classes: Vector, Stack, Queue, List, etc: Found in libraries such as java. util or the C++ STL The Java or C++ stream library

Frameworks A framework is a set of cooperating classes that make up a reusable Frameworks A framework is a set of cooperating classes that make up a reusable design for a specific class of software. – Gamma, et al. The framework dictates the architecture of your application. It captures the design decisions that are common to the domain of the application. It establishes the partitioning into classes and objects and the pattern of collaboration between objects. Frameworks promote design reuse Example: the java awt and swing packages

Design Patterns capture solutions to particular design problems. They provide for the reuse of Design Patterns capture solutions to particular design problems. They provide for the reuse of successful designs. Incorporate design experience. A design pattern is like a template that can be applied in many different situations. A design pattern provides an abstract description of a design problem and how a general arrangement of elements (classes and objects) solves it. – Gamma et al.

Design Patterns emphasize “design for maintainability”. Good design not only addresses the current problem, Design Patterns emphasize “design for maintainability”. Good design not only addresses the current problem, but also anticipates the need for future additions and modifications.

Elements of a Design Pattern Names allow the designer to work at a higher Elements of a Design Pattern Names allow the designer to work at a higher level of abstraction. Names make it easier to talk about and think about designs. Description of the problem Describes the problem and its context. The Solution Describes the classes that make up the design, their relationships, and collaborations. The Consequences The results and tradeoffs of applying the pattern.

Categories of Design Patterns Purpose Design Pattern Creational Abstract Factory Builder Aspects that can Categories of Design Patterns Purpose Design Pattern Creational Abstract Factory Builder Aspects that can vary Families of product objects Creation of a composite object Factory Method Structural Subclass of object instantiated Adapter Interface to an object Facade Flyweight How an object is accessed Command When & how a request is fulfilled Iterator {Partial listing} Storage costs of objects Proxy Behavioral Interface to a subsystem Traversal of elements in an aggregate

Notation Symbol Meaning Aggregation B A Object A instantiates B A B is a Notation Symbol Meaning Aggregation B A Object A instantiates B A B is a subclass of A B Comment box Class. Name class method o Implementation pseudocode

Iterator Pattern Intent Provide a way to access the elements of an aggregate object Iterator Pattern Intent Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. • Supports multiple traversals of aggregate objects. • Provides a uniform interface for traversing different aggregate structures (it supports polymorphic iteration).

Iterator Pattern Structure Aggregate Client create. Iterator( ) Iterator first( ) next( ) is. Iterator Pattern Structure Aggregate Client create. Iterator( ) Iterator first( ) next( ) is. Done( ) current. Item( ) Concrete. Aggregate Concrete. Iterator create. Iterator( ) //client code return new Concrete. Iterator(this) Aggregate my. List = new Concrete. Aggregate( ); Iterator itr = my. List. create. Iterator( );

Iterator Pattern Participants • Iterator -- defines an interface for accessing and traversing elements Iterator Pattern Participants • Iterator -- defines an interface for accessing and traversing elements • Concrete. Iterator -- implements the Iterator interface -- keeps track of the current position in the traversal of the aggregate • Aggregate -- defines an interface for creating an Iterator object • Concrete. Aggregate -- implements the Iterator creation interface to return an instance of the proper Concrete. Iterator

Iterator Pattern Consequences 1. It supports variations in the traversal of an aggregate. For Iterator Pattern Consequences 1. It supports variations in the traversal of an aggregate. For example, code generation may traverse the parse tree inorder or preorder. Iterators make it easy to change the traversal. Just replace the iterator instance with a different one. 2. Iterators simplify the Aggregate interface. Iterator’s traversal interface obviates the need for a similar interface in Aggregate. 3. More than one traversal can be pending on an aggregate.

Iterator Pattern Iterators in Java In object-oriented programming one distinguishes between the container object Iterator Pattern Iterators in Java In object-oriented programming one distinguishes between the container object that holds data and an Iterator object that traverses the container and visits the data items. To use an Iterator perform the following steps: 1. Obtain an iterator from the Collection object (List) by using a method (in the interface) iterator( ). This method call returns an iterator object and sets it to read the first element in the list. 2. Get the next object in sequence with method next( ). 3. See if there any more elements in the sequence with a call to has. Next( ). 4. You may remove the last item returned by the iterator from the list with a call to remove( )

Iterator Pattern Iterator in Java -- in Java we have the following methods that Iterator Pattern Iterator in Java -- in Java we have the following methods that implement the Design Pattern Iterator itr = container. Object. iterator( ); Implements the create. Iterator( ) in the Iterator Pattern itr. has. Next( ) Java implementation of is. Done( ) itr. next( ) Combines methods current. Item( ) and next( ) specified in the Pattern

Iterator Pattern Example of a polymorphic iterator With an iterator one can write generic Iterator Pattern Example of a polymorphic iterator With an iterator one can write generic methods to handle any collection public class Printer { public static void print. All (Iterator e) { while (e. has. Next( ) ) System. out. print (e. next( ) + “ ” ); System. out. println( ); The method receives an iterator object as its argument with no distinction made about what collection produced it. } public class Frog. Pond { public static void main(String [ ] args) { Array. List v = new Array. List( ); for (int i = 0; i < args[0]; i++) v. add(new Frog(i)); Printer. print. All(v. iterator( )); } } Obtain an iterator from the Array. List and pass it to the Printer

Command Pattern Intent Encapsulate a request as an object letting you: • Parameterize clients Command Pattern Intent Encapsulate a request as an object letting you: • Parameterize clients with different requests • Queue or log requests • Support undo operations

Command Pattern Structure Client Invoker Command execute() Receiver action() Concrete. Command execute() o state Command Pattern Structure Client Invoker Command execute() Receiver action() Concrete. Command execute() o state receiver_ action()

Command Pattern Participants • Command -- declares an interface for executing an operation • Command Pattern Participants • Command -- declares an interface for executing an operation • Concrete. Command -- defines a binding between a Receiver object and an action --implements execute() by invoking the corresponding operation(s) on Receiver • Client (Application) -- creates a Concrete. Command object and sets its receiver • Invoker (Menuitem) --asks the command to carry out the request • Receiver (Document) --knows how to carry out the request. (Any class may serve as Receiver)

Command Pattern Collaborations a. Receiver a. Client a. Command an. Invoker new Command add(a. Command Pattern Collaborations a. Receiver a. Client a. Command an. Invoker new Command add(a. Command) execute( ) action( )

Command Pattern Collaborations 1. The Client creates a Concrete. Command object and specifies its Command Pattern Collaborations 1. The Client creates a Concrete. Command object and specifies its receiver. 2. An Invoker object stores the Concrete. Command object. 3. The invoker issues a request by calling execute( ) on the command. When commands are undoable, Concrete. Command stores state for undoing prior to invoking execute( ). 4. The Concrete. Command object invokes operations on its receiver to carry out the request.

Command Pattern Consequences 1. Command decouples the object that invokes the operation from the Command Pattern Consequences 1. Command decouples the object that invokes the operation from the one that knows how to perform it. 2. Commands are first-class objects. They can be manipulated and extended like any other object. 3. Its easy to add new Commands, because you don’t have to change existing classes. 4. Commands can be assembled into a composite command.

Invoker App Menu Abstract Command Stack Command select( ); run( ) Menu. Item execute( Invoker App Menu Abstract Command Stack Command select( ); run( ) Menu. Item execute( ) Push Pop execute() Receiver Command Pattern Implemented in C++ Concrete Commands item ask( ) S-> push(item)

Using Command Pattern in Java Let’s redo the previous example in Java. The Command Using Command Pattern in Java Let’s redo the previous example in Java. The Command Design Pattern can be implemented within the framework provided in the swing classes. Inheritance Structure JMenu. Bar JComponent Abstract. Button JPopup. Menu JMenu. Item JRadio. Button. Menu. Item JCheck. Boxmenu. Item Notice that a JMenu is itself a menu item. This allows one to place a submenu inside of another menu.

Command Pattern in Java Extend class JMenu. Item to pass in a Command object Command Pattern in Java Extend class JMenu. Item to pass in a Command object in the constructor, and add a method select( ) that calls the execute( ) method of the embedded command. class My. JMenu. Item extends JMenu. Item{ Command the. Order; public My. JMenu. Item(String str, Command c) { super(str); the. Order = c; } public void select( ) { the. Order. execute( ); } }

Command Pattern in Java In the application (JApplet) method action. Performed( ) determines the Command Pattern in Java In the application (JApplet) method action. Performed( ) determines the source of the event (menu item selected) and executes its method select. public void action. Performed(Action. Event e) { My. JMenu. Item the. Item = (My. JMenu. Item)(e. get. Source( )); the. Item. select( ); }

Command Pattern in Java Commands are OBJECTS that implement the Command interface Command { Command Pattern in Java Commands are OBJECTS that implement the Command interface Command { public void execute( ); Interface Command with (abstract) method execute } class Pop implements Command{ Stack the. Stack; public Pop(Stack s) { the. Stack = s; } public void execute( ) { the. Stack. pop( ); } } Object Pop a concrete Command The Stack object that the Command directs is passed in as a parameter in the constructor Concrete Command objects override method execute( )

Invoker JApplet JMenu Abstract Command select( ); run( ) JMenu. Item execute( ) Stack Invoker JApplet JMenu Abstract Command select( ); run( ) JMenu. Item execute( ) Stack Push Pop execute() Receiver Command Pattern Implemented in Java Concrete Commands item ask( ) S-> push(item)

Further Information about Design Patterns Home Page Links of particular interest: Doug Lea’s FAQ Further Information about Design Patterns Home Page Links of particular interest: Doug Lea’s FAQ on Design Patterns Setting up a Design Pattern study group