<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

    主站蜘蛛池模板: 亚洲自偷精品视频自拍| 国产成人麻豆亚洲综合无码精品| 91亚洲精品视频| 69视频免费在线观看| 91大神亚洲影视在线| 一级女人18毛片免费| 成人亚洲国产va天堂| 国产又粗又猛又爽又黄的免费视频| 亚洲国产精品无码第一区二区三区| 毛片在线免费视频| 鲁啊鲁在线视频免费播放| mm1313亚洲国产精品美女| 思思久久99热免费精品6| 在线观看国产区亚洲一区成人| 中国一级特黄高清免费的大片中国一级黄色片 | 亚洲AV无码一区二区三区在线| 91黑丝国产线观看免费| 亚洲国产精品无码久久久秋霞1 | 最新国产乱人伦偷精品免费网站| 国产V亚洲V天堂无码| 久久精品无码专区免费青青 | 理论秋霞在线看免费| 亚洲中文字幕久久精品无码喷水| 国产羞羞的视频在线观看免费| 亚洲黄色免费在线观看| 成年美女黄网站色大免费视频| 午夜亚洲乱码伦小说区69堂| 国产亚洲精品线观看动态图| 8x网站免费入口在线观看| 亚洲欧美日韩中文二区| 亚洲国产人成中文幕一级二级| 成全动漫视频在线观看免费高清版下载| 亚洲高清在线播放| 成人免费无码大片A毛片抽搐色欲| 羞羞网站免费观看| 久久青青成人亚洲精品| 在线观看人成视频免费| jizz日本免费| 亚洲男人天堂影院| 亚洲精品一级无码中文字幕| 在线观看的免费网站无遮挡|