Presentation is loading. Please wait.

Presentation is loading. Please wait.

DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.

Similar presentations


Presentation on theme: "DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development."— Presentation transcript:

1

2 DESIGN PATTERNS COMMONLY USED PATTERNS

3 What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development environment. A pattern addresses a recurring design problem that arises in a specific design situation and presents a solution to it.

4 Types Of Design Patterns  The Gang Of Four Design Patterns  The EJB Design Patterns  The Sun J2EE Design patterns  The Application Design patterns

5 Elements of a Design Pattern Name Problem Solution Consequences

6 Classification Creational Patterns – are ones that create objects for you,rather than having you instantiate objects directly Structural Patterns – help you compose groups of objects into larger structures. Behavioral Patterns – help you define the communication between objects in your system and how the flow is controlled in a complex program.

7 GoF Design Patterns

8 Key Rules 1. Program to an interface and not to an implementation 2. Favor Object Composition over Inheritance.

9 Creational Patterns Factory Abstract Factory Builder Prototype Singleton

10 Factory Returns an instance of one of several possible classes depending on the data provided to it. Usually all of the classes it returns have a common parent class and common methods, but each of them performs a task differently and is optimized for different kinds of data.

11 Abstract Factory Abstraction over the Factory Design Pattern Abstract factory is the factory that returns one of several factories. One of the example to this could be a look-and-feel in Java.

12 UML Diagram

13 Pros & Cons ProsCons 1. Shields clients from concrete classes. 2. Easy to switch product family at runtime – just change concrete factory 3. “keep it in the family” – enforces product family grouping 1. Adding a new product means changing factory interface + all concrete factories

14 Builder Separates the construction process of a complex object from its representation. Used when the construction process is same but the representations may differ.

15 UML Diagram

16 Example -- UML RTFReader ParseRTF() while(t=get the next token){ switch t.Type{ CHAR: builder->ConvertCharacter(t.Char) FONT: builder->ConventFontCharnge(t.Font) PARA: Builder->ConventParagraph() } } TextConverter ConvertCharacter(char) ConvertFontChange(Font) ConvertParagraph() ASCIIConverter ConvertCharacter(char) GetASCIIText() TextConverter ConvertCharacter(char) ConvertFontChange(Font) ConvertParagraph() GetTeXText() TextWidgestConverter ConvertCharacter(char) ConvertFontChange(Font) ConvertParagraph() GetTextWidget() ASCIIText TeXText TextWidget builders

17 How is it different from Abstract Factory ? BuilderAbstract Factory It focuses on constructing a complex object step-by- step Returns a complex object as a final result AF emphasizes on creating a family of related objects Product gets returned immediately.

18 Prototype Used when creation of an object is time consuming or very complex. In java you can use this by implementing Cloneable interface. Pros.Cons. 1. Shields clients from concrete classes 2. The object is the factory - i.e. Product and Creator combined (saves coding a Creator for every Product) 3. Pre-configured object instances – instead of create/set member every time. 1. Requires Memory to hold prototype. 2. Clone() is hard to Implement. 3. Many prototypes must be passed.

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

20 Structural Patterns Describes how classes and objects can be combined to make larger structures. Class Patterns – Inheritance Object Patterns – Composition

21 Structural Patterns Adapter Bridge Composite Façade Proxy Flyweight Decorator

22 Adaptor Pattern Converting an interface into another interface that its client is expected to see. Two flavor of this pattern come as: Class Adaptor Object Adaptor Java Adaptors

23 Class Adaptor

24 Object Adaptor

25 Bridge Pattern Separates Interface from its implementation so that both can be changed independently.

26 UML Diagram

27 Composite Pattern Allows clients to treat both single components and collection of components identically. Represents recursive data structures. Compose objects into tree structures to represent part-whole hierarchies.

28 UML Diagram

29 Façade Pattern Defines a high level interface that makes a sub system easier to use. Does not prevent an advanced user to use low level functionality. J2EE – Session Façade & Message Façade

30 UML Diagram

31 Proxy Provide a surrogate or placeholder for another object to control access to it. Provides identical interface as the original object has. Controls access to the original object and may be responsible for creating & destroying it

32 UML Diagram

33 Decorator Adding responsibilities to objects dynamically and without having to create a new class. Also known as “Wrapper” Used when sub-classing is impractical. Java I/O Streams is a good example.

34 UML Diagram

35 Flyweight Use sharing to support large numbers of fine-grained objects efficiently. Reusability of existing objects. Flyweight objects have two states intrinsic state & extrinsic state, that make them reusable.

36 UML Diagram

37 Example public class StringTest { public static void main(String[] args) { String fly = "fly", weight = "weight"; String fly2 = "fly", weight2 = "weight"; System.out.println(fly == fly2); System.out.println(weight == weight2); String distinctString = fly + weight; System.out.println(distinctString == "flyweight"); String flyweight = (fly + weight).intern(); System.out.println(flyweight == distinctString ); } } // TRUE // FALSE // TRUE

38 Strategy Pattern an object and its behavior are separated and put into two different classes. Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

39 UML Diagram

40 Thank You !


Download ppt "DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development."

Similar presentations


Ads by Google