Java/Methods: Difference between revisions
From charlesreid1
(Created page with "Basic idea of methods: procedural decomposition, separating parts of the task, making code more reusable ==Lambda Expressions== {{Main|Java/Lambdas}} When it comes to dea...") |
|||
| (One intermediate revision by the same user not shown) | |||
| Line 1: | Line 1: | ||
Basic idea of methods: procedural decomposition, separating parts of the task, making code more reusable | Basic idea of methods: procedural decomposition, separating parts of the task, making code more reusable | ||
==Methods as Objects== | |||
To wrap a method in an object that can be passed around, have a state, etc., you can use the "Command" design pattern, in which you encapsulate a single action in an object that contains all information and materials necessary to execute that command. | |||
An example from [https://stackoverflow.com/questions/2186931/java-pass-method-as-parameter#2186993 stack overflow]: | |||
<pre> | |||
public class CommandExample { | |||
public interface Command { | |||
public void execute(Object data); | |||
} | |||
public class PrintCommand implements Command { | |||
public void execute(Object data) { | |||
System.out.println(data.toString()); | |||
} | |||
} | |||
public static void callCommand(Command command, Object data) { | |||
command.execute(data); | |||
} | |||
public static void main(String... args) { | |||
callCommand(new PrintCommand(), "hello world"); | |||
} | |||
} | |||
</pre> | |||
==Lambda Expressions== | ==Lambda Expressions== | ||
| Line 10: | Line 36: | ||
Here is the Java tutorial on Lambda expressions: http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html | Here is the Java tutorial on Lambda expressions: http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html | ||
==Flags== | |||
{{CSFlag}} | |||
Latest revision as of 22:28, 5 July 2017
Basic idea of methods: procedural decomposition, separating parts of the task, making code more reusable
Methods as Objects
To wrap a method in an object that can be passed around, have a state, etc., you can use the "Command" design pattern, in which you encapsulate a single action in an object that contains all information and materials necessary to execute that command.
An example from stack overflow:
public class CommandExample {
public interface Command {
public void execute(Object data);
}
public class PrintCommand implements Command {
public void execute(Object data) {
System.out.println(data.toString());
}
}
public static void callCommand(Command command, Object data) {
command.execute(data);
}
public static void main(String... args) {
callCommand(new PrintCommand(), "hello world");
}
}
Lambda Expressions
When it comes to dealing with functions as first-class objects in the language, Java is much more limited than Python. In fact, it was not until Java 8 that Java finally got lambda expressions, which are ways of defining functions using in-place, shorthand expressions.
Here is the Java tutorial on Lambda expressions: http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
Flags
| Computer Science notes on computer science topics on the wiki, for educational and learning purposes
Part of the 2017 CS Study Plan.
Python/Exceptions · Python/Assertions · Python/Decorators Python/Os (os module) · Python/Strings Python/Splat · Python/Iterators · Python/Generators Python/Comparators · Python/Lambdas
Builtin features of Java: Java/Exceptions · Java/Assertions · Java/Memory · Java/Interfaces Java/Generics · Java/Decorators · Java/Diamond Notation Java/Iterators · Java/Iterable · Iterators vs Iterable Java/Comparators · Java/Comparable · Comparators vs Comparable Java/Numeric · Java/TypeChecking · Java/Testing · Java/Timing · Java/Profiling Documentation: Javadocs · Java/Documentation Tools and functionality: Java/URLs · Java/CSV External libraries: Guava · Fastutil · Eclipse Collections OOP: OOP Checklist · Java/Abstract Class · Java/Encapsulation · Java/Generics
|
See also: