250 likes | 406 Views
Design Pattern . Team 5: The Infinite Loops Gloria Berumen Patricia Martinez Jose Roberto Salcido Michelle Soto Jose Luis Yanez Omar Zorrilla. Design Pattern. Prototype. What is Prototype?. Used to clone or copy the existing objects instead create new ones from scratch.
E N D
Design Pattern Team 5: The Infinite Loops Gloria Berumen Patricia Martinez Jose Roberto Salcido Michelle Soto Jose Luis Yanez Omar Zorrilla
Design Pattern Prototype
What is Prototype? • Used to clone or copy the existing objects instead create new ones from scratch. • Used when creating an instance of a class that is very time consuming. • Helps to speed up the instantiation of objects • When the classes to instantiate are specified at run time.
Advantages of Prototype • Avoid repetitive code. • Simplified object copy process. • Makes the program structure easier to understand and maintain. • Improve performance. • Reducing sub-classing. • Adding and removing products at runtime.
public abstract class Product implements Cloneable { private String SKU; private String description; public Object clone() { Object clone = null; try { clone = super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return clone; } public String getDescription() { return description; } public String getSKU() { return SKU; } public void setDescription(String string) { description = string; } public void setSKU(String string) { SKU = string; } }
public class Book extends Product { private intnumberOfPages; public intgetNumberOfPages() { return numberOfPages; } public void setNumberOfPages(inti) { numberOfPages = i; } } public class ProductCache { private static HashtableproductMap = new Hashtable(); public static Product getProduct(String productCode) { Product cachedProduct = (Product) productMap.get(productCode); return (Product) cachedProduct.clone(); } public static void loadCache() { b1.setSKU("B1"); b1.setNumberOfPages(100); productMap.put(b1.getSKU(), b1); } }
public class Application { public static void main(String[] args) { ProductCache.loadCache(); Book clonedBook = (Book) ProductCache.getProduct("B1"); System.out.println("SKU = " + clonedBook.getSKU()); System.out.println("SKU = " + clonedBook.getDescription()); System.out.println("SKU = " + clonedBook.getNumberOfPages()); } }
References • http://www.c-sharpcorner.com/UploadFile/PashuSX/PrototypeDesignPattern05042006112741AM/PrototypeDesignPattern.aspx • http://en.wikipedia.org/wiki/Prototype_pattern • http://sourcemaking.com/design_patterns/prototype • http://www.c-sharpcorner.com/uploadfile/rajeshvs/prototypepatternsincs11142005003823am/prototypepatternsincs.aspx • http://www.oodesign.com/prototype-pattern.html • http://wiki.asp.net/page.aspx/499/prototype-pattern/ • http://www.apwebco.com/gofpatterns/creational/Prototype.html
Design Pattern Command
What is the Command Pattern? • Design pattern used to express a request, including the call to be made and all of its required parameters, in a command object. • This command object can be executed immediately or can be held for later use.
Command Design Pattern • The command object does not contain the functionality that is to be executed, only the info required to perform an action. • This removes the direct link between the command definitions and the functionality, promoting loose coupling.
Command Design Pattern • Is useful during activities that require the execution of a series of commands. • The command objects can be held in a queue and processed sequentially. • Also, if each object is stored in a stack after it is executed, they can allow the implementation of a multi-level undo facility.
Example: Remote Controlling a Robot • Receiver would be the robot. • The receiver class would include methods that allowed the robot to move forward/backward. • To control the robot, the commands would be added to a queue in the invoker object. • When commands are executed, the invoker would process the queue and add the commands to the stack.
References • http://www.blackwasp.co.uk/Command.aspx
Design Pattern Proxy
Intent of the Proxy Pattern • Provide a surrogate or placeholder for another object to control access to it. • Use an extra level of indirection to support distributed, controlled, or intelligent access. • Add a wrapper and delegation to protect the real component from undue complexity.
Problem • You need to support resource-hungry objects, and you do not want to instantiate such objects unless and until they are actually requested by the client.
Four Situations Where Proxy Pattern is Applicable • A virtual proxy is a placeholder for “expensive to create” objects. The real object is only created when a client first requests/accesses the object. • A remote proxy provides a local representative for an object that resides in a different address space. This is what the “stub” code in RPC and CORBA provides.
Four Situations Where Proxy Pattern is Applicable • A protective proxy controls access to a sensitive master object. The “surrogate” object checks that the caller has the access permissions required prior to forwarding the request. • A smart proxy interposes additional actions when an object is accessed. Typical uses include: • Counting the number of references to the real object so that it can be freed automatically when there are no more references (aka smart pointer), • Loading a persistent object into memory when it’s first referenced, • Checking that the real object is locked before it is accessed to ensure that no other object can change it
Example of Proxy Design Pattern • The Proxy provides a surrogate or place holder to provide access to an object. A check or bank draft is a proxy for funds in an account. A check can be used in place of cash for making purchases and ultimately controls access to cash in the issuer’s account.
References • Source Making: Teaching IT Professionals http://sourcemaking.com/design_patterns/proxy