Download presentation
Presentation is loading. Please wait.
Published byElaine Sharlene Turner Modified over 9 years ago
1
Java Design Patterns 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 Creational: the creation of objects. Structural:how one object relates with another object. Structural:how one object relates with another object. Behavioral: Behavioral:communication mechanism between objects (invoke method)
5
Creational 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
Patterns 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() 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(); public abstract Button createButton();} class WinFactory:GUIFactory { public override Button createButton() { return new WinButton(); }} class MacFactory:GUIFactory { public override Button createButton(){ return new MacButton(); }} abstract class Button { public string caption; public abstract void paint(); }
11
Abstract Factory design pattern - example try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc: dumy"; "sa", ""); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc: dumy"; "sa", ""); Statement stmt = con.createStatement(); String query = "SELECT * FROM " + TABLE_NAME + ";"; ResultSet table = stmt.executeQuery(query); Statement stmt = con.createStatement(); String query = "SELECT * FROM " + TABLE_NAME + ";"; ResultSet table = stmt.executeQuery(query); } catch( Exception e ) { e.printStackTrace(); } 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"); studentRecords.Add("Jimmy"); studentRecords.Add("Sandra"); studentRecords.SetSortStrategy(new QuickSort()); studentRecords.Sort(); studentRecords.SetSortStrategy(new ShellSort()); studentRecords.Sort(); SortedList studentRecords = new SortedList(); studentRecords.Add("Samual"); studentRecords.Add("Jimmy"); studentRecords.Add("Sandra"); studentRecords.SetSortStrategy(new QuickSort()); studentRecords.Sort(); studentRecords.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 public override void Sort(ArrayList list) { list.Sort(); // Default is Quicksort }} class ShellSort : SortStrategy { public override void Sort(ArrayList list) { //list.ShellSort(); not-implemented public override void Sort(ArrayList list) { //list.ShellSort(); not-implemented } }
15
class SortedList { private ArrayList list = new ArrayList(); private SortStrategy sortstrategy; public void SetSortStrategy(SortStrategy sortstrategy) { this.sortstrategy = sortstrategy; } public void Add(string name) { private ArrayList list = new ArrayList(); private SortStrategy sortstrategy; public void SetSortStrategy(SortStrategy sortstrategy) { this.sortstrategy = sortstrategy; } public void Add(string name) { list.Add(name); } public void Sort() { public void Sort() { sortstrategy.Sort(list); sortstrategy.Sort(list); }} Strategy design pattern - example
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.
18
The End
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.