Encapsulation API

    Java and other APIs can be encapsulated into models. This document describes the basic methods of encapsulating various APIs as models.

1.Define API usage interfaces as models

    When we encapsulate an API as a model, the main consideration is how to better use these APIs through the model, so encapsulating an API is usually encapsulating its use interface as a model.

2.Method of encapsulating API

2.1.Java Project

    You can encapsulate models in Java projects.

2.2.Define the model using xworker.lang.MetaDescriptor3

    Generally, xworker.lang.MetaDescriptor3 is used to define the model.

2.3.The behavior of the model uses JavaAction

    The implementation of the model's behavior generally uses JavaAction, which calls Java code.

2.4.Implementation of Java Code for Model Behavior

     Models can generally call Java's static methods with an ActionContext parameter and an unlimited return value.

	public static void println(ActionContext actionContext) throws IOException {
		Thing self = actionContext.getObject("self");
		
		PrintWriter writer = getPrintWriter(self, actionContext);
		String varName = self.doAction("getWriterWar", actionContext);
		if(varName != null && !"".equals(varName)) {
			actionContext.peek().put(varName, writer);
		}
		for(Thing child : self.getChilds()) {
			if(!"actions".equals(child.getThingName())) {
				Object result = child.getAction().run(actionContext);
				if(result == null && UtilData.isTrue(self.doAction("isIgnoreNull", actionContext))) {
					continue;
				}
				writer.println(result);
			}
		}
		
		if(UtilData.isTrue(self.doAction("isClose", actionContext))) {
			writer.close();
		}
	}

3.Skills in Defining Models

3.1.Using API    

    The purpose of packaging APIs into models is to use them better, so we should consider how to package APIs into models from the perspective of how to use them better and more conveniently.

3.2.Object model

    If an API is object-oriented, it can be defined as an object model, that is, the attributes and sub-nodes of the model are designed to create corresponding Java objects. For example, the interface model of SWT.

3.3.Action model

    If an API is a method or function, it can be defined as an action model. For example, compress, copy files and so on.

 

Copyright ©  2007-2019 XWorker.org  版权所有  沪ICP备08000575号