1 / 15

Using Patterns Examples

Using Patterns Examples. Dr. Neal CIS 480. Singleton Pattern Concept. Singleton Pattern: For those situations when you only one object to be created from a particular class. This means that only one instantiation of the class is allowed.

dusan
Download Presentation

Using Patterns Examples

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Using Patterns Examples Dr. Neal CIS 480

  2. Singleton Pattern Concept • Singleton Pattern: • For those situations when you only one object to be created from a particular class. • This means that only one instantiation of the class is allowed. • The first reference to the class will create the instance and all future references will return this same instance. • Therefore the class must control that only one instance of itself is created. • Our Example: • This example is a web site with a home page and two product pages that the user may shop for furniture and lamps. • The user may add items to their shopping cart on each of the product pages. • In addition to keeping tract of a users purchases we would also like to keep a count of how many times each page is visited on the site. • We will create to classes a CartManager (keep tract of purchases) and PageCounter (keep tract of page counts) • The home page will also allow display of the cart contents and the current status of page counts.

  3. PageCounter Class • Our Page Manager: • Only a single instance of PageCounter must exist for the entire application. • The first call to PageCounter must create an instance of the class and all other calls will reference it. • This means that all users will see the same PageCounter object. • It must keep tract of the “get” request for each page in our application and increment the page counts. • It must provide methods for incrementing the count per page and retrieving all page counts

  4. CartManager Class • Our Shopping Cart: • We what only one instance of a shopping CartManager per user or session that keeps track of all user purchases. • This means that there will multiple carts if more than one user is accessing the application. • This one instance of the CartManager for a user must live across multiple pages while the user is shopping. • The CartManager should be accessible from anywhere so new products can be added from any page. • The CartManager needs to provide methods for us add contents as well as to access the contents.

  5. Contents of cart Page Counts

  6. DesignClasses

  7. SequenceDiagram

  8. SequenceDiagram(cont)

  9. using System; using System.Collections; namespace Patterns { /// <summary> /// Dr. Neal /// CIS 480 /// Example of singleton pattern used for a Page Counter and a modified /// singleton pattern for a Cart Controller /// </summary> public class PageCounter { private static PageCounter pc; private int[] ct; private ArrayList al; // the page counts are kept in the int array and their corresponding // page names are kept in the arraylist private PageCounter() { ct = new int[4]; al = new ArrayList(); } // create the singleton object on the first call and return its // instance for each subsequent call, this creates one pagecounter // for the entire application across all user sessions public static PageCounter getPageCounter() { if (pc == null) { pc = new PageCounter(); } return pc; } PageCounter Class Arrays for counts & pages Static declaration Store in static attribute

  10. PageCounter Class // the countPage method insures that the page has been visited before // if not is added to the arraylist, then the position in the arraylist // is used to index the int counter array public void countPage(string page) { if (! al.Contains(page)) { al.Add(page); } ct[al.IndexOf(page)] += 1; } // to retrieve the values of page counts a hashtable is built with // the page as a key and the counts as the value objects public Hashtable getCounts() { Hashtable ht = new Hashtable(); foreach (Object o in al) { ht.Add(o, ct[al.IndexOf(o)]); } return ht; } } } Add page to arraylist and use its index to increase count int array Fill a hashtable for return with page as key and count as value

  11. using System; using System.Collections; namespace Patterns { /// <summary> /// Dr. Neal /// CIS 480 /// Example of singleton pattern used for a Page Counter and a modified /// singleton pattern for a Cart Controller /// </summary> public class CartController { private ArrayList al; //Array List is used to store cart items private CartController() { al = new ArrayList(); } // this static method is called on the class to get the instance of // the cartcontroller that is associated with the current session // if the instance doesn't exist then it is created and stored in the // current session, this creates one cartcontroller per session public static CartController getController(System.Web.SessionState.HttpSessionState s) { if (s["Cart"] == null) { s["Cart"] = new CartController(); } return (CartController) s["Cart"]; } CartManager Class Static declaration Create instance if it doesn’t exist Store in session so one per user

  12. CartManager Class // the add method just adds whatever object it is handed to the arraylist // which are the contents of the shopping cart public void add(Object o) { al.Add(o); } // this property allows access to the arraylist that contains our cart entries public ArrayList Al { get { return al;} } } } Add cart contents Get cart contents

  13. Class, Session, and Instance Variables/Methods Static attribute Static Space (Class) One per application static PageCounter pc static PageCounter getPageCounter() Static methods static CartController getController() Session attribute Session Space (per User) One per user Session["Cart"] instance attributes ArrayList al int[] ct Object Space (Instance) One per object instance methods void countPage() void add()

  14. Method Calls Static Method Call CartController.getController() Static call referencing the class name which is loaded when the application starts Instance Method Call cc.add(); Call referencing an instance of the object of type CartController

  15. Object Life During Processing Alive for the application PageCounter Alive for the entire user session CartManager Only alive as page is executed during the request & response Home Page (Home.aspx.cs) Product Page One (ProductOne.aspx.cs) Product Page Two (ProductTwo.aspx.cs) Home Page (Home.aspx) Product Page (ProductTwo.aspx) Only alive in browser on client Product Page (ProductOne.aspx)

More Related