<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Chan Chen Coding...

    Three: Prototype Design

    Prototype Definition

    Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.

    Class Diagram

    Builder

    Participants

    • Prototype
      • declares an interface for cloning itself.
    • ConcretePrototype.
      • implements an operation for cloning itself.
    • Client.
      • creates a new object by asking a prototype to clone itself.

    Example: Product Cache

    When object instantiation is a lot more expensive than cloning, Prototype pattern may offer a useful optimization technique.

    Example assumptions:

    • An e-commerce application gathers product information trough complex queries against a legacy database.
    • The legacy database is updated at predefined intervals which are known.
    • The number of products allows caching with a reasonable memory consumption.

    When a user asks for information for a certain product the application could gather that information in two ways:

    1. execute the complex query against legacy database, gather the information, and instantiate the object.
    2. instantiate the objects at predefined intervals and keep them in a cache, when an object is requested, it is retrieved from cache and cloned. When the legacy database is updated, discard the content of the cache and re-load with new objects.

    The second approach is based on Prototype pattern and it is illustrated below:

    Example: Class Diagram

    Prototype Example

    Example: Java sample code

        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 int numberOfPages;

            
    public int getNumberOfPages() {
                
    return numberOfPages;
            }
            
    public void setNumberOfPages(int i) {
                numberOfPages 
    = i;
            }
        }
        
    public class DVD extends Product {
            
    private int duration;

            
    public int getDuration() {
                
    return duration;
            }
            
    public void setDuration(int i) {
                duration 
    = i;
            }
        }
        
    import java.util.*;
        
    public class ProductCache {
            
    private static Hashtable productMap = new Hashtable();

            
    public static Product getProduct(String productCode) {
                Product cachedProduct 
    = (Product) productMap.get(productCode);
                
    return (Product) cachedProduct.clone();
            }

            
    public static void loadCache() {
                
    // for each product run expensive query and instantiate product
                
    // productMap.put(productKey, product);
                
    // for exemplification, we add only two products
                Book b1 = new Book();
                b1.setDescription(
    "Oliver Twist");
                b1.setSKU(
    "B1");
                b1.setNumberOfPages(
    100);
                productMap.put(b1.getSKU(), b1);
                DVD d1 
    = new DVD();
                d1.setDescription(
    "Superman");
                d1.setSKU(
    "D1");
                d1.setDuration(
    180);
                productMap.put(d1.getSKU(), d1);
            }
        }
        
    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());

                DVD clonedDVD 
    = (DVD) ProductCache.getProduct("D1");
                System.out.println(
    "SKU = " + clonedDVD.getSKU());
                System.out.println(
    "SKU = " + clonedDVD.getDescription());
                System.out.println(
    "SKU = " + clonedDVD.getDuration());
            }
        }

    Benefits

    • Adding and removing products at runtime.
    • Specifying new objects by varying values.
    • Specifying new objects by varying structure.
    • Reduced subclassing.
    • Configuring an application with classes dinamincally.

    Usage

    • When the classes to instantiate are specified at run time.
    • When you want to avoid building a class hierarchy of factories that parallels the class hierarchy of products.
    • When instances of a class can have one of only a few combinations of state.


    -----------------------------------------------------
    Silence, the way to avoid many problems;
    Smile, the way to solve many problems;

    posted on 2012-10-26 14:30 Chan Chen 閱讀(227) 評論(0)  編輯  收藏 所屬分類: Design Pattern

    主站蜘蛛池模板: a毛片免费播放全部完整| 国产精品亚洲片在线花蝴蝶| 中国在线观看免费的www| 四虎永久免费观看| 香蕉视频亚洲一级| xvideos亚洲永久网址| 美女被羞羞网站免费下载| 国产免费牲交视频| 美美女高清毛片视频黄的一免费| 国产自产拍精品视频免费看| 久久久久亚洲AV无码去区首| 俄罗斯极品美女毛片免费播放| 鲁啊鲁在线视频免费播放| 亚洲爽爽一区二区三区| 99久久婷婷免费国产综合精品| 国产亚洲av片在线观看播放 | 亚洲avav天堂av在线网毛片| 日本成人免费在线| 成年免费大片黄在线观看com| 国产亚洲日韩在线三区| 91成人在线免费视频| 亚洲国产成a人v在线观看| 国产伦一区二区三区免费| 中国一级毛片视频免费看| 亚洲AV日韩AV天堂久久 | 久久精品熟女亚洲av麻豆 | 日韩免费视频在线观看| 免费人成网站永久| 亚洲阿v天堂在线| 99久久99这里只有免费费精品| 亚洲中文字幕乱码AV波多JI| 亚洲AV无码不卡在线观看下载| 中文字幕视频在线免费观看| 亚洲六月丁香六月婷婷色伊人| 又色又污又黄无遮挡的免费视| 黄页免费在线观看| 亚洲AV无码专区亚洲AV桃| 亚洲一区二区三区AV无码| 波多野结衣免费在线观看| 成人a毛片视频免费看| 久久精品a亚洲国产v高清不卡|