1 / 24

Design Pattern

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.

taipa
Download Presentation

Design Pattern

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. Design Pattern Team 5: The Infinite Loops Gloria Berumen Patricia Martinez Jose Roberto Salcido Michelle Soto Jose Luis Yanez Omar Zorrilla

  2. Design Pattern Prototype

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

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

  5. Example

  6. Example: Class Diagram

  7. 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; } }

  8. 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); } }

  9. 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()); } }

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

  11. Design Pattern Command

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

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

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

  15. Command Pattern Class Diagram

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

  17. References • http://www.blackwasp.co.uk/Command.aspx

  18. Design Pattern Proxy

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

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

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

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

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

  24. References • Source Making: Teaching IT Professionals http://sourcemaking.com/design_patterns/proxy

More Related