J2SE 5.0 Generic Application

xiaoxiao2021-03-06  63

J2SE 5.0 Generic Application 1: Type Safety Functor 1. Introduction Functional programming is a very common and very important programming paradigm, and some languages ​​provide support, C provides flexible flexible Support, and the functional programming in Java is not widely applied due to the limitations of the language itself. The Apache Commons Functor project is a functional programming library under development, but it is not currently safe; J2SE 5.0 provides limited Generic capability, in addition to use in Collection, Type Safety Functor is also useful, and an open source project Generic Algorithms for Java has started working in this area.

One dollar function, predicate, process public interface unaryfunction {r evatarate (p Obj);} public interface unarypredicate {Boolean Test (t obj);} public interface unaryProcedure {void Run (t obj }

Binary functions, predicates, process public interface binaryfunction {r evrating (t left, s right);} public interface binarypredicate {boolean test (t left, s right);} public Interface BinaryProcedure {Void Run (t left, s right);}

Charge 1: Filter PUBLIC Interface Filter Extends UnaryFunction {}

Several example algorithm: transform, select, foreach public static List transform (Collection source, UnaryFunction transformer) {List result = new ArrayList ( ); For (Source Item: Source) {Result.Add (item);} Return Result;} public static list Select (Collection Source, UnaryPredicate Selector) {List Result = new arraylist (); for (t item: source) {if (select.test (item)) {result.add (item);}}} Return Result;} > Void Foreach (Collection Source, UnaryProcedure Procedure) {for (t item: source) {procedure.run (item);}}

Several composite: And, Or, Not public class And implements UnaryPredicate , Serializable {private UnaryPredicate [] predicates; public And (UnaryPredicate ... predicates) {this.predicates = predicates PUBLIC Boolean Test (T Obj) {for (UnaryPredicate Predicate: Predicate) {if (! Predicate.test (obj)) {Return False;}} return true;} public static and AND (unarypredicate ... predicates) {return new and (Predicates);}} public class or imports unarypredicate , serializable {private unarypredicate [] PREDICES; public OR UnaryPredicate ... Predicates) {this.predicates = predicates;} public boolean test ({IF (unarypredicate predicate: predicate) {if (Predicate.test (obj)) {Return true;} } Return False;} public static or or (unarypredicate ... predicates) {RETU rn new Or (predicates);}} public class Not implements UnaryPredicate , Serializable {private UnaryPredicate predicate; public Not (UnaryPredicate predicate) {this.predicate = predicate;} Public Boolean Test (T Obj) {Return! PrediCate.test (Obj); PUBLIC Static NOT NOT (UnaryPredicate Predicate) {Return New Not (PREDICATE);}} Question 1, Interface vs. functorfunctor is a "Interface containing only one Method" in Java. You can use the functionor in most places to use the interface. The advantage is low intrusion (no need to implement the interface), the disadvantage is that the class cannot be accessed Private information;

Given the current implementation of generic, the Module of Java Dynamic Characteristics (Serialized / Deserialized, Reflection, etc.), priority uses Interface, other occasions can be used as appropriate, Function vs. functor is internally used, FUNCTION, in multiple places, make functor3, classic vs. modern branch control statement if, else, switch in object-oriented programs, can use "subclass" large-area elimination traditional loop control statement for, Do WHILE should also use the "Algorithm Functor" to eliminate J2SE 5.0 Generic Application 2: Type Security Multi-return value 1. Introduction OUT parameters and REF parameters provide the same function to return multiple values, C and C # separately With reference and OUT, REF keyword support, Java generally uses array or collection to solve this problem; Generic provides another way, but it is not convenient to be convenient than arrays, so it is just for generic and generic, no actual Value; additional PAIR and other technologies are often used, in fact, Map.Entry II, example

OUT public class out {private t obj = null; public t get () {return obj;} public void set (t obj) {this.obj = Obj;}}

Ref public class ref {private t obj = null; public refholder (t obj) {this.obj = Obj;} public t get () {return obj;} public void set (t obj) {this.obj = Obj;}}

转载请注明原文地址:https://www.9cbs.com/read-83580.html

New Post(0)