http://blog.csdn.net/haoxingfeng/article/details/9191619
1> Proxy Mode
Http://www.cnblogs.com/chinajava/p/5880870.html
Proxy mode static agent and dynamic agent
Static Proxy: (Requires an interface, a delegate class implementation interface, a proxy class implementation interface,) proxy mode is to provide a proxy object, which can implement some functions of the delegate class, but also can expand their own functions
For example (consignment Class) ticket office can be ticket sales, change, refund; (agent type) cattle tickets can only be sold tickets, to deal with the change or refund business must go to the ticket office
//Interface Packagecom.wyx.proxyTest; Public InterfaceIsubject { Public voidrequest ();}//Delegate Class Packagecom.wyx.proxyTest; Public classSubjectimplImplementsIsubject {@Override Public voidrequest () {System.out.println ("This is real subject"); } Public voidOtherfun () {}}//proxy class Packagecom.wyx.proxyTest;/** This is a proxy class, it only has some features in Subjectimpl*/ Public classSubjectproxyImplementsisubject{PrivateIsubject Sub =NULL; PublicSubjectproxy (Isubject sub) { This. Sub =Sub; } @Override Public voidrequest () {System.out.println ("This is Proxy"); Sub.request (); }} //proxy class Testing Packagecom.wyx.proxyTest; Public classMaintest { Public Static voidMain (string[] args) {Subjectimpl sub=NewSubjectimpl (); Subjectproxy Proxy=NewSubjectproxy (sub); Proxy.request (); }}
Dynamic Agent:
2> decoration mode
3> Strategy Mode (this pattern is the abstraction of behavior, Chiang's other classes have similar methods, extracted by common abstract methods, easier to expand)
4> Singleton mode (so that there is only one such object in memory, a hungry man type, full-han style)
public class singlemain{
Main method
public static void Main (string[] args) {
}
}
A Hungry man type
Class singletondome1{
Private objects
private static final SingletonDome1 sgd1 = new SingletonDome1 ();
Private method of construction
Private SingletonDome1 () {}
//
public static SingletonDome1 GetSgd1 () {
return SGD1;
}
}
Full-Chinese style
Class singletondome2{
Private objects
private static SingletonDome2 sgd2 = new SingletonDme2 ();
Private method of construction
Private SingletonDome2 () {}
//
public static SingletonDome2 GetSgd2 () {
if (SGD2 = = null) {
SGD2 = new SingletonDome2 ();
}
return SGD2;
}
}
5> Factory mode (is the method pattern for instantiating objects, i.e. a pattern of replacing the new operation with a factory method, for example: Here is a simple example)
/*
* Static Factory mode
*/
Interface
Public interface Alphabet {
Alphabetfun (Object ... params);}
Implementation of the interface Class A
public class A implements alphabet{
@Override
Alphabetfun (Object ... params) {
System.out.println (params[0]+ "" + "This is A");
}
}
Implementation of the interface Class B
public class B implements alphabet{
@Override
Alphabetfun (Object ... params) {
System.out.println (params[0]+ "" + "This is A");
}
}
//Factory class
public class factorycls{
public staticAlphabet Factoryfun (String ABC) {
Alphabet tempobj;
Using the mechanism of Java reflection
Class CLS = null;
try{
CLS = Class.forName ("Classpath");
}catch (Expection e) {
E.printstacktrace ();
}
/*
if (abc== "A") {
tempobj = new A ();
}else if (abc== "B") {
Tempobj = new B ();
}
*/
return tempobj;
}
}
Common design Patterns and Java code descriptions