Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 480 Software Engineering Design With Patterns.

Similar presentations


Presentation on theme: "CSC 480 Software Engineering Design With Patterns."— Presentation transcript:

1 CSC 480 Software Engineering Design With Patterns

2 The Essence of Patterns Each pattern describes a problem which occurs over and over again, 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

3 What Is a Design Pattern? In general, a pattern has four essential elements – The pattern name – abstracts a design problem, its solutions, and consequences (vocabulary) – The problem – explains the context and describes how to represent algorithm as objects – The solution – describes the elements that make up the design, their relationships, responsibilities, and collaborations – The consequences – the results and trade-offs of applying the pattern (evaluation)

4 The Catalog of GoF Patterns Design patterns are first classified by their purpose – Creational – the process of object creation – Structural – composition of classes and objects – Behavioral – the ways in which classes and objects interact and distribute responsibilities The second criterion is scope – Class – static, fixed at compile-time – Object – more dynamic, changeable at run-time

5 Design Pattern Space

6 The COMMAND Pattern The COMMAND pattern defines a method that all subclasses needs to do, while deferring details of the algorithms to each subclass to allow for additional information being provided. behavioral object

7 Solution Define a command interface with a method to execute the command. Supply methods in the command interface type to manipulate the state of the command objects. Each concrete command class implement the command interface. To invoke the command, call the execute method.

8 Structure Client state Concrete Command execute() > Command execute()

9 Example The java.awt.Component class Name in Command PatternActual Name CommandComponent ConcreteCommandMyTankPanel execute()paintComponent() statemyTank, road, shell

10 How to Memorize? The Hollywood Principle Don’t call us, we’ll call you. When we design with the Command pattern, we’re telling subclasses the same thing Other patterns (to be discussed shortly) that make use of the Hollywood Principle – Observer – Factory method

11 The OBSERVER Pattern The OBSERVER pattern provides a mechanism to monitor and notify changes in an source object to one or more objects (known as the “observers”) that need to update themselves with the latest state from the source. behavioral object

12 Solution Define an observer interface type. All concrete observer classes must implement this interface type. The subject (source) maintains a collection of observer objects. The subject class supplies the method(s) for attaching observers. Whenever an event (change) occurs, the subject notifies all observers, which in turn update themselves.

13 Structure Subject Concrete Observer notify() > Observer notify() attach()

14 Example The java.awt.event.ActionListener interface (as well as other XxxListener interfaces) Name in Command PatternActual Name Subject JButton Observer ActionListener ConcreteObserverSpecific classes implementing ActionListener attach() addActionListener() notify() actionPerformed()

15 The FACTORY METHOD Pattern The Factory Method pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. creational class

16 Context A type (the creator) creates objects of another type (the product). Subclasses of the creator type need to create different kinds of product objects. Clients do not need to know the exact type of product objects.

17 Solution Define a creator type that expresses the commonality of all creators. Define a product type that expresses the commonality of all products. Define a method, called the factory method, in the creator type. The factory method yields a product object. Each concrete creator class implements the factory method so that it returns an object of a concrete product class.

18 Structure

19 Example The java.util.Collection interface (or the java.util.Iterable interface as available since Java5) Name in Command PatternActual Name Creator Collection Product Iterator factorMethod() iterator() ConcreteCreatorAny specific Collection class ConcreteProductAny specific Iterator class (which is often anonymous)

20 The ABSTRACT FACTORY Pattern Similar to the FACTORY METHOD, the ABSTRACT FACTORY pattern defines methods that construct a family of related products. A concrete factory class is needed for each family of related products. creational object

21 Solution Define an AbstractFactory interface type. All concrete Factory classes must implement this interface type. Define two or more product types that expresses the commonality of family of related products. Define a create method for each kind of related products in the family in the AbstractFactory type. Each create method yields a product object in the desired type. Each concrete factory class implements all set of the factory methods so that it returns an set of related product objects for the specific style.

22 Structure

23 Example The java.awt.event.ActionListener interface (as well as other XxxListener interfaces) Name in Command PatternActual Name AbstractFactoryAbstractTankFactory AbstractProductAbstractTank (may be broken down to tank body, canon, and shell, etc) factorMethod()createTank() ConcreteFactoryEET1TankFactory, MyGameTankFactory ConcreteProductEET1Tank, MyGameTank

24 Composite Design Pattern


Download ppt "CSC 480 Software Engineering Design With Patterns."

Similar presentations


Ads by Google