Thursday, May 22, 2014

Multiton Pattern (Registry of Singletons) in Java


Simple Note

From reference:
The multiton pattern expands on the singleton concept to manage a map of named instances as key-value pairs.
Rather than having a single instance per application (e.g. the java.lang.Runtime object in the Java programming language) the multiton pattern instead ensures a single instance per key.

Program

https://github.com/benbai123/JSP_Servlet_Practice/tree/master/Practice/JAVA/DesignPattern/src/multiton

Run TestMain.java to test.

Put Vitamin into VitaminBox, there is only one VitaminBox for a kind of Vitamin.

Reference:

Multiton pattern

Wednesday, May 21, 2014

Smart Proxy Pattern in Java


Simple Note

From reference:
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.

Smart References: providing a sophisticated access to certain objects such as tracking the number of references to an object and denying access if a certain number is reached, as well as loading an object from database into memory on demand.

Program

https://github.com/benbai123/JSP_Servlet_Practice/tree/master/Practice/JAVA/DesignPattern/src/proxy/smartproxy

Run TestMain.java to test.

OperatorProxy will limit the operation performed by RealOperator, at most 3 operation at a time.

Reference:

Proxy Design Pattern

Proxy Pattern

Protection Proxy Pattern in Java


Simple Note

(From Reference)
Protective Proxy – The protective proxy acts as an authorisation layer to verify if the actual user has access to appropriate content. An example can be thought about the proxy server which provides restrictive internet access in office. Only the websites and contents which are valid will be allowed and the remaining ones will be blocked.

Program

https://github.com/benbai123/JSP_Servlet_Practice/tree/master/Practice/JAVA/DesignPattern/src/proxy/protectionproxy

Run TestMain.java to test.

ProxyMouseController will check role first then control mouse via RealMouseController

Reference:

Gang of Four – Proxy Design Pattern

Tuesday, May 20, 2014

Remote Proxy Pattern in Java


Simple Note

(From Reference)
Remote Proxy – A remote proxy can be thought about the stub in the RPC call. The remote proxy provides a local representation of the object which is present in the different address location. Another example can be providing interface for remote resources such as web service or REST resources.

Program

https://github.com/benbai123/JSP_Servlet_Practice/tree/master/Practice/JAVA/DesignPattern/src/proxy/remoteproxy

Run TestMain.java to test.

Access Google Geo Services via GeoServiceProxy

The sample is based on Java Practice: java.net practice, Use Google Geocode Web Service

Reference:

Gang of Four – Proxy Design Pattern

Sunday, May 18, 2014

Control Mouse Move in Java


Simple Note

Use java.awt.Robot class.

Code

package proxy.remoteproxy;

import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.Robot;

public class TestMain {
    public static void main (String args[]) throws AWTException, InterruptedException {
        // get X/Y position of mouse cursor
        int posX = (int)MouseInfo.getPointerInfo().getLocation().getX();
        int posY = (int)MouseInfo.getPointerInfo().getLocation().getY();

        Robot bot = new Robot();
        // move around a rect
        bot.mouseMove(posX-30, posY-30);
        Thread.sleep(1000);
        bot.mouseMove(posX+30, posY-30);
        Thread.sleep(1000);
        bot.mouseMove(posX+30, posY+30);
        Thread.sleep(1000);
        bot.mouseMove(posX-30, posY+30);
        Thread.sleep(1000);
        bot.mouseMove(posX-30, posY-30);
        Thread.sleep(1000);
        // back to start position
        bot.mouseMove(posX, posY);
    }
}


Reference

Class Robot

Get X/Y Position of Mouse Cursor in Java


Simple Note

Use java.awt.MouseInfo as below:

        MouseInfo.getPointerInfo().getLocation().getX();
        MouseInfo.getPointerInfo().getLocation().getY();

Reference

Get Mouse Position

Saturday, May 17, 2014

Virtual Proxy Pattern in Java


Simple Note

(From SO)
A Virtual Proxy creates expensive objects on demand

(From Wiki)
Lazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed.

Program

https://github.com/benbai123/JSP_Servlet_Practice/tree/master/Practice/JAVA/DesignPattern/src/proxy/virtualproxy

Run TestMain.java to test.

ProxyFactorial will create and cache the Factorial object on demand.

Reference:

Proxy pattern

Lazy initialization

Great answer at SO

Factorial