CSC/ECE 517 Fall 2007/wiki3 5 ab: Difference between revisions
(113 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
__TOC__ | __TOC__ | ||
=Problem Statement = | ==Problem Statement == | ||
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 | ''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'' | ||
==Introduction== | |||
The creator design pattern is a part of the family of design patterns collectively referred to as (GRASP) General Responsibility Assignment Software Patterns. The various patterns or principles associated with GRASP are creator, controller, low coupling, high cohesion, polymorphism and Factory pattern. These patterns are critical to any software development project. We specifically look into the creator pattern of GRASP and compare it with the factory pattern. | |||
=The | ==The Creator Pattern == | ||
The creator pattern under GRASP is mainly associated with delegating the creation of an object of a certain class to another responsible class. Object creations is one of the most important aspects of object oriented design and hence a good design should be associated with proper delegation of object creations to appropriate classes so that it results in maximum reuse and minimum coupling. | |||
To figure out which class should be delegated the responsibility of creating objects of another class, the following set of rules should be followed. | |||
= | A certain class X should be associated with creating objects of class Y if any of the following is true | ||
1)Class X contains objects of class Y. | |||
2)Class X aggregates objects of class Y. | |||
3)Class X records instances of class Y. | |||
4)Class X owns the initialization data which helps in creating objects of class X. | |||
5)Class X makes good use of objects of class Y. | |||
Following examples have been illustrated to show the implementation of the Creator pattern. These examples have been documented from various resources available on the web. If you will notice very limited resources are available on the web about creator pattern and those that are there,document the same example. This page however tries to document some of those unique examples. | |||
===Example 1=== | |||
The class diagram shown below of creator pattern has been taken from the site [http://translate.google.com/translate?hl=en&sl=it&u=http://www.ugolandini.net/CreatorPattern.html&sa=X&oi=translate&resnum=8&ct=result&prev=/search%3Fq%3Dcreator%2Bpattern%26start%3D60%26hl%3Den%26lr%3D%26sa%3DN%26as_qdr%3Dall, www.ugolandini.net ]. This example actually has been taken from the book [http://books.google.com/books?id=r8i-4En_aa4C&pg=PA228&lpg=PA228&dq=what+is+creator+pattern&source=web&ots=mT9HL_BgM-&sig=y9ZyqedS8KioGOfp8mYghZQljiE#PPA227,M1 Applying UML and Patterns ] by Craig Larman. Most of the examples on the web document this same example as is illustrated later on. | |||
[[Image:Creator11.jpg]] | |||
The above diagram illustrates a class Sale which contains attributes date and item and an item SaleLineItem which represents an item which is sold and it contains an attribute quantity. Since the Sale class contains a number of objects of type class SaleLineItem, considering rule 1 given above , class Sale should be assigned the responsibility of creating objects of class SaleLineItem. To implement such a creation a method might be added in Sale class called makeLineItem() which invokes the constructor of SaleLineItem class. | |||
Since this example has been used in most resources on the web and it is actually used originally in the book Applying UML and Patterns, it would be safe to say that this could be expressed as the best example to be used to explain to a class. | |||
===Example 2=== | |||
The following code snippet has been taken from a lecture slide at [http://www.cs.purdue.edu/homes/jv/510s05/patterns.pdf www.cs.purdue.edu] The Maze class consists of rooms and hence it is given the responsibility of creating instances of Class Room. | |||
Maze create() { | |||
Maze maze = new Maze(); | |||
Room r1 = new Room(1); | |||
Room r2 = new Room(2); | |||
Door door = new Door(r1, r2); | |||
maze.addRoom(r1); | |||
maze.addRoom(r2); | |||
r1.setSide(North, new Wall()); | |||
r1.setSide(East, door); | |||
r1.setSide(South, new Wall()); | |||
r1.setSide(West, new Wall()); | |||
r2.setSide(North, new Wall()); | |||
r2.setSide(East, new Wall()); | |||
r2.setSide(South, new Wall()); | |||
r2.setSide(West, door); | |||
return maze; | |||
} | |||
===Example 3=== | |||
This example has been taken from the reource by [http://davidhayden.com/blog/dave/archive/2004/12/06/667.aspx DavidHayden] . He illustrates the creation of new Rows to a DataBase Table. To obtain a new Row object, the DataTable class is asigned the responsibility of creating new rows to the table. This is a result of applying Rule 1 as given above. Since the DataTable class contains many DataRow objects, it should be the class which creates objects of type DataRow. | |||
The following code snippet elaborates on this. | |||
DataRow myRow = myTable.NewRow(); | |||
... | |||
Rows.Add(myRow); | |||
===Other examples on the web === | |||
A few more resources, which document about the creator pattern are as follows: | |||
1) [http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design)#Factory, wikipedia] This page introduces the usage of GRASP patterns. The creator pattern is explained in some detail but the factory pattern could be illustrated by an example. | |||
2) The lecture notes at [http://staff.cs.utu.fi/kurssit/Programming-III/GRASP(9).pdf staff.cs.utu ] illustrates on the example given in Craig larman's book. It provides in depth exposure on the subject and illustrates how the low coupling and high cohesion pattern influences the usage of Creator pattern. This page is a also a good page to start learning about GRASP. | |||
3) The course notes in [http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class4/Creator.html web.cs.wpi.edu ] also gives an explanation of creator pattern by illustrating the same example of the Sale class given in Example 1. | |||
4) The following page by [http://people.msoe.edu/~blessing/cs489/cs489-13ch18.pdf Jeffrey Blessing ] is a good page which explains the idea of GRASP patterns. It illustrates all the patterns with simple examples related to the Sale class example. | |||
5) The lecture slides on [http://www.soc.napier.ac.uk/module.php3?op=getresource&cloaking=no&resourceid=5938601#267,20,Creator, www.soc.napier.ac.uk] illustrate the idea of GRASP patterns and further illustrates how creator pattern promotes low coupling . | |||
6)Lecture slides at | |||
[http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/GRASP.pdf iastate.edu], also illustrate on the GPASP patterns and provide examples for Creator pattern. | |||
7) The following page at [http://www.informit.com/content/images/0130925691/samplechapter/0130925691.pdf www.informit.com] provides illustrative examples of GRASP pattern using the same example but this page is more educative and would be a good resource to learn about GRASP in general. | |||
8) Lecture slides at [http://cs.uwindsor.ca/~kobti/322/lectures/Lecture17.pdf cs.uwindsor.ca] again provides an overview of GRASP but it gives a very general outline , more illustrative examples could have been provided. | |||
9) Lecture slides by [http://www.cse.mrt.ac.lk/lecnotes/cs201/Old_0304Batches/2004-2005/yohan/Lecture10-4.ppt Yohan Welikala] provides another illustrative resource of GRASP patterns. It provides a different example to elaborate the Creator pattern . The example used is of that of a Digital Library which contains a LendingDatabase class which contains LendingRecords and hence creates instances of LendingRecords class. | |||
10) Lecture slides at [http://www.dcs.bbk.ac.uk/~niki/lecture5.ppt#594, www.dcs.bbk.ac.uk] is another resource which describes about GRASP. This page has some further discussions about Creator pattern and how it relates to Low Coupling. | |||
==Creator vs Factory Pattern== | |||
As explained in [http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design)#Factory, wikipedia], both the creator and factory patterns are members of the family of GRASP Patterns . While the creator pattern is associated with assigning which class is responsible for creating objects of other classes, the factory pattern actually determines through some inbuilt logic to determine,which class should be made responsible for creating new objects. | |||
The difference is made more clear with the resource in [http://www.patterndepot.com/put/8/factory.pdf patterndepot.com] which gives good examples of factory pattern. An example given in this page illustrates the use of factory pattern which returns an instance of either firstname followed by lastname class or an instance of lastname followed by firstname class depending on the logic executed by the factory pattern class. If you try to use the creator pattern in this example given in the resource, it wont be of much use since it is not known until the logic is actually executed as to which instance of class is actually needed to be created. To summarize the factory pattern is used when | |||
1) A class cannot easily determine which kind of class objects it should create. (This logic is handled by the factory class which returns the appropriate instance of a particular class.) | |||
2) A class uses its subclasses to specify which object is should create. | |||
3) Parameters can be passed to the factory class which would help it determine which class instance to return. | |||
==External Links== | |||
1. http://blogs.msdn.com/sanjeets/archive/2007/09/02/creational-factory-pattern.aspx | 1. http://blogs.msdn.com/sanjeets/archive/2007/09/02/creational-factory-pattern.aspx | ||
Line 28: | Line 119: | ||
9. http://translate.google.com/translate?hl=en&sl=it&u=http://wiki.ugidotnet.org/default.aspx/UGIdotNETWiki/PatternCreator.html&sa=X&oi=translate&resnum=1&ct=result&prev=/search%3Fq%3Dcreator%2Bpattern%26start%3D100%26hl%3Den%26lr%3D%26sa%3DN%26as_qdr%3Dall | 9. http://translate.google.com/translate?hl=en&sl=it&u=http://wiki.ugidotnet.org/default.aspx/UGIdotNETWiki/PatternCreator.html&sa=X&oi=translate&resnum=1&ct=result&prev=/search%3Fq%3Dcreator%2Bpattern%26start%3D100%26hl%3Den%26lr%3D%26sa%3DN%26as_qdr%3Dall | ||
10. http:// | 10. http://faculty.inverhills.edu/dlevitt/CS%202000%20(FP)/GRASP%20Patterns.pdf | ||
11. http://www.ondotnet.com/pub/a/dotnet/2003/08/11/factorypattern.html | |||
12. http://www.mindspring.com/~mgrand/pattern_synopses2.htm | |||
13. http://www.oodesign.com/oo_design_patterns/creational_patterns/factory_method.html | |||
14. http://www.codeproject.com/useritems/BuilderPattern.asp | |||
15. http://www.cs.purdue.edu/homes/jv/510s05/patterns.pdf | |||
16. http://www-plan.cs.colorado.edu/danielvd/paper/gpce03.pdf | |||
17. http://www.patterndepot.com/put/8/factory.pdf |
Latest revision as of 23:49, 28 November 2007
Problem Statement
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
Introduction
The creator design pattern is a part of the family of design patterns collectively referred to as (GRASP) General Responsibility Assignment Software Patterns. The various patterns or principles associated with GRASP are creator, controller, low coupling, high cohesion, polymorphism and Factory pattern. These patterns are critical to any software development project. We specifically look into the creator pattern of GRASP and compare it with the factory pattern.
The Creator Pattern
The creator pattern under GRASP is mainly associated with delegating the creation of an object of a certain class to another responsible class. Object creations is one of the most important aspects of object oriented design and hence a good design should be associated with proper delegation of object creations to appropriate classes so that it results in maximum reuse and minimum coupling. To figure out which class should be delegated the responsibility of creating objects of another class, the following set of rules should be followed.
A certain class X should be associated with creating objects of class Y if any of the following is true
1)Class X contains objects of class Y.
2)Class X aggregates objects of class Y.
3)Class X records instances of class Y.
4)Class X owns the initialization data which helps in creating objects of class X.
5)Class X makes good use of objects of class Y.
Following examples have been illustrated to show the implementation of the Creator pattern. These examples have been documented from various resources available on the web. If you will notice very limited resources are available on the web about creator pattern and those that are there,document the same example. This page however tries to document some of those unique examples.
Example 1
The class diagram shown below of creator pattern has been taken from the site www.ugolandini.net . This example actually has been taken from the book Applying UML and Patterns by Craig Larman. Most of the examples on the web document this same example as is illustrated later on.
The above diagram illustrates a class Sale which contains attributes date and item and an item SaleLineItem which represents an item which is sold and it contains an attribute quantity. Since the Sale class contains a number of objects of type class SaleLineItem, considering rule 1 given above , class Sale should be assigned the responsibility of creating objects of class SaleLineItem. To implement such a creation a method might be added in Sale class called makeLineItem() which invokes the constructor of SaleLineItem class.
Since this example has been used in most resources on the web and it is actually used originally in the book Applying UML and Patterns, it would be safe to say that this could be expressed as the best example to be used to explain to a class.
Example 2
The following code snippet has been taken from a lecture slide at www.cs.purdue.edu The Maze class consists of rooms and hence it is given the responsibility of creating instances of Class Room.
Maze create() { Maze maze = new Maze(); Room r1 = new Room(1); Room r2 = new Room(2); Door door = new Door(r1, r2); maze.addRoom(r1); maze.addRoom(r2); r1.setSide(North, new Wall()); r1.setSide(East, door); r1.setSide(South, new Wall()); r1.setSide(West, new Wall()); r2.setSide(North, new Wall()); r2.setSide(East, new Wall()); r2.setSide(South, new Wall()); r2.setSide(West, door); return maze; }
Example 3
This example has been taken from the reource by DavidHayden . He illustrates the creation of new Rows to a DataBase Table. To obtain a new Row object, the DataTable class is asigned the responsibility of creating new rows to the table. This is a result of applying Rule 1 as given above. Since the DataTable class contains many DataRow objects, it should be the class which creates objects of type DataRow. The following code snippet elaborates on this.
DataRow myRow = myTable.NewRow(); ... Rows.Add(myRow);
Other examples on the web
A few more resources, which document about the creator pattern are as follows:
1) wikipedia This page introduces the usage of GRASP patterns. The creator pattern is explained in some detail but the factory pattern could be illustrated by an example.
2) The lecture notes at staff.cs.utu illustrates on the example given in Craig larman's book. It provides in depth exposure on the subject and illustrates how the low coupling and high cohesion pattern influences the usage of Creator pattern. This page is a also a good page to start learning about GRASP.
3) The course notes in web.cs.wpi.edu also gives an explanation of creator pattern by illustrating the same example of the Sale class given in Example 1.
4) The following page by Jeffrey Blessing is a good page which explains the idea of GRASP patterns. It illustrates all the patterns with simple examples related to the Sale class example.
5) The lecture slides on www.soc.napier.ac.uk illustrate the idea of GRASP patterns and further illustrates how creator pattern promotes low coupling .
6)Lecture slides at iastate.edu, also illustrate on the GPASP patterns and provide examples for Creator pattern.
7) The following page at www.informit.com provides illustrative examples of GRASP pattern using the same example but this page is more educative and would be a good resource to learn about GRASP in general.
8) Lecture slides at cs.uwindsor.ca again provides an overview of GRASP but it gives a very general outline , more illustrative examples could have been provided.
9) Lecture slides by Yohan Welikala provides another illustrative resource of GRASP patterns. It provides a different example to elaborate the Creator pattern . The example used is of that of a Digital Library which contains a LendingDatabase class which contains LendingRecords and hence creates instances of LendingRecords class.
10) Lecture slides at www.dcs.bbk.ac.uk is another resource which describes about GRASP. This page has some further discussions about Creator pattern and how it relates to Low Coupling.
Creator vs Factory Pattern
As explained in wikipedia, both the creator and factory patterns are members of the family of GRASP Patterns . While the creator pattern is associated with assigning which class is responsible for creating objects of other classes, the factory pattern actually determines through some inbuilt logic to determine,which class should be made responsible for creating new objects. The difference is made more clear with the resource in patterndepot.com which gives good examples of factory pattern. An example given in this page illustrates the use of factory pattern which returns an instance of either firstname followed by lastname class or an instance of lastname followed by firstname class depending on the logic executed by the factory pattern class. If you try to use the creator pattern in this example given in the resource, it wont be of much use since it is not known until the logic is actually executed as to which instance of class is actually needed to be created. To summarize the factory pattern is used when
1) A class cannot easily determine which kind of class objects it should create. (This logic is handled by the factory class which returns the appropriate instance of a particular class.)
2) A class uses its subclasses to specify which object is should create.
3) Parameters can be passed to the factory class which would help it determine which class instance to return.
External Links
1. http://blogs.msdn.com/sanjeets/archive/2007/09/02/creational-factory-pattern.aspx
2. http://davidhayden.com/blog/dave/archive/2004/12/06/667.aspx
3. http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design)#Factory
4. http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class4/Creator.html
5. http://gsraj.tripod.com/design/creational/factory/factory.html
7. http://www.apwebco.com/gofpatterns/creational/FactoryMethod.html
10. http://faculty.inverhills.edu/dlevitt/CS%202000%20(FP)/GRASP%20Patterns.pdf
11. http://www.ondotnet.com/pub/a/dotnet/2003/08/11/factorypattern.html
12. http://www.mindspring.com/~mgrand/pattern_synopses2.htm
13. http://www.oodesign.com/oo_design_patterns/creational_patterns/factory_method.html
14. http://www.codeproject.com/useritems/BuilderPattern.asp
15. http://www.cs.purdue.edu/homes/jv/510s05/patterns.pdf
16. http://www-plan.cs.colorado.edu/danielvd/paper/gpce03.pdf