Upload
guillaume-agis
View
257
Download
1
Embed Size (px)
Citation preview
How to design an application correctly?
Touch Surgery Lunch Presentation 03/02/2014
What is a design pattern?
Definition : Represent the best practices used by experienced OO software developers.Solutions to general problems that software developers faced during software development.
2 main usages :
• Common platform for developers : provide a standard terminology and are specific to particular scenario
• Best Practices : best solutions to certain problems faced during software development
=> 23 different design patterns
• Creational Patterns
• Structural Patterns
• Behavioral Patterns
Type of design patterns
Creational Patterns
Creation object
flexibility
Builder & Factory pattern
Prototype pattern
• Specifying the kind of objects to create using a prototypical instance
• Creating new objects by copying this prototypeIt is more convenient to copy an existing instance than to create a new one.
• Avoid using “new” operator.
Ex : copy set of data from the DB or copy container of objects
Prototype patternpublic interface Prototype {
public abstract Object clone ( );}
public class ConcretePrototype implements Prototype {
// Data setpublic Object clone() {
return super.clone();}
}
public class Client {
public static void main( String arg[] ) {
ConcretePrototype obj1= new ConcretePrototype ();// fill obj1 with large amount of data / object or heavy tasks
ConcretePrototype obj2 = ConcretePrototype)obj1.clone();
}
}
Structural Patterns
Concept of inheritance
Compose objects to obtain new functionalities.
Bridge Patterns
The implementation of an abstraction can be configured and even switched at run-time.Abstraction and Implementor hierarchies can be extended independently
Example : GUI Frameworks use the bridge pattern to separate abstractions from platform specific implementation (for example on Windows or linux).
Decouple abstraction from implementation so that the two can vary independently.
Bridge Patterns
Decorator Patterns
Add/remove additional responsibilities dynamically to an
objectExample : Extending capabilities of a
Graphical Window at runtime
Flyweight Patterns Minimise memory use by sharing as much data as possible with similar
objects.
Exemple : ressources (in video games)
saves memory, depends on the number of flyweight categories saved
public class ShapeFactory { private static final HashMap<String, Shape> circleMap = new HashMap();
public static Shape getCircle(String color) { Circle circle = (Circle)circleMap.get(color);
if(circle == null) { circle = new Circle(color); circleMap.put(color, circle); System.out.println("Creating circle of color : " + color); } return circle; }}
Behavioral Patterns
Communication between objects
Command Patterns Encapsulate a request as an object, in a queue. Can be parameterise
- Another objects can manipulate the request even without knowing any information about it.- The command is just a link between the receiver and the actions that carry out the request.- The command implements everything itself, without sending anything to the receiver.
Mediator Patterns
Intermediary / play the role of a controller between objects
Design reusable componentsLoose coupling between objects
Observer Patterns
The "View" part of Model-View-Controller.
Defines a one-to-many relationship : one object changes state, the others are notified and updated automatically.
Example : subscription NL, auction