CSC/ECE 517 Fall 2007/wiki3 5 ab: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
Line 60: Line 60:


This example has been documented from this resource on the web.
This example has been documented from this resource on the web.
===Other examples on the web ===


==The Factory Pattern ==
==The Factory Pattern ==

Revision as of 21:52, 18 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 colectively 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 paatern 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 oject oriented design and hence having a good design associated with proper delegation of object creations would result in maximum reuse and minimum coupling. To figure out which class should be delegated the resposibility of creating objects, the folowing 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 above diagram illustrates a class Sale which contains attributes data and item and an item SaleLineItem which represents a sold item. It contains an attribute quantity. Since the Sale class contains a number of objects of type class SaleLineItem, considering rule 1 , class Sale should be assigned the resposibility of creating objects of type SaleLineItem. To implement such a creation a method might be added in Sale class called makeLineItem() which invokes the constructor of SaleLineItem class. This example has been taken from the resource in []

Example 2

Consider the following piece of code . The Maze class consists of rooms and hence it is given the responsibility of creating instances of Class Room. This example has been documented from this resource [] on the web.

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

Another example can be illustrated in creation of new DataRows to a DataTable. To obtain a new row object, the DataTable class is asigned the responsibility of creating new rows to the table. The following code illustrates this.

DataRow myRow = myTable.NewRow();
...
Rows.Add(myRow);

This example has been documented from this resource on the web.

Other examples on the web

The Factory Pattern

Creator vs Factory Pattern

Information on the Web

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

6. 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#PPP1,M1

7. http://www.apwebco.com/gofpatterns/creational/FactoryMethod.html

8. 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

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://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

11. http://faculty.inverhills.edu/dlevitt/CS%202000%20(FP)/GRASP%20Patterns.pdf

12. http://www.ondotnet.com/pub/a/dotnet/2003/08/11/factorypattern.html

13. http://www.mindspring.com/~mgrand/pattern_synopses2.htm

14. http://www.oodesign.com/oo_design_patterns/creational_patterns/factory_method.html

15. http://www.codeproject.com/useritems/BuilderPattern.asp

16. http://www.cs.purdue.edu/homes/jv/510s05/patterns.pdf

17. http://www-plan.cs.colorado.edu/danielvd/paper/gpce03.pdf

18. http://www.patterndepot.com/put/8/factory.pdf