Design Patterns Reference Guide

December 12, 2016 | Author: Ashok Renukappa | Category: N/A
Share Embed Donate


Short Description

Design Patterns Reference Guide...

Description

Design Patterns Reference Guide

Design Patterns Reference Guide Explained

Creational Patterns Pattern

Definition

Abstract Factory

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

Participants AbstractFactory declares an interface for operations that create abstract products ConcreteFactory implements the operations to create concrete product objects AbstractProduct declares an interface for a type of product object Product defines a product object to be created by the corresponding concrete factory implements the AbstractProduct interface Client uses interfaces declared by AbstractFactory and AbstractProduct classes

UML Class Diagram

Frequency of Usage, Benefits and Usage High Benefits - Isolates concrete classes - Allows to change product family easily Promotes consistency among products Usage - When the system needs to be independent of how its products are created composed and represented. - When the system needs to be configured with one of multiple families of products. - When a family of products need to be used together and this constraint needs to be enforced. - -When you need to provide a library of products, expose their interfaces not the implementation.

1

Design Patterns Reference Guide

Factory Method

Singleton

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

Ensure a class has only one instance and provide a global point of access to it.

Product defines the interface of objects the factory method creates ConcreteProduct implements the Product interface Creator declares the factory method, which returns an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object. may call the factory method to create a Product object. ConcreteCreator overrides the factory method to return an instance of a ConcreteProduct. Singleton defines an Instance operation that lets clients access its unique instance. Instance is a class operation. responsible for creating and maintaining its own unique instance.

High Benefits Eliminates the needs to bind application classes into your code. The code deals woth interfaces so it can work with any classes that implement a certain interface. Enables the subclasses to provide an extended version of an object, because creating an object inside a class is more flexible than creating an object directly in the client. Usage When a class cannot anticipate the class of objects it must create. When a class wants its subclasses to specify the objects it creates. Classes delegate responsability to one of several helper subclasses, and you want to localize the knowledge of which helper subclasses is the delegate.

medium high Benefits Controlled access to unique instance. Reduced name space. Allows refinement of operations and representations. Permits a variable number of instances. More flexible than class operations. Usage When there must be only one instance of a class.

Structural Patterns Pattern

Definition

Participants

UML Class Diagram

Frequency of Usage, Benefits and Usage

2

Design Patterns Reference Guide

Adapter

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces

Composit Compose objects into e

tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

Target defines the domain-specific interface that Client uses. Adapter adapts the interface Adaptee to the Target interface. Adaptee defines an existing interface that needs adapting. Client collaborates with objects conforming to the Target interface.

Component  declares the interface for objects in the composition.  implements default behavior for the interface common to all classes, as appropriate.  declares an interface for accessing and managing its child components.  (optional) defines an interface for accessing a component's parent in the recursive structure, and implements it if that's appropriate. Leaf  represents leaf objects in the composition. A leaf has

medium high Benefits Allow two or more incompatible objects to communicate and interact. Improves reusability of older functionality. Usage When you want to use an existing class, and its interface does not match the interface you need. When you want to create a reusable class that cooperates with unrelated or onforeseen classes, classes that don't necesarily have compatible interfaces. When you want to use an object in an environment that expects an interface that is diferent from the object's interface. When you must ensure interface translation among multiple sources.

medium high Benefits Define class hierarchies consisting of primitive objects and composite objects. Makes it easier to add new kind of components. Provides flexibility of structure and manageable interface. Usage When you want to represent the whole hierarchy or a part of the hierarchy of objects. When you want clients to be able to ignore the differences between compositions of objects and individual objects. When the structure can have any level of complexity andis dynamic.

3

Design Patterns Reference Guide

no children. defines behavior for primitive objects in the composition. Composite  defines behavior for components having children.  stores child components.  implements child-related operations in the Component interface. Client  manipulates objects in the composition through the Component interface. 

Decorato r

Attach additional responsibilitie s to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

Component defines the interface for objects that can have responsibilities added to them dynamically. ConcreteComponent defines an object to which additional responsibilities can be attached. Decorator maintains a reference to a Component object and defines an interface that conforms to Component's interface. ConcreteDecorator adds responsibilities to the

Medium Benefits More flexibility than static inheritance. Avoids feature-laedn classes high up in the hierarchy. Simplifies coding because you write a series of classes each targeted at a specific part of the functionality rather than coding all behavior into the object. Enhances the object's extensibility because you make changes by coding new classes. Usage When you want to add responsibilities to individual objects dinamically and transparently, without affecting other objects. When you want to add responsibilities to the object that you might want to change in the future. When extension by static subclassing is impractical.

4

Design Patterns Reference Guide

Facade

Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.

component. Facade knows which subsystem classes are responsible for a request. delegates client requests to appropriate subsystem objects. Subsystem classes implement subsystem functionality. handle work assigned by the Facade object. have no knowledge of the facade and keep no reference to it.

High Benefits Provides a simple interface to a complex system without reducing the functionality provided by the system. Shields clients from complexity of subsystem components. Promotes weak coupling between the subsystem and its clients. Reduces coupling between subsystems if every subsystem uses its own facade pattern. Translates client requests to the subsystems that can fulfill those requests. Usage When you want to provide a simple interface to a complex subsystem. When there are many dependencies between clients and the implementation classes of an abstraction. When you want to layer your subsystems.

5

Design Patterns Reference Guide

Proxy

Provide a surrogate or placeholder for another object to control access to it.

Proxy  maintains a reference that lets the proxy access the real subject. Proxy may refer to a Subject if the RealSubject and Subject interfaces are the same.  provides an interface identical to Subject's so that a proxy can be substituted for for the real subject.  controls access to the real subject and may be responsible for creating and deleting it.  other responsibilites depend on the kind of proxy:  remote proxies are responsible for encoding a request and its arguments and for sending the encoded request to the real subject in a different address space.

medium high Benefits A remote proxy can hide the fact that an object resides in a different address space. A virtual proxy can perform optimizations such as creating an object on demand. both protection proxies and smart references allow additional housekeeping tasks when an object is accessed. Usage when you need a more versatile or sophisticated reference to an object than a simple pointer.

6

Design Patterns Reference Guide

virtual proxies may cache additional information about the real subject so that they can postpone accessing it. For example, the ImageProxy from the Motivation caches the real images's extent.  protection proxies check that the caller has the access permissions required to perform a request. Subject defines the common interface for RealSubject and Proxy so that a Proxy can be used anywhere a RealSubject is expected. RealSubject defines the real object that the proxy represents. 

Behavioral Patterns Pattern

Definition

Participants

UML Class Diagram

Frequency of Usage

7

Design Patterns Reference Guide

Command Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

Command declares an interface for executing an operation ConcreteCommand defines a binding between a Receiver object and an action implements Execute by invoking the corresponding operation(s) on Receiver Client creates a ConcreteCommand object and sets its receiver Invoker asks the command to carry out the request Receiver knows how to perform the operations associated with carrying out the request.

medium high

8

Design Patterns Reference Guide

Iterator

Observer

Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation .

Iterator defines an interface for accessing and traversing elements. ConcreteIterator 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 ConcreteAggregate implements the Iterator creation interface to return an instance of the proper ConcreteIterator Define a oneSubject to-many knows its observers. Any dependency number of Observer between objects so that objects may observe a subject when one object provides an interface for changes attaching and detaching state, all its Observer objects. dependents ConcreteSubject are notified stores state of interest to and updated automatically. ConcreteObserver sends a notification to its observers when its state

high

high

9

Design Patterns Reference Guide

State

Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

changes Observer defines an updating interface for objects that should be notified of changes in a subject. ConcreteObserver maintains a reference to a ConcreteSubject object stores state that should stay consistent with the subject's implements the Observer updating interface to keep its state consistent with the subject's Context defines the interface of interest to clients maintains an instance of a ConcreteState subclass that defines the current state. State defines an interface for encapsulating the behavior associated with a particular state of the Context. Concrete State each subclass implements

medium

10

Design Patterns Reference Guide

Strategy

Define a family of algorithms, encapsulate each one, and make them interchangeab le. Strategy lets the algorithm vary independently from clients that use it.

a behavior associated with a state of Context Strategy declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy ConcreteStrategy implements the algorithm using the Strategy interface Context is configured with a ConcreteStrategy object maintains a reference to a Strategy object may define an interface that lets Strategy access its data.

medium high

11

Design Patterns Reference Guide

Template

Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

AbstractClass defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm  implements a template method defining the skeleton of an algorithm. The template method calls primitive operations as well as operations defined in AbstractClass or those of other objects. ConcreteClass  implements the primitive operations to carry out subclassspecific steps of the algorithm 

medium

12

Design Patterns Reference Guide

Summary Creational Patterns Abstract Factory Builder Factory Method Prototype

Singleton

Creates an instance of several families of classes Separates object construction from its representation Creates an instance of several derived classes A fully initialized instance to be copied or cloned A class of which only a single instance can exist

Structural Patterns Adapter Bridge

Composite

Match interfaces of different classes Separates an object’s interface from its implementation A tree structure of simple and composite objects

Decorator

Add responsibilities to objects dynamically

Facade

A single class that represents an entire

13

Design Patterns Reference Guide

subsystem Flyweight Proxy

A fine-grained instance used for efficient sharing An object representing another object

Behavioral Patterns Chain of Resp. Command

Interpreter

Iterator

Mediator

Memento

Observer State

A way of passing a request between a chain of objects Encapsulate a command request as an object A way to include language elements in a program Sequentially access the elements of a collection Defines simplified communication between classes Capture and restore an object's internal state A way of notifying change to a number of classes Alter an object's behavior when its state

14

Design Patterns Reference Guide

changes Strategy

Encapsulates an algorithm inside a class

Template

Defer the exact steps of an algorithm to a

Method Visitor

subclass Defines a new operation to a class without change

OO Basics Abstraction Encapsulation Polymorphism Inheritance

15

Design Patterns Reference Guide

OO Design Principles Encapsulate what varies. Favor composition over inheritance. Program to interfaces, not implementations. Strive for loosely coupled designs between objects that interact. Classes should be open for extension but closed for modification. Depend on abstractions. Do not depend on concrete classes. Only talk to your friends. Don’t call us, we’ll call you. A class should have only one reason to change. References: Head First Design Patterns – http://www.javainterview.com/design_patterns_interview_questions.html http://www.java-interview.com/design_patterns_interview_questions.html http://www.apwebco.com/gofpatterns/ http://www.java-interview.com/design_patterns_interview_questions.html

16

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF