際際滷

際際滷Share a Scribd company logo
Design Patterns Introduction to  Design Patterns Rafael Coutinho  -  [email_address]
Agenda Motivation  Brief history  Definition Categories Examples Conclusion Questions
Motivation Context: Home to live in Problem: Protect home from rain Solutions: Live in a cave Live in a  tent  Build a house with a roof
Motivation Its a solution, not a receipt
Motivation Advantages of a standard design Speed up the development process More robust solution Easy to maintain Easy to evolve Allow beginners to act as experienced professional
Brief history 1977 - Christopher Alexander - Architecture A Pattern Language: Towns, Buildings, Construction Mid-to-late 1980  Object oriented use 1995 - Design Patterns: Elements of Reusable Object-Oriented Software Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides  Gang of Four
Definition Christopher Alexander  "Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice"
Definition A design pattern is defined using the following template: Pattern name and classification:  A conceptual handle and category for the pattern Intent:  What problem does the pattern address? Also known as:  Other common names for the pattern Motivation:  A scenario that illustrates the problem Applicability:  In what situations can the pattern be used? Structure:  Diagram UML Participants:  Classes and objects in design
Definition A design pattern is defined using the following template: Collaborations:  How classes and objects in the design collaborate Consequences:  What objectives does the pattern achieve? What are the tradeoffs? Implementation:  Implementation details to consider, language-specific issues Sample code:  Sample code in a selected language (Smalltalk, Java or C++) Known uses:  Examples from the real world Related patterns:  Comparison and discussion of related patterns
Sample Singleton  Creational Pattern Pattern name and Classification Ensure a class only has one (or a limited number of) instance, and provide a global point of access to it. Intention
Singleton Singleton define a getInstance operation that lets clients access its unique instance.   Structure and Participants
Singleton Controlled access to sole instance. Improvement over global variables. Permits a variable number of instances Consequences
Singleton Implementation and Code public class Singleton { private  static Singleton instance; private  Singleton(){ } public static synchronized  Singleton getInstance() { if (instance ==  null ) { instance =  new  Singleton(); } return  instance; } }
Singleton Motivation Printers Files Applicability One instance of a class Well-known access point
Singleton Examples: java.lang.Runtime   getRuntime Known Uses Abstract Factory Related Patterns
Categories Fundamental  Creational Structural Behavioral
Fundamental Patterns They are fundamental in the sense that they are widely used by other patterns or are frequently used in a large number of programs.   Interface Immutable Proxy*
Structural Patterns A re concerned with how classes and objects are composed to form larger structures  Structural  class  patterns use inheritance to compose interfaces or implementations  Rather than composing interfaces or implementations, structural  object  patterns describe ways to compose objects to realize new functionality  Proxy Facade Composite Decorator Adapter
Behavioral Patterns Patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication   Related to interaction and responsability. Design Patterns (GoF): Observer Strategy Visitor Chain of Responsability  Command Iterator
Creational Patterns Creational design patterns abstract the instantiation process   Help make a system independent of how its objects are created, composed, and represented   Encapsulate knowledge about which concrete classes the system uses.   Hide how instances of classes are created and put together   Design Patterns: Singleton Abstract Factory Factory Method Prototype
Some More Patterns Samples Structural  Decorator Creational  Abstract Factory
Decorator Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality Intention Motivation
Decorator To add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects For responsibilities that can be withdrawn When extension by subclassing is impractical. Sometimes a large number of independent extensions are possible and would produce an explosion of subclasses to support every combination. Or a class definition may be hidden or otherwise unavailable for subclassing Applicability
Decorator Structure and Participants
Decorator Decorator forwards requests to its Component object. It may optionally perform additional operations before and after forwarding the request More flexibility than static inheritance Avoids feature-laden classes high up in the hierarchy A decorator and its component aren't identical Lots of little objects Consequences
Decorator Implementation and Code public   class  LowerCaseInputStream  extends   FilterInputStream  {   public  LowerCaseInputStream( InputStream  in) {   super ( in );   }   public   int  read()  throws   IOException  {   int  c =  super .read();   return  ( c == -1 ? c :  Character .toLowerCase( ( char )c ) );   }   public  int read( byte [] b,  int  offset,  int  len)  throws   IOException  {   int  result = super.read(b, offset, len);   for  ( int  i = offset; i < offset+result; i++)  {   b[i] = ( byte ) Character .toLowerCase( ( char )b[i] );   }   return  result;   } }   public   class  InputTest {   public   static   void  main( String [] args) {   int  c;   try  {   InputStream  in =  new  LowerCaseInputStream(  new   BufferedInputStream (  new   FileInputStream ( test.txt ) ) );   while ( (c = in.read())  >=  0 ) {   System . out . print ( ( char )c );   }   in.close();   }  catch  ( IOException  e) {   e.printStackTrace();   }   } }
Decorator Adapter Composite Strategy Known Uses Related Pattners
Abstract Factory   Provide an interface for creating families of related or dependent objects without specifying their concrete classes. Intention Motivation GUI
Abstract Factory independence of how its products are created, composed and represented   a system should be configured with one of multiple families of products   a family of related product objects is designed to be used together.   provide a class library of products, and reveal just their interfaces, not their implementations. Applicability
Abstract Factory Structure and Participants
Abstract Factory It isolates concrete classes. It makes exchanging product families easy. It promotes consistency among products. Supporting new kinds of products is hard. (new methods) Consequences
Abstract Factory public interface class  DAOFactory {   public  User getUser(); } public class  MySQLDAOFactory  implements  DAOFactory { public  User getUser() { MySqlConnection conn = new  MySqlConnection();   User u = new MySqlUser();      u.setName(rs.getString(NAME)); return   u ; } } public class  Client { public static void  main(String[] args){ DAOFactory factory =  new  MySQLDAOFactory(); User userDao = factory.getUser(); } } public class  Db2DAOFactory  implements   DAOFactory { public  User getUser() { Db2Connection conn = new  Db2Connection();   User u = new Db2User();      u.setName(rs.getString(NAME)); return   u ; } }
Abstract Factory Structure and Participants
Abstract Factory Factory Method  Singleton Prototype Known Uses javax.net.SocketFactory SocketFactory.getDefault() Related Patterns
Rational Software Architect
Conclusion Design Patterns  its based on three aspects: Context, problem and solution   Can be implemented in N ways Speed up development Allow easier maintenance Easy to extend the solution Improve the solution
Resources Design Patterns bestseller book: Erich Gamma, Richard Helm, Ralph  Johnson and John Vlissides , Design Patterns: Elements of Reusable Object-Oriented Software , Addison-Wesley, 1994.   ISBN 0-201-63361-2 Wikipedia website  http:// en.wikipedia.org/wiki/Adapter_pattern  and  http:// en.wikipedia.org/wiki/Decorator_pattern . Object Oriented System course from Department of Computer Science of University of Texas at San Antonio  http://vip.cs.utsa.edu/classes/cs4773s2004/index.html . Joseph Bergin professor at Pace University personal website http://csis.pace.edu/~bergin/
Questions

More Related Content

What's hot (20)

C# Constructors
C# ConstructorsC# Constructors
C# Constructors
Prem Kumar Badri
Constructor
ConstructorConstructor
Constructor
abhay singh
Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design pattern
11prasoon
Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3
Naga Muruga
PATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design PatternsPATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design Patterns
Michael Heron
Design patterns illustrated-2015-03
Design patterns illustrated-2015-03Design patterns illustrated-2015-03
Design patterns illustrated-2015-03
Herman Peeren
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
Satheesh Sukumaran
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
Gaurav Tyagi
Design patterns creational patterns
Design patterns creational patternsDesign patterns creational patterns
Design patterns creational patterns
Malik Sajid
Factory Design Pattern
Factory Design PatternFactory Design Pattern
Factory Design Pattern
Jaswant Singh
Design Patterns
Design PatternsDesign Patterns
Design Patterns
adil raja
Introduction to Design Patterns and Singleton
Introduction to Design Patterns and SingletonIntroduction to Design Patterns and Singleton
Introduction to Design Patterns and Singleton
Jonathan Simon
Singleton Pattern
Singleton PatternSingleton Pattern
Singleton Pattern
Reber Novanta
Software Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural PatternsSoftware Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural Patterns
Sergey Aganezov
Design Patterns Illustrated
Design Patterns IllustratedDesign Patterns Illustrated
Design Patterns Illustrated
Herman Peeren
Power mock
Power mockPower mock
Power mock
Piyush Mittal
Junit, mockito, etc
Junit, mockito, etcJunit, mockito, etc
Junit, mockito, etc
Yaron Karni
Introduction to design_patterns
Introduction to design_patternsIntroduction to design_patterns
Introduction to design_patterns
amitarcade
Mockito
MockitoMockito
Mockito
sudha rajamanickam
All about unit testing using (power) mock
All about unit testing using (power) mockAll about unit testing using (power) mock
All about unit testing using (power) mock
Pranalee Rokde
Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design pattern
11prasoon
Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3
Naga Muruga
PATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design PatternsPATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design Patterns
Michael Heron
Design patterns illustrated-2015-03
Design patterns illustrated-2015-03Design patterns illustrated-2015-03
Design patterns illustrated-2015-03
Herman Peeren
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
Gaurav Tyagi
Design patterns creational patterns
Design patterns creational patternsDesign patterns creational patterns
Design patterns creational patterns
Malik Sajid
Factory Design Pattern
Factory Design PatternFactory Design Pattern
Factory Design Pattern
Jaswant Singh
Design Patterns
Design PatternsDesign Patterns
Design Patterns
adil raja
Introduction to Design Patterns and Singleton
Introduction to Design Patterns and SingletonIntroduction to Design Patterns and Singleton
Introduction to Design Patterns and Singleton
Jonathan Simon
Singleton Pattern
Singleton PatternSingleton Pattern
Singleton Pattern
Reber Novanta
Software Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural PatternsSoftware Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural Patterns
Sergey Aganezov
Design Patterns Illustrated
Design Patterns IllustratedDesign Patterns Illustrated
Design Patterns Illustrated
Herman Peeren
Junit, mockito, etc
Junit, mockito, etcJunit, mockito, etc
Junit, mockito, etc
Yaron Karni
Introduction to design_patterns
Introduction to design_patternsIntroduction to design_patterns
Introduction to design_patterns
amitarcade
All about unit testing using (power) mock
All about unit testing using (power) mockAll about unit testing using (power) mock
All about unit testing using (power) mock
Pranalee Rokde

Viewers also liked (6)

Design Pattern - Factory Method Pattern
Design Pattern - Factory Method PatternDesign Pattern - Factory Method Pattern
Design Pattern - Factory Method Pattern
Mudasir Qazi
Design Patterns - The Ultimate Blueprint for Software
Design Patterns - The Ultimate Blueprint for SoftwareDesign Patterns - The Ultimate Blueprint for Software
Design Patterns - The Ultimate Blueprint for Software
Edureka!
Factory Method Pattern
Factory Method PatternFactory Method Pattern
Factory Method Pattern
Anjan Kumar Bollam
Design pattern - part 1
Design pattern - part 1Design pattern - part 1
Design pattern - part 1
Jieyi Wu
Design Patterns - 01 Introduction and Decorator Pattern
Design Patterns - 01 Introduction and Decorator PatternDesign Patterns - 01 Introduction and Decorator Pattern
Design Patterns - 01 Introduction and Decorator Pattern
eprafulla
Design Patterns
Design PatternsDesign Patterns
Design Patterns
soms_1
Design Pattern - Factory Method Pattern
Design Pattern - Factory Method PatternDesign Pattern - Factory Method Pattern
Design Pattern - Factory Method Pattern
Mudasir Qazi
Design Patterns - The Ultimate Blueprint for Software
Design Patterns - The Ultimate Blueprint for SoftwareDesign Patterns - The Ultimate Blueprint for Software
Design Patterns - The Ultimate Blueprint for Software
Edureka!
Design pattern - part 1
Design pattern - part 1Design pattern - part 1
Design pattern - part 1
Jieyi Wu
Design Patterns - 01 Introduction and Decorator Pattern
Design Patterns - 01 Introduction and Decorator PatternDesign Patterns - 01 Introduction and Decorator Pattern
Design Patterns - 01 Introduction and Decorator Pattern
eprafulla
Design Patterns
Design PatternsDesign Patterns
Design Patterns
soms_1

Similar to Design Patterns (20)

Introduction To Design Patterns
Introduction To Design PatternsIntroduction To Design Patterns
Introduction To Design Patterns
sukumarraju6
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docx
danhaley45372
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
Pankhuree Srivastava
M04_DesignPatterns software engineering.ppt
M04_DesignPatterns software engineering.pptM04_DesignPatterns software engineering.ppt
M04_DesignPatterns software engineering.ppt
ssuser2d043c
1. Mini seminar intro
1. Mini seminar intro1. Mini seminar intro
1. Mini seminar intro
Leonid Maslov
chapter 5 Objectdesign.ppt
chapter 5 Objectdesign.pptchapter 5 Objectdesign.ppt
chapter 5 Objectdesign.ppt
TemesgenAzezew
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
Luis Valencia
27418524 design-patterns-dot-net-with-examples
27418524 design-patterns-dot-net-with-examples27418524 design-patterns-dot-net-with-examples
27418524 design-patterns-dot-net-with-examples
Quang Suma
M04 Design Patterns
M04 Design PatternsM04 Design Patterns
M04 Design Patterns
Dang Tuan
Design Pattern For C# Part 1
Design Pattern For C# Part 1Design Pattern For C# Part 1
Design Pattern For C# Part 1
Shahzad
Software Patterns
Software PatternsSoftware Patterns
Software Patterns
bonej010
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .Net
Greg Sohl
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Steven Smith
Design patterns
Design patternsDesign patterns
Design patterns
Anas Alpure
Framework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users GroupFramework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users Group
brada
Prophecy Of Design Patterns
Prophecy Of Design PatternsProphecy Of Design Patterns
Prophecy Of Design Patterns
pradeepkothiyal
Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2
Savio Sebastian
Module 4: UML In Action - Design Patterns
Module 4:  UML In Action - Design PatternsModule 4:  UML In Action - Design Patterns
Module 4: UML In Action - Design Patterns
jaden65832
Patterns in Python
Patterns in PythonPatterns in Python
Patterns in Python
dn
Framework engineering JCO 2011
Framework engineering JCO 2011Framework engineering JCO 2011
Framework engineering JCO 2011
YoungSu Son
Introduction To Design Patterns
Introduction To Design PatternsIntroduction To Design Patterns
Introduction To Design Patterns
sukumarraju6
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docx
danhaley45372
M04_DesignPatterns software engineering.ppt
M04_DesignPatterns software engineering.pptM04_DesignPatterns software engineering.ppt
M04_DesignPatterns software engineering.ppt
ssuser2d043c
1. Mini seminar intro
1. Mini seminar intro1. Mini seminar intro
1. Mini seminar intro
Leonid Maslov
chapter 5 Objectdesign.ppt
chapter 5 Objectdesign.pptchapter 5 Objectdesign.ppt
chapter 5 Objectdesign.ppt
TemesgenAzezew
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
Luis Valencia
27418524 design-patterns-dot-net-with-examples
27418524 design-patterns-dot-net-with-examples27418524 design-patterns-dot-net-with-examples
27418524 design-patterns-dot-net-with-examples
Quang Suma
M04 Design Patterns
M04 Design PatternsM04 Design Patterns
M04 Design Patterns
Dang Tuan
Design Pattern For C# Part 1
Design Pattern For C# Part 1Design Pattern For C# Part 1
Design Pattern For C# Part 1
Shahzad
Software Patterns
Software PatternsSoftware Patterns
Software Patterns
bonej010
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .Net
Greg Sohl
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Steven Smith
Design patterns
Design patternsDesign patterns
Design patterns
Anas Alpure
Framework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users GroupFramework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users Group
brada
Prophecy Of Design Patterns
Prophecy Of Design PatternsProphecy Of Design Patterns
Prophecy Of Design Patterns
pradeepkothiyal
Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2
Savio Sebastian
Module 4: UML In Action - Design Patterns
Module 4:  UML In Action - Design PatternsModule 4:  UML In Action - Design Patterns
Module 4: UML In Action - Design Patterns
jaden65832
Patterns in Python
Patterns in PythonPatterns in Python
Patterns in Python
dn
Framework engineering JCO 2011
Framework engineering JCO 2011Framework engineering JCO 2011
Framework engineering JCO 2011
YoungSu Son

Design Patterns

  • 1. Design Patterns Introduction to Design Patterns Rafael Coutinho - [email_address]
  • 2. Agenda Motivation Brief history Definition Categories Examples Conclusion Questions
  • 3. Motivation Context: Home to live in Problem: Protect home from rain Solutions: Live in a cave Live in a tent Build a house with a roof
  • 4. Motivation Its a solution, not a receipt
  • 5. Motivation Advantages of a standard design Speed up the development process More robust solution Easy to maintain Easy to evolve Allow beginners to act as experienced professional
  • 6. Brief history 1977 - Christopher Alexander - Architecture A Pattern Language: Towns, Buildings, Construction Mid-to-late 1980 Object oriented use 1995 - Design Patterns: Elements of Reusable Object-Oriented Software Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides Gang of Four
  • 7. Definition Christopher Alexander &quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&quot;
  • 8. Definition A design pattern is defined using the following template: Pattern name and classification: A conceptual handle and category for the pattern Intent: What problem does the pattern address? Also known as: Other common names for the pattern Motivation: A scenario that illustrates the problem Applicability: In what situations can the pattern be used? Structure: Diagram UML Participants: Classes and objects in design
  • 9. Definition A design pattern is defined using the following template: Collaborations: How classes and objects in the design collaborate Consequences: What objectives does the pattern achieve? What are the tradeoffs? Implementation: Implementation details to consider, language-specific issues Sample code: Sample code in a selected language (Smalltalk, Java or C++) Known uses: Examples from the real world Related patterns: Comparison and discussion of related patterns
  • 10. Sample Singleton Creational Pattern Pattern name and Classification Ensure a class only has one (or a limited number of) instance, and provide a global point of access to it. Intention
  • 11. Singleton Singleton define a getInstance operation that lets clients access its unique instance. Structure and Participants
  • 12. Singleton Controlled access to sole instance. Improvement over global variables. Permits a variable number of instances Consequences
  • 13. Singleton Implementation and Code public class Singleton { private static Singleton instance; private Singleton(){ } public static synchronized Singleton getInstance() { if (instance == null ) { instance = new Singleton(); } return instance; } }
  • 14. Singleton Motivation Printers Files Applicability One instance of a class Well-known access point
  • 15. Singleton Examples: java.lang.Runtime getRuntime Known Uses Abstract Factory Related Patterns
  • 16. Categories Fundamental Creational Structural Behavioral
  • 17. Fundamental Patterns They are fundamental in the sense that they are widely used by other patterns or are frequently used in a large number of programs. Interface Immutable Proxy*
  • 18. Structural Patterns A re concerned with how classes and objects are composed to form larger structures Structural class patterns use inheritance to compose interfaces or implementations Rather than composing interfaces or implementations, structural object patterns describe ways to compose objects to realize new functionality Proxy Facade Composite Decorator Adapter
  • 19. Behavioral Patterns Patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication Related to interaction and responsability. Design Patterns (GoF): Observer Strategy Visitor Chain of Responsability Command Iterator
  • 20. Creational Patterns Creational design patterns abstract the instantiation process Help make a system independent of how its objects are created, composed, and represented Encapsulate knowledge about which concrete classes the system uses. Hide how instances of classes are created and put together Design Patterns: Singleton Abstract Factory Factory Method Prototype
  • 21. Some More Patterns Samples Structural Decorator Creational Abstract Factory
  • 22. Decorator Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality Intention Motivation
  • 23. Decorator To add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects For responsibilities that can be withdrawn When extension by subclassing is impractical. Sometimes a large number of independent extensions are possible and would produce an explosion of subclasses to support every combination. Or a class definition may be hidden or otherwise unavailable for subclassing Applicability
  • 24. Decorator Structure and Participants
  • 25. Decorator Decorator forwards requests to its Component object. It may optionally perform additional operations before and after forwarding the request More flexibility than static inheritance Avoids feature-laden classes high up in the hierarchy A decorator and its component aren't identical Lots of little objects Consequences
  • 26. Decorator Implementation and Code public class LowerCaseInputStream extends FilterInputStream { public LowerCaseInputStream( InputStream in) { super ( in ); } public int read() throws IOException { int c = super .read(); return ( c == -1 ? c : Character .toLowerCase( ( char )c ) ); } public int read( byte [] b, int offset, int len) throws IOException { int result = super.read(b, offset, len); for ( int i = offset; i < offset+result; i++) { b[i] = ( byte ) Character .toLowerCase( ( char )b[i] ); } return result; } } public class InputTest { public static void main( String [] args) { int c; try { InputStream in = new LowerCaseInputStream( new BufferedInputStream ( new FileInputStream ( test.txt ) ) ); while ( (c = in.read()) >= 0 ) { System . out . print ( ( char )c ); } in.close(); } catch ( IOException e) { e.printStackTrace(); } } }
  • 27. Decorator Adapter Composite Strategy Known Uses Related Pattners
  • 28. Abstract Factory Provide an interface for creating families of related or dependent objects without specifying their concrete classes. Intention Motivation GUI
  • 29. Abstract Factory independence of how its products are created, composed and represented a system should be configured with one of multiple families of products a family of related product objects is designed to be used together. provide a class library of products, and reveal just their interfaces, not their implementations. Applicability
  • 30. Abstract Factory Structure and Participants
  • 31. Abstract Factory It isolates concrete classes. It makes exchanging product families easy. It promotes consistency among products. Supporting new kinds of products is hard. (new methods) Consequences
  • 32. Abstract Factory public interface class DAOFactory { public User getUser(); } public class MySQLDAOFactory implements DAOFactory { public User getUser() { MySqlConnection conn = new MySqlConnection(); User u = new MySqlUser(); u.setName(rs.getString(NAME)); return u ; } } public class Client { public static void main(String[] args){ DAOFactory factory = new MySQLDAOFactory(); User userDao = factory.getUser(); } } public class Db2DAOFactory implements DAOFactory { public User getUser() { Db2Connection conn = new Db2Connection(); User u = new Db2User(); u.setName(rs.getString(NAME)); return u ; } }
  • 33. Abstract Factory Structure and Participants
  • 34. Abstract Factory Factory Method Singleton Prototype Known Uses javax.net.SocketFactory SocketFactory.getDefault() Related Patterns
  • 36. Conclusion Design Patterns its based on three aspects: Context, problem and solution Can be implemented in N ways Speed up development Allow easier maintenance Easy to extend the solution Improve the solution
  • 37. Resources Design Patterns bestseller book: Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides , Design Patterns: Elements of Reusable Object-Oriented Software , Addison-Wesley, 1994. ISBN 0-201-63361-2 Wikipedia website http:// en.wikipedia.org/wiki/Adapter_pattern and http:// en.wikipedia.org/wiki/Decorator_pattern . Object Oriented System course from Department of Computer Science of University of Texas at San Antonio http://vip.cs.utsa.edu/classes/cs4773s2004/index.html . Joseph Bergin professor at Pace University personal website http://csis.pace.edu/~bergin/