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. – 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. 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 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. Contents of cart Page Counts Design Classes Sequence Diagram Sequence Diagram (cont) using System; using System.Collections; PageCounter Class 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; Static declaration // the page counts are kept in the int array and their corresponding // page names are kept in the arraylist private PageCounter() { Arrays for counts ct = new int[4]; & pages 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) { Store in static attribute pc = new PageCounter(); } return pc; } PageCounter Class Add page to arraylist and use its index to increase count int array // 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() { Fill a hashtable for Hashtable ht = new Hashtable(); return with page as foreach (Object o in al) key and count as { ht.Add(o, ct[al.IndexOf(o)]); value } return ht; } } } using System; using System.Collections; CartManager Class 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(); } Static declaration // 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) { Create instance if it if (s["Cart"] == null) doesn’t exist { s["Cart"] = new CartController(); } Store in session so one return (CartController) s["Cart"]; per user } 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); } Add cart contents Get cart contents } } // this property allows access to the arraylist that contains our cart entries public ArrayList Al { get { return al;} } Class, Session, and Instance Variables/Methods Static Space (Class) static PageCounter pc One per application static CartController getController() Session Space (per User) Static attribute static PageCounter getPageCounter() Session["Cart"] Static methods Session attribute One per user Object Space (Instance) One per object ArrayList al int[] ct void countPage() void add() instance attributes instance methods 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 Object Life During Processing Home Page (Home.aspx.cs) Home Page (Home.aspx) PageCounter Alive for the application CartManager Alive for the entire user session Product Page One Product Page Two (ProductOne.aspx.cs) (ProductTwo.aspx.cs) Product Page (ProductOne.aspx) Product Page (ProductTwo.aspx) Only alive as page is executed during the request & response Only alive in browser on client