Design Patterns
Design Patterns
Lesson03DesignPatterns (Part01)
ByCrishanthaNanayakkara
DDEA DDEA
The gap between the best software engineering practice and the average practice is very wide perhaps wider than the other engineering
What is a Pattern?
A pattern is a solution to a problem in a context Context : is the situation in which the pattern applies.
This should be a recurring situation
anyone can apply which resolves the goal and the set of constraints
What is a Pattern?
This means, a Design Pattern gives you a solution to a common recurring design problem However, The Design Patterns are not meant to be laws or rules; they are just guidelines that you can even alter to fit to your needs
CoreSecurityPatternCatalog
Reference:CoreSecurityPatterns
CoreJ2EEPatternCatalog
OO vs Design Patterns
Knowing the OO basics (abstraction, encapsulation, inheritance, polymorphism) will not make you a good designer!!!
Oh!!! Whatshould Idothen?
OO vs Design Patterns
Patterns show you how to build systems with good OO design qualities Patterns do not give you code. They give general solutions to design problems Patterns are not invented they are discovered
PatternOrganization2(Whetherapplytoclassesor objects)
ClassPatternsrelatedtoclasses,createdatcompiletime ObjectPatternsrelatedtoobjects,createdatruntime
Structural :
Adapter Bridge Composite Decorator Facade Flyweight Proxy
Asshownintheabovetabletherearetwoadapters.Oneadapterpatterncanbeappliedtoabstractionof classwhileotherinabstractionofobject
GOFDesign PatternAnalysis
StrategyPattern
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independentlyfromclientsthatuseit.
Design Principle 2
Program to an interface not to an implementation
Design Principle 3
Favor composition over inheritance
Reference:HeadFirstDesignPatterns
Thentheinheritedbehaviorwillbeattachedtoallinheritedclasseswhichdo nothavethatbehavior
CHANGE
Means,
Take what varies and encapsulate it. So it will not effect the rest of your code This results,
Separating behaviors
By separating behaviors from concrete classes, you will get the flexibility even change those behaviors with a minimal code change
In the previous example, if there are 10 sub classes extended or implemented the Flying behavior, you got to change all 10 sub classes if there is any change to the Flying behavior.
Separating behaviors
By separating behaviors from concrete classes, concrete classes will not need to know any implementation details of its own behaviors This approach is different to previous two approaches, which basically coupled the behavior to concrete classes
The behavior was implemented extending a super class The behavior was implemented providing a specialized interface to the sub class
Design Principle 2
Program to an interface, not an implementation
Does not mean a Java construct interfaces. This means a supertype However this is usually abstract class or an interface Doing this will create the opportunity to dynamic behavioral changes with the help of Polymorphism Programming to an implementation Dog dog = new Dog(); Programming to an interface / super type
The behavior variables are declared as interface variable types performQuack() and performFly() replaces quack() and fly() PerformQuack() and performFly() methods delegates behavior implementations to the behavior classes
Design Principle 3
Favor composition over inheritance
When you put more than one class together as class variables that is called composition This has the Has-A relationship This gives a lot of flexibility Lets you encapsulate a family of algorithms into their own set of classes By doing this allows you to change the behavior runtime
Here any concrete class behavior can be changed dynamically by using the setter methods specified within the abstract class
Reference:HeadFirstDesignPatterns
Reference:HeadFirstDesignPatterns
Reference:HeadFirstDesignPatterns
Reference:HeadFirstDesignPatterns
ObserverPattern
Observer2
Subscribers
Subject
Publisher
Observer3
When the in Subject changes, subscribers arenotified TheobserversaresubscribedtotheSubject to receive updates when the Subject data changes
Unsubscribed
ConcreteSubject:
Stores state of interest to ConcreteObserver objects and sends notifications to its observers upon state changes.
ConcreteObserver:
Maintains a reference to a ConcreteSubject object and a state that should stay consistent with the subject's.
Design Principle 4 Strive for loosely coupled designs between objects that interact Loosely coupled designs allow us to build flexible Object Oriented systems that can handle change How?
An Example
An Example
CommandPattern
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests,andsupportundoableoperations
Client
Invoker
Command
Receiver
Concrete Command
CommandandConcreteCommand
Receiver
Invoker
Client
References
Head First Design Patterns: by Eric Freeman & Elisabeth Freeman Design Patterns: Elements of Reusable Object Oriented Software: Erich Gamma, Richard Helm, Ralph Johnson & John Vilssides (GOF) (http://www.hillside.net) Core Security Patterns: Best Practices and Strategies J2EE Web Services and Identity Management: Chris Steel, Ramesh Nagappan, Ray Lai, Sun Microsystems Core J2EE Patterns, Best Practices and Design Strategies: Deepak Alur, John Crupi, Dan Malks, 2nd Edition, Prentice Hall/ Sun Microsystems, 2003 http://www.javaworld.com/jw-11-1998/jw-11-techniques.html