Java implements the mechanism of reflection of abstract factory pattern +java __java

Source: Internet
Author: User
Tags stub

the reflection mechanism of Java:

The Java reflection mechanism is in the state of running, for any class, all the properties and methods of the class can be known, and any one of its methods and properties can be invoked for any object; This dynamically acquired information, and the function of the method that dynamically invokes the object, is called the reflection mechanism of the Java language. The reflection mechanism enables you to dynamically access the properties and methods of the class and the fields.

The most commonly used reflection mechanism is: (of course, there are many other methods of reflect class are not enumerated)

<pre name= "code" class= "Java" >class.forname ("obj name"). newinstance ();                                                                                               

Abstract Factory mode:

Abstract Factory Role: This is the core of the factory method pattern, and it has nothing to do with the application. is the interface that the specific factory role must implement or the parent class that must be inherited. In Java, it is implemented by an abstract class or interface.
Specific factory role: it contains code related to specific business logic. An object that is called by the application to create the corresponding specific product. In Java it is implemented by a specific class.
Abstract product (method) role: It is the parent or implementation interface for a specific product. Abstract classes or interfaces are generally implemented in Java.

Abstract factories are often closely associated with the Java reflection mechanism. When you encounter a new specific factory role, if there is no Java reflection mechanism, we need to first create a specific factory class, the UI (client) to modify the relevant code, the specific factory instantiation code to join. The principle of opening and closing is seriously violated. however , the implementation of abstract factory pattern can be closed by the reflection mechanism of Java. In the open, the new specific factory class name written in the XML file, by reading the XML file can be read to the new specific factory class name, according to the class name automatically generate the corresponding class instantiation.

The specific code is as follows:

Abstract Factory Role:

Iifactory.java Public
interface Iffactory {public
      void Createob ();
}

Specific factory role:

Javafactory public
class Javafactory implements iffactory{

	@Override public
	void Createob () {
		// TODO auto-generated Method Stub
		System.out.println ("Create Java Object");
	}

Cppfactory public
class Cppfactory  implements iffactory{

	@Override public
	void Createob () {
		//TODO auto-generated method stub
		System.out.println ("Create C + + object");
	}

Csharpfactory public
class Csharpfactory implements iffactory{

	@Override public
	void Createob () {
		//TODO auto-generated method stub
		System.out.println ("Create a CSharp object");
	}
     
Testui (client)

public class Testui {
    private String objectstr;
    public void Setselect (String key) {
    	This.objectstr=key
    }
    public string Getselect (string key) {return
    	this.objectstr
    }
    Public Iffactory Createobj () {
    	iffactory IFFAC = null;
    	try{
    		//
    		iffac= (iffactory) class.forname (OBJECTSTR). newinstance ();
    	catch (Exception e) {
    		e.printstacktrace ();
    	}
    	return IFFAC;
    }
    public static void Main (string[] args) {
		testui test=new testui ();
		Iffactory Iffac=null;
		Test.setselect ("Cppfactory");
		Iffac=test.createobj ();
		Iffac.createob ();
	}
Run Result:

Therefore, if we have other specific factories to join, first create a specific factory class to produce specific objects, and then the specific class name into the XML file, through the XML file specific string class name, to achieve the specific factory instantiation.

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.