Interface
package com.liguoliang.soap.jaxws; import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; @WebService @SOAPBinding(style = Style.RPC) public interface IHelloWorld { @WebMethod String getHelloWorldAsString(String name); }
Implement:
package com.liguoliang.soap.jaxws; import javax.jws.WebService; @WebService(endpointInterface = "com.liguoliang.soap.jaxws.IHelloWorld") public class HelloWorldImpl implements IHelloWorld { @Override public String getHelloWorldAsString(String name) { return "Hello: " + name; } }
package com.liguoliang.soap.jaxws; import javax.xml.ws.Endpoint; public class HelloWorldPublisher { public static void main(String[] args) { Endpoint.publish("http://localhost:8080/ws/hello", new HelloWorldImpl()); } }
package com.liguoliang.soap.client; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import com.liguoliang.soap.jaxws.IHelloWorld; public class HelloWorldClient { public static void main(String[] args) throws MalformedURLException { URL url = new URL("http://localhost:8080/ws/hello?wsdl"); QName qName = new QName("http://jaxws.soap.liguoliang.com/", "HelloWorldImplService"); Service service = Service.create(url, qName); // Get the instance. IHelloWorld iheHelloWorld = service.getPort(IHelloWorld.class); System.out.println(iheHelloWorld.getHelloWorldAsString("liguoliang.com")); } }
The outupt:
Hello: liguoliang.com
Adapter pattern(Wrapper pattern), is a design pattern that allows classes to work together, those classes can not work normally because they have incompatible interface.
For example, you travel from US to China, you need to use an adapter so that you charger can work, because they have different standard.
“The adapter is created by implementing or inheriting both the interface that is expected and the interface that is pre-existing. It is typical for the expected interface to be created as a pure interface class, especially in languages such as Java that do not support multiple inheritance”
(http://en.wikipedia.org/wiki/Adapter_pattern)
Adapter contains an instance of the class it wraps, so that the adapter can delegate the request to warped instance.
This class implements a vector of bits that grows as needed. Each component of the bit set has a boolean value. The bits of a BitSet are indexed by nonnegative integers. Individual indexed bits can be examined, set, or cleared. One BitSet may be used to modify the contents of another BitSet through logical AND, logical inclusive OR, and logical exclusive OR operations.
By default, all bits in the set initially have the value false, for example:
BitSet bs = new BitSet(9); // Create a new bit set,the initial size is 9; bs.set(3); // set the value of index 3 to true; // bs.clear(3); // clear - set to false; bs.set(10); // the number of bits currently in this bit set, the last true index + 1; System.out.println(bs.size()); // the bit length. System.out.println(bs.length()); // the true index. System.out.println(bs.toString()); // set to the complement of its current. bs.flip(3); System.out.println(bs.toString()); // create a new bitset. BitSet bs3 = new BitSet(); bs3.set(10); System.out.println(bs.equals(bs3)); // true bs.clear(); // clear all bs.xor(bs3); // XOR System.out.println(bs); }
Enum is a new feature shipped in Java 5, An enum type is a type whose fields consist of a fixed set of constants.
/** * Specify the currency type. * @author Guoliang */ public static enum Currency{ /** NICKLE */ N("NICKLE", 0.05), /** DIME */ D("DIME", 0.10), /** QUARTER */ Q("QUARTER", 0.25), /** DOLLAR */ DOLLAR("DOLLAR", 1.00); /** Abbr of the money type. */ private String dispalyName; /** the value of the money. */ private double value; // Constructor private Currency(String abbr, double value) { this.dispalyName = abbr; this.value = value; } public String getAbbr() { return dispalyName; } public double getValue() { return value; } public static void main(String[] args) { for (Currency currency : Currency.values()) { System.out.println(currency.getAbbr()); } } }
Java.lang.Enum is an abstract Class, so Enum can not extends other classes, btw, should not create enum instance, so the constructor need to be private: “only private is permitted.”
Ref: http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
Some times we need write some Singleton class for some reasons, like we need a singleton to store application settings. btw, we need this singleton thread safe.
Here is a good example for open JDK:
/** * Every Java application has a single instance of class *Runtime
that allows the application to interface with * the environment in which the application is running. The current * runtime can be obtained from thegetRuntime
method. ** An application cannot create its own instance of this class. * * @author unascribed * @see java.lang.Runtime#getRuntime() * @since JDK1.0 */ public class Runtime { private static Runtime currentRuntime = new Runtime(); /** * Returns the runtime object associated with the current Java application. * Most of the methods of class
Runtime
are instance * methods and must be invoked with respect to the current runtime object. * * @return theRuntime
object associated with the current * Java application. */ public static Runtime getRuntime() { return currentRuntime; } /** Don't let anyone else instantiate this class */ private Runtime() {}
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/lang/Runtime.java
The private static runtime instance will initialized when the class loaded by JVM, so thread safe is guaranteed by JVM class load mechanism.
// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.