CSC/ECE 517 Fall 2007/wiki3 5 ns: Difference between revisions
(19 intermediate revisions by 2 users not shown) | |||
Line 2: | Line 2: | ||
=GRASP= | =GRASP= | ||
GRASP stands for '''G'''eneral '''R'''esponsibility '''A'''ssignment '''S'''oftware '''P'''atterns (or '''P'''rinciples), and they are the common object-oriented design patterns or principles that assign responsibility to classes and objects. | GRASP stands for '''G'''eneral '''R'''esponsibility '''A'''ssignment '''S'''oftware '''P'''atterns (or '''P'''rinciples), and they are the common object-oriented design patterns or principles that assign responsibility to classes and objects. | ||
Some of GRASP includes Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, Protected Variations. | Some of GRASP includes Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations. | ||
=What is Creator Pattern?= | =What is Creator Pattern?= | ||
Line 13: | Line 13: | ||
* B closely uses instances of A, or; | * B closely uses instances of A, or; | ||
* B has the necessary information for creating the new instance of A. | * B has the necessary information for creating the new instance of A. | ||
=What is Factory Pattern?= | |||
Factory pattern is one of the creational patterns which is used to answer the question "How should a class A instantiate one of the classes B,C,D but still be independent from them?" | |||
Factory pattern suggests the creation of a Factory class or a Factory method to which the responsibility of instantiating the object (one of the B,C,D classes) is delegated. This object instance is then returned to class A. For example, currently the factory method might be returning the instance of B, later the requirements might change and A needs to instantiate C. Then, the change in the code is only at the Factory method or Factory class. This ensures that the changes in requirements, require changes in minimum number of places throughout the code. | |||
=Creator Pattern vs Factory Pattern= | =Creator Pattern vs Factory Pattern= | ||
The subtle difference between the Creator and Factory pattern can be made out from the synopsis for both the patterns as put by Mark Grand in his book "Patterns in Java" | The subtle difference between the Creator and Factory pattern can be made out from the synopsis for both the patterns as put by Mark Grand in his book "Patterns in Java". | ||
<b><i>Synopsis for Creator Pattern</i></b> ( one of the GRASP patterns ) | <b><i>Synopsis for Creator Pattern</i></b> (one of the GRASP patterns) | ||
" Creator pattern is used to determine which class should create instances of a class based on the relationship between potential creator classes and the class to be instantiated " | "Creator pattern is used to determine which class should create instances of a class based on the relationship between potential creator classes and the class to be instantiated." | ||
<b><i>Synopsis for Factory | <b><i>Synopsis for Factory Pattern</i></b> (one of the Creational Patterns) | ||
" In Factory pattern, a class | "In Factory pattern, a class is organized so that it can instantiate other classes without being dependent on any of the classes it instantiates. This reusable class is able to remain independent of the classes it instantiates by delegating the choice of which class to instantiate to another object and referring to the newly created object through a common interface." | ||
Thus, the Creator Pattern gives us the guidelines for determining which class should be creating the instances of a particular class depending on the relationship between theses classes. The Factory Pattern provides guidelines to deal with the problem of creating objects without specifying the exact class of object that will be created.The factory method design pattern defines a separate method for creating the objects, which subclasses can then override to specify the derived type of product that will be created. More generally, the term factory method is often used to refer to any method whose main purpose is creation of objects. | Thus, the Creator Pattern gives us the guidelines for determining which class should be creating the instances of a particular class depending on the relationship between theses classes. The Factory Pattern provides guidelines to deal with the problem of creating objects without specifying the exact class of object that will be created.The factory method design pattern defines a separate method for creating the objects, which subclasses can then override to specify the derived type of product that will be created. More generally, the term factory method is often used to refer to any method whose main purpose is creation of objects. | ||
<u><i>Advantages of Creator Pattern</i></u> | <u><i>Advantages of Creator Pattern</i></u> | ||
# The creator pattern promotes low coupling by making instances of a class responsible for creating objects they need to reference | # The creator pattern promotes low coupling by making instances of a class responsible for creating objects they need to reference. | ||
# | # The referencing class itself creates the objects of the classes it references and hence it avoids being dependent on another class to create objects for itself. | ||
# By having the aggregate class create the child class, we ensure that the Information Expert, High Cohesion and | # By having the aggregate class create the child class, we ensure that the Information Expert, High Cohesion and Low coupling patterns also hold. | ||
<u><i>Advantages of Factory Pattern</i></u> | <u><i>Advantages of Factory Pattern</i></u> | ||
# The creation requester class is independent of the class of concrete product objects actually created. | # The creation requester class is independent of the class of concrete product objects actually created. | ||
# The set of product classes that can be instantiated may change dynamically | # The set of product classes that can be instantiated may change dynamically. | ||
From the advantages it might seem that Creator patterns contradicts what is proposed by the Factory Pattern. But the difference that needs to be noted is in the relationship between the classes. | |||
In Creator Pattern, the responsibility of object creation is given to the class itself ONLY IF the class aggregates the class of the object it creates. The Factory Pattern deals with the object creation by classes unrelated to the class of the object it instantiates. In this situation, Factory Pattern suggests the delegation of the responsibility to another class or method. | |||
= Example for Creator Pattern = | = Example for Creator Pattern = | ||
The following example was one of the best examples that we saw while searching this topic. The resources found online are generally brief, and use the same example for describing creator pattern, thus we feel the intuitiveness in this example makes it a better option than the one in the web. So we propose this example as the ''best example for teaching Creator Pattern in class. | |||
<u><i>Employee Time Keeping System </i></u> ( from Mark Grand's book "Patterns in Java" ) | <u><i>Employee Time Keeping System </i></u> ( from Mark Grand's book "Patterns in Java" ) | ||
This is used to determine how many hours an employee works during a | This example is used to determine how many hours an employee works during a PayPeriod. During a PayPeriod an employee works for periods of time called Shifts. The system learns about shifts worked by an employee as it receives TimeKeepingEvents. | ||
The classes used to represent this is shown in below Class Diagram: | The classes used to represent this is shown in below Class Diagram: | ||
Line 47: | Line 55: | ||
From the diagram we can infer that instances of PayPeriod class compose instances of Shift class. Instances of Shift class compose instances of TimeKeepingEvents class. The subclasses of TimeKeepingEvents represent the time employee started a shift, started a break, ended a break and ended a shift. | From the diagram we can infer that instances of PayPeriod class compose instances of Shift class. Instances of Shift class compose instances of TimeKeepingEvents class. The subclasses of TimeKeepingEvents represent the time employee started a shift, started a break, ended a break and ended a shift. | ||
Here | Here the problem is determining which classes are responsible for creating instances of Shift and TimeKeepingEvent classes... | ||
The Creator Pattern states that a class that composes or aggregates instances of another class in a good class to assign responsibility of creating instances of the composed or aggregated classes. | The Creator Pattern states that a class that composes or aggregates instances of another class in a good class to assign responsibility of creating instances of the composed or aggregated classes. | ||
By following the Creator pattern, creating instances of TimeKeepingEvent should be assigned to Shift class, and creating instances of Shift class should be assigned to PayPeriod. | |||
=Information Available on the Web= | =Information Available on the Web= | ||
The Creator Pattern does not have many information on the web at all. Most of the websites that we found, use the same approach and the same example for explaining the pattern. In addition to this most of the resources explaining creator pattern online are power point documents for some course's lecture notes which are prepared to be very brief. Here, we | The Creator Pattern does not have many information on the web at all. Most of the websites that we found, use the same approach and the same example for explaining the pattern. In addition to this most of the resources explaining creator pattern online are power point documents for some course's lecture notes which are prepared to be very brief. Here, we present the websites that are dealing with the Creator Pattern: | ||
[http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design) "Wikipedia"] is one of the good places to read, to get a better understanding of what GRASP is, and what patterns and principles are included in GRASP. It also briefly explains the Creator Pattern, and how | [http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design) "Wikipedia"] is one of the good places to read, to get a better understanding of what GRASP is, and what patterns and principles are included in GRASP. It also briefly explains the Creator Pattern, and how the pattern helps in making the decision that which class should create instances of another class. It provides no examples that illustrates this principle so it is not the best site for Creator Pattern online. | ||
[http://davidhayden.com/blog/dave/archive/2004/12/06/667.aspx "David Hayden"]'s blog on Creator Pattern | [http://davidhayden.com/blog/dave/archive/2004/12/06/667.aspx "David Hayden"]'s blog on Creator Pattern explains the Creator Pattern briefly, and provides one simple example about how Creator pattern can play a role when adding a new DataRow to a DataTable class. | ||
[http://books.google.com/books?id=r8i-4En_aa4C&pg=PA228&lpg=PA228&dq=%22creator+pattern%22+example&source=web&ots=mT9HKWvlQX&sig=zsygR_-22hq9SfNOQLSiAaEExy8#PPA216,M1 "Craig Larmann"]'s book, Applying UML and Pattern: An Object-Oriented Analysis and Design and the Unified Process, is an excellent book that has a thorough explanation on Creator Pattern and other GRASPs. Some of its sections are available online through google-books. This book explains the Creator Pattern and presents an example of a system with Sale, SalesLineItem and ProductSpecification objects and it discusses how it uses Creator pattern to decide which class is responsible for creating new instances of SalesLineItem. Surprisingly, many of the sources | [http://books.google.com/books?id=r8i-4En_aa4C&pg=PA228&lpg=PA228&dq=%22creator+pattern%22+example&source=web&ots=mT9HKWvlQX&sig=zsygR_-22hq9SfNOQLSiAaEExy8#PPA216,M1 "Craig Larmann"]'s book, Applying UML and Pattern: An Object-Oriented Analysis and Design and the Unified Process, is an excellent book that has a thorough explanation on Creator Pattern and other GRASPs. Some of its sections are available online through google-books. This book explains the Creator Pattern and presents an example of a system with Sale, SalesLineItem and ProductSpecification objects and it discusses how it uses Creator pattern to decide which class is responsible for creating new instances of SalesLineItem. Surprisingly, many of the sources listed here, use the example provided in this book for explaining creator pattern, so in our opinion, this is one of the best sources that one can read to learn and understand more about the Creator pattern since it explains it in whole lot more detail than other links provided. | ||
[http://www.augustana.ab.ca/~mohrj/courses/2003.fall/csc220/lecture_notes/responsibilities.html "Lecture Notes"] on GRASP from Augustana College describes the creator pattern briefly and gives the Sale example (which is originally from the book above), this website is a a good one that summarizes what is written in Craig Larmann's book, but it might not be the best place to start learning the Creator Pattern. | [http://www.augustana.ab.ca/~mohrj/courses/2003.fall/csc220/lecture_notes/responsibilities.html "Lecture Notes"] on GRASP from Augustana College describes the creator pattern briefly and gives the Sale example (which is originally from the book above), this website is a a good one that summarizes what is written in Craig Larmann's book, but it might not be the best place to start learning the Creator Pattern. | ||
Line 68: | Line 76: | ||
[http://www.soc.napier.ac.uk/module.php3?op=getresource&cloaking=no&resourceid=5938601 "Lecture Notes"] on GRASP from Napier University Edinburgh, also describes the pattern, and uses the Sale example. It also mentions briefly advantages of using this patterns and how it promotes low coupling. | [http://www.soc.napier.ac.uk/module.php3?op=getresource&cloaking=no&resourceid=5938601 "Lecture Notes"] on GRASP from Napier University Edinburgh, also describes the pattern, and uses the Sale example. It also mentions briefly advantages of using this patterns and how it promotes low coupling. | ||
[http://www.cse.ucsd.edu/classes/fa06/cse111/lectures/Lecture%206%20%20Design%20Evaluation%20and%20Intro%20to%20OO%20Patterns.ppt "Lecture Notes"] on GRASP from University of California San Diego explains the terminology used in Creator Pattern (for example: what "aggregates" or "contains" means), it also provides a different example about DatingRequest but the slides are very brief and they only | [http://www.cse.ucsd.edu/classes/fa06/cse111/lectures/Lecture%206%20%20Design%20Evaluation%20and%20Intro%20to%20OO%20Patterns.ppt "Lecture Notes"] on GRASP from University of California San Diego explains the terminology used in Creator Pattern (for example: what "aggregates" or "contains" means), it also provides a different example about DatingRequest but the slides are very brief and they contain only the sequence diagrams of the system so it is not very clear what the example is about and how creator pattern is implemented in it. | ||
[http://faculty.inverhills.edu/dlevitt/CS%202000%20(FP)/GRASP%20Patterns.pdf "Lecture Notes"] on GRASP from Inverhills Community College summarizes the whole points of the Creator Pattern in one slide, and also mentions about the steps of applying the pattern to the system. This slide might be a nice summary of the pattern as a whole but it's not necessarily a good place to start learning the subject. | [http://faculty.inverhills.edu/dlevitt/CS%202000%20(FP)/GRASP%20Patterns.pdf "Lecture Notes"] on GRASP from Inverhills Community College summarizes the whole points of the Creator Pattern in one slide, and also mentions about the steps of applying the pattern to the system. This slide might be a nice summary of the pattern as a whole but it's not necessarily a good place to start learning the subject. |
Latest revision as of 15:28, 28 November 2007
Take the Creator pattern and catalog the information on it available on the Web. Explain how it is different from the Factory pattern. Find good descriptions and good, concise, understandable examples. Tell which you consider the best to present to a class.
GRASP
GRASP stands for General Responsibility Assignment Software Patterns (or Principles), and they are the common object-oriented design patterns or principles that assign responsibility to classes and objects. Some of GRASP includes Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations.
What is Creator Pattern?
Creator Pattern is one of the GRASP approaches, which is developed to find an answer to the question of "Who creates an object of class A?" in object-oriented systems. Creator pattern suggests that Class B should create an instance of Class A if
- B aggregates instances of A, or;
- B contains instances of A, or;
- B records instances of A, or;
- B closely uses instances of A, or;
- B has the necessary information for creating the new instance of A.
What is Factory Pattern?
Factory pattern is one of the creational patterns which is used to answer the question "How should a class A instantiate one of the classes B,C,D but still be independent from them?"
Factory pattern suggests the creation of a Factory class or a Factory method to which the responsibility of instantiating the object (one of the B,C,D classes) is delegated. This object instance is then returned to class A. For example, currently the factory method might be returning the instance of B, later the requirements might change and A needs to instantiate C. Then, the change in the code is only at the Factory method or Factory class. This ensures that the changes in requirements, require changes in minimum number of places throughout the code.
Creator Pattern vs Factory Pattern
The subtle difference between the Creator and Factory pattern can be made out from the synopsis for both the patterns as put by Mark Grand in his book "Patterns in Java".
Synopsis for Creator Pattern (one of the GRASP patterns) "Creator pattern is used to determine which class should create instances of a class based on the relationship between potential creator classes and the class to be instantiated."
Synopsis for Factory Pattern (one of the Creational Patterns) "In Factory pattern, a class is organized so that it can instantiate other classes without being dependent on any of the classes it instantiates. This reusable class is able to remain independent of the classes it instantiates by delegating the choice of which class to instantiate to another object and referring to the newly created object through a common interface."
Thus, the Creator Pattern gives us the guidelines for determining which class should be creating the instances of a particular class depending on the relationship between theses classes. The Factory Pattern provides guidelines to deal with the problem of creating objects without specifying the exact class of object that will be created.The factory method design pattern defines a separate method for creating the objects, which subclasses can then override to specify the derived type of product that will be created. More generally, the term factory method is often used to refer to any method whose main purpose is creation of objects.
Advantages of Creator Pattern
- The creator pattern promotes low coupling by making instances of a class responsible for creating objects they need to reference.
- The referencing class itself creates the objects of the classes it references and hence it avoids being dependent on another class to create objects for itself.
- By having the aggregate class create the child class, we ensure that the Information Expert, High Cohesion and Low coupling patterns also hold.
Advantages of Factory Pattern
- The creation requester class is independent of the class of concrete product objects actually created.
- The set of product classes that can be instantiated may change dynamically.
From the advantages it might seem that Creator patterns contradicts what is proposed by the Factory Pattern. But the difference that needs to be noted is in the relationship between the classes.
In Creator Pattern, the responsibility of object creation is given to the class itself ONLY IF the class aggregates the class of the object it creates. The Factory Pattern deals with the object creation by classes unrelated to the class of the object it instantiates. In this situation, Factory Pattern suggests the delegation of the responsibility to another class or method.
Example for Creator Pattern
The following example was one of the best examples that we saw while searching this topic. The resources found online are generally brief, and use the same example for describing creator pattern, thus we feel the intuitiveness in this example makes it a better option than the one in the web. So we propose this example as the best example for teaching Creator Pattern in class.
Employee Time Keeping System ( from Mark Grand's book "Patterns in Java" )
This example is used to determine how many hours an employee works during a PayPeriod. During a PayPeriod an employee works for periods of time called Shifts. The system learns about shifts worked by an employee as it receives TimeKeepingEvents. The classes used to represent this is shown in below Class Diagram:
From the diagram we can infer that instances of PayPeriod class compose instances of Shift class. Instances of Shift class compose instances of TimeKeepingEvents class. The subclasses of TimeKeepingEvents represent the time employee started a shift, started a break, ended a break and ended a shift.
Here the problem is determining which classes are responsible for creating instances of Shift and TimeKeepingEvent classes...
The Creator Pattern states that a class that composes or aggregates instances of another class in a good class to assign responsibility of creating instances of the composed or aggregated classes.
By following the Creator pattern, creating instances of TimeKeepingEvent should be assigned to Shift class, and creating instances of Shift class should be assigned to PayPeriod.
Information Available on the Web
The Creator Pattern does not have many information on the web at all. Most of the websites that we found, use the same approach and the same example for explaining the pattern. In addition to this most of the resources explaining creator pattern online are power point documents for some course's lecture notes which are prepared to be very brief. Here, we present the websites that are dealing with the Creator Pattern:
"Wikipedia" is one of the good places to read, to get a better understanding of what GRASP is, and what patterns and principles are included in GRASP. It also briefly explains the Creator Pattern, and how the pattern helps in making the decision that which class should create instances of another class. It provides no examples that illustrates this principle so it is not the best site for Creator Pattern online.
"David Hayden"'s blog on Creator Pattern explains the Creator Pattern briefly, and provides one simple example about how Creator pattern can play a role when adding a new DataRow to a DataTable class.
"Craig Larmann"'s book, Applying UML and Pattern: An Object-Oriented Analysis and Design and the Unified Process, is an excellent book that has a thorough explanation on Creator Pattern and other GRASPs. Some of its sections are available online through google-books. This book explains the Creator Pattern and presents an example of a system with Sale, SalesLineItem and ProductSpecification objects and it discusses how it uses Creator pattern to decide which class is responsible for creating new instances of SalesLineItem. Surprisingly, many of the sources listed here, use the example provided in this book for explaining creator pattern, so in our opinion, this is one of the best sources that one can read to learn and understand more about the Creator pattern since it explains it in whole lot more detail than other links provided.
"Lecture Notes" on GRASP from Augustana College describes the creator pattern briefly and gives the Sale example (which is originally from the book above), this website is a a good one that summarizes what is written in Craig Larmann's book, but it might not be the best place to start learning the Creator Pattern.
"Lecture Notes" on GRASP from Worcester Polytechnic Institute is the brief summary of what is explained on Creator Pattern in Craig Larmann's book.
"Lecture Notes" on GRASP from Napier University Edinburgh, also describes the pattern, and uses the Sale example. It also mentions briefly advantages of using this patterns and how it promotes low coupling.
"Lecture Notes" on GRASP from University of California San Diego explains the terminology used in Creator Pattern (for example: what "aggregates" or "contains" means), it also provides a different example about DatingRequest but the slides are very brief and they contain only the sequence diagrams of the system so it is not very clear what the example is about and how creator pattern is implemented in it.
"Lecture Notes" on GRASP from Inverhills Community College summarizes the whole points of the Creator Pattern in one slide, and also mentions about the steps of applying the pattern to the system. This slide might be a nice summary of the pattern as a whole but it's not necessarily a good place to start learning the subject.
"Lecture Notes" on GRASP from New Jersey Institute of Technology explains the Sale example and how creator pattern is applied in detail, this slides are again the summary of Larmann's book is mentioned above, but they summarize the book in detail so one can understand the example by looking through the slides, without actually reading the book.
"Marc Conrad"'s presentation has a brief discussion on Creator Pattern, also he presents a Deposit Item example which is an easy-to-understand example.
"Paul Christmann"'s lecture notes on GRASP pattern, also uses the example from the Larmann's book but the example is explained in detail and where the creator pattern is applied in the system is easy-to-understand.
"Lecture Notes" from Marist College and "Lecture Notes" from Kutztown University uses Monopoly game example for explaining the creator pattern, even though a person who knows about this pattern can understand what the example is trying to demonstrate, but the explanation of the example can be improved.
References
- "David Hayden" http://davidhayden.com/blog/dave/archive/2004/12/06/667.aspx
- "Craig Larmann, Applying UML and Pattern: An Object-Oriented Analysis and Design and the Unified Process" http://books.google.com/books?id=r8i-4En_aa4C&pg=PA228&lpg=PA228&dq=%22creator+pattern%22+example&source=web&ots=mT9HKWvlQX&sig=zsygR_-22hq9SfNOQLSiAaEExy8#PPA216,M1
- "Lecture Notes on GRASP from Augustana College" http://www.augustana.ab.ca/~mohrj/courses/2003.fall/csc220/lecture_notes/responsibilities.html
- "Lecture Notes on GRASP from Worcester Polytechnic Institute" http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/pdf/CS4233%20Class%204.pdf
- "Lecture Notes on GRASP from Napier University Edinburgh" http://www.soc.napier.ac.uk/module.php3?op=getresource&cloaking=no&resourceid=5938601
- "Lecture Notes on GRASP from University of California San Diego" http://www.cse.ucsd.edu/classes/fa06/cse111/lectures/Lecture%206%20%20Design%20Evaluation%20and%20Intro%20to%20OO%20Patterns.ppt
- "Lecture Notes on GRASP from Inverhills Community College" http://faculty.inverhills.edu/dlevitt/CS%202000%20(FP)/GRASP%20Patterns.pdf
- "Lecture Notes on GRASP from New Jersey Institute of Technology" http://web.njit.edu/~gblank/cis683/Larman%20Chapter%2018.ppt
- "Marc Conrad's presentation" http://www.perisic.com/oosd/design/GRASP.ppt
- "Paul Christmann's notes" http://www.christmann.ws/ucis342/class7/class7.html
- "Lecture Notes from Kutztown University" http://faculty.kutztown.edu/spiegel/CSc520/PowerPoint/10_GRASP_1.ppt
- "Lecture Notes from Marist College" http://www.academic.marist.edu/~jzbv/SoftwareDevelopment/GRASP.ppt
- "Patterns in Java" Vol 1 and Vol 2 - Mark Grand - Wiley Computer Publishing