Common design Patterns and Java code descriptions

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.