Architecture and design patterns - PowerPoint
Document Sample


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
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
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)
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
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.
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.
Examples
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;
}
}
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 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();
}
Abstract Factory design pattern -
example
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);
}
catch( Exception e ) {
e.printStackTrace();
}
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
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();
}
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
}
}
Strategy design pattern - example
class SortedList
{
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()
{
sortstrategy.Sort(list);
}
}
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.
The End
Get documents about "