Wu Yuxiong - natural born flyweight JAVA EE enterprise application development Struts2Sping4Hibernate Integrated Development Study Notes: enterprise application development thinking and strategies _Proxy

/ ** 
 * the Description: 
 * the website: <a href=" http://www.crazyit.org "> crazy Java league </a> 
 * the Copyright (C), 2001-2016, Yeeku.H.Lee 
 * This Program IS protected by a Copyright Laws. 
 * a Program the Name: 
 * a a Date: 
 * @author   Yeeku.H.Lee [email protected] 
 * @version   1.0
  * / 

// use the BigImage simulate a big picture 
public  class BigImage the implements Image 
{ 
    public BigImage () 
    { 
        the try 
        { 
            // program 3s pause mode simulation overhead 
            Thread.sleep (3000 );
            System.out.println ( "Picture loaded successfully ..." ); 
        } 
        the catch (InterruptedException EX) 
        { 
            ex.printStackTrace (); 
        } 
    } 
    // achieve Image in the show () method 
    public  void show () 
    { 
        System.out .println ( "draw real big picture" ); 
    } 
}
/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee [email protected]
 * @version  1.0
 */
public class BigImageTest
{
    public static void main(String[] args)
    {
        long start = System.currentTimeMillis();
        
        Image image =program returns an Image object that just BigImage proxy object//new new ImageProxy ( null ); 
        System.out.println ( "system overhead time to get the Image object:" + 
             (System.currentTimeMillis () - Start));
         // Only when the actual call image agency show () methods, procedures It will really create the proxy object. 
        image.show (); 
    } 
}
/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee [email protected]
 * @version  1.0
 */
public interface Image
{
    void show();
}
/ ** 
 * the Description: 
 * the website: <a href=" http://www.crazyit.org "> crazy Java league </a> 
 * the Copyright (C), 2001-2016, Yeeku.H.Lee 
 * This Program IS protected by a Copyright Laws. 
 * a Program the Name: 
 * a a Date: 
 * @author   Yeeku.H.Lee [email protected] 
 * @version   1.0
  * / 
public  class ImageProxy the implements Image 
{ 
    // combinations of one example image, the proxy object as a 
    Private Image image;
     // used to initialize the proxy object abstract entity 
    public ImageProxy (Image image) 
    { 
        the this .image =Image; 
    } 
    / ** 
     * Rewrite Image interface show () method 
     * This method is used to control access to the proxy object, 
     * as needed and is responsible for creating and deleting the proxy object 
     * / 
    public  void show () 
    { 
        // only when creating the proxy object method when the show really need to call the image of the 
        iF (image == null ) 
        { 
            image = new new BigImage (); 
        } 
        image.show (); 
    } 
}

 

Guess you like

Origin www.cnblogs.com/tszr/p/12376447.html