Java Design Patterns - PowerPoint PPT Presentation

About This Presentation
Title:

Java Design Patterns

Description:

Java Design Patterns What are design patterns? the best solution for a recurring problem a technique for making code more flexible by making it meet certain criteria ... – PowerPoint PPT presentation

Number of Views:463
Avg rating:3.0/5.0
Slides: 19
Provided by: JonA90
Category:

less

Transcript and Presenter's Notes

Title: Java Design Patterns


1
Java Design Patterns
2
What are design patterns?
  • the best solution for a recurring problem
  • a technique for making code more flexible by
    making it meet certain criteria

3
Why design patterns?
  • The primary goal is to help improve the quality
    of the software in terms of the software being
    reusable, maintainable, extensible, etc.
  • Reduce the development time

4
Three main categories of design patterns
  • Creational the creation of objects.
  • Structural how one object relates with another
    object.
  • Behavioral communication mechanism between
    objects (invoke method)

5
Creational Patterns
  • 1. Factory Method- Creates an instance of
    several derived classes
  • 2. Singleton- A class in which only a
    single instance can exist
  • 3. Abstract Factory- Creates an instance of
    several families of classes
  • 4. Prototype- A fully initialized instance
    to be copied or cloned
  • 5. Builder - Separates object construction
    from its representation

6
Structural Patterns
  • 1. Adapter- Match interfaces of different
    classes .
  • 2. Bridge- Separates an object's
    abstraction from its implementation.
  • 3. Composite- A tree structure of simple and
    composite objects.
  • 4. Decorator-Add responsibilities to objects
    dynamically.
  • 5. Facade- A single class that represents an
    entire subsystem.
  • 6. Flyweight- A fine-grained instance used for
    efficient sharing.
  • 7. Proxy- An object representing another
    object.

7
Behavioral Patterns
  • 1. Mediator- Defines simplified
    communication between classes.
  • 2. Memento- Capture and restore an object's
    internal state.
  • 3. Interpreter- A way to include language
    elements in a program.
  • 4. Iterator- Sequentially access the
    elements of a collection.
  • 5. Chain of Resp - A way of passing a request
    between a chain of objects.
  • 6. Command- Encapsulate a command request as
    an object.
  • 7. State- Alter an object's behavior
    when its state changes.
  • 8. Strategy- Encapsulates an algorithm
    inside a class.
  • 9. Observer - A way of notifying change to a
    number of classes.
  • 10. Template Method- Defer the exact steps of
    an algorithm to a subclass.
  • 11. Visitor- Defines a new operation to a
    class without change.

8
Examples
9
Singleton design pattern
  • Creational pattern
  • ensure that a class has only one instance, and to
    provide a global point of access to it
  • Example
  • Class SomeClass
  • static SomeClass singleTonInstance null
  • static SomeClass GetInstance()
  • if(singleTonInstance null)
  • singleTonInstance new SomeClass()
  • return singleTonInstance

10
Factory design pattern - example
  • abstract class GUIFactory
  • public static GUIFactory getFactory()
  • int sys readFromConfigFile("OS_TYPE")
  • return sys 0 ? new WinFactory() new
    OSXFactory()
  • public abstract Button createButton()
  • class WinFactoryGUIFactory
  • public override Button createButton()
  • return new WinButton()
  • class MacFactoryGUIFactory
  • public override Button createButton()
  • return new MacButton()
  • abstract class Button
  • public string caption

11
Abstract Factory design pattern - example
  •  try             Class.forName("sun.jdbc.odbc.Jd
    bcOdbcDriver")            
     Connection con  DriverManager.getConnection("jd
    bcodbcdumy" "sa", "")           
  •  Statement stmt  con.createStateme
    nt() String query "SELECT FROM "
    TABLE_NAME "" ResultSet table
    stmt.executeQuery(query)
  •   
  •   catch( Exception e )     e.printStackTrace() 
             

12
Decorator design pattern
  • Structural Pattern
  • Avoid excessive sub-classing and gain run time
    flexibility
  • Example
  • Java.IO package
  • BufferedReader br  new BufferedReader(
  • new InputStreamReader(           
    new FileInputStream(inFile)))
  • All derives from abstract io.Reader

13
Strategy design pattern
  • Behavioral Pattern
  • defines a family of interchangeable encapsulated
    algorithms that receives the same input type and
    provides the same output type in different
    manners that can be determined in run-time.
  • static void Main(
  • SortedList studentRecords new
    SortedList()studentRecords.Add("Samual")studen
    tRecords.Add("Jimmy")studentRecords.Add("Sandra"
    )    studentRecords.SetSortStrategy(new
    QuickSort())studentRecords.Sort()studentRecord
    s.SetSortStrategy(new ShellSort())studentRecords
    .Sort()  

14
Strategy design pattern - example
  • abstract class SortStrategy
  • public abstract void Sort(ArrayList list)
  • class QuickSort SortStrategy
  • public override void Sort(ArrayList
    list)      list.Sort() // Default is
    Quicksort       
  • class ShellSort SortStrategy
  • public override void Sort(ArrayList list)
          //list.ShellSort() not-implemented      
  •   

15
Strategy design pattern - example
  • class SortedList
  • private ArrayList list new ArrayList()private
    SortStrategy sortstrategypublic void
    SetSortStrategy(SortStrategy sortstrategy)     
     this.sortstrategy sortstrategypublic void
    Add(string name)      
  • list.Add(name)
  •     
  • public void Sort()   
  •   sortstrategy.Sort(list) 
  •       

16
Facade design pattern
  • Structural Pattern
  • Provide a unified interface to a set of
    interfaces in a subsystem without damaging the
    genric form of the sub system.

17
(No Transcript)
18
The End
Write a Comment
User Comments (0)
About PowerShow.com