Skip to content

Commit 3f36d4f

Browse files
committed
java lambda 08.25.18
java lambda 08.25.18
1 parent 2641b4a commit 3f36d4f

File tree

5 files changed

+150
-0
lines changed

5 files changed

+150
-0
lines changed

java-lambda/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Using Lambda to Implement Funtional Interface Method
2+
====================================================
3+
4+
- We have a *virtual* `Button` that when clicked will call an abstract method **onClick()** of a listener interface `OnClickListener`.
5+
- We need to implement the **onClick()** so that it prints the `Button` name.
6+
7+
8+
`OnClickListener.java`
9+
10+
```java
11+
package com.hmkcode;
12+
13+
public interface OnClickListener {
14+
void onClick(Button button);
15+
}
16+
```
17+
18+
`Button.java`
19+
20+
```java
21+
package com.hmkcode;
22+
23+
public class Button {
24+
25+
private OnClickListener onClickListener;
26+
private String name;
27+
28+
// click the button
29+
public void click(){
30+
this.onClickListener.onClick(this);
31+
}
32+
33+
// getters & setters
34+
}
35+
```
36+

java-lambda/pom.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.hmkcode</groupId>
8+
<artifactId>java-lambda</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<name>java-lambda</name>
12+
13+
<build>
14+
<plugins>
15+
<plugin>
16+
<artifactId>maven-compiler-plugin</artifactId>
17+
<version>3.8.0</version>
18+
<configuration>
19+
<source>1.8</source>
20+
<target>1.8</target>
21+
</configuration>
22+
</plugin>
23+
<plugin>
24+
<groupId>org.codehaus.mojo</groupId>
25+
<artifactId>exec-maven-plugin</artifactId>
26+
<version>1.6.0</version>
27+
<configuration>
28+
<mainClass>com.hmkcode.App</mainClass>
29+
</configuration>
30+
</plugin>
31+
</plugins>
32+
</build>
33+
34+
</project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.hmkcode;
2+
3+
4+
public class App implements OnClickListener
5+
{
6+
public static void main( String[] args ){
7+
System.out.println( "Running App..." );
8+
new App().run();
9+
}
10+
11+
public void run(){
12+
13+
Button myButton = new Button();
14+
myButton.setName("MyButton");
15+
16+
// 1. implements onClickListener
17+
//myButton.setOnClickListener(this);
18+
19+
// 2. anonymous class
20+
/*myButton.setOnClickListener(new OnClickListener() {
21+
@Override
22+
public void onClick(Button button) {
23+
System.out.println(button.getName() +" Clicked! - anonymous class");
24+
}
25+
}); */
26+
27+
// 3. lambda
28+
OnClickListener lambda = button -> { System.out.println(button.getName()+" Clicked! - lambda"); } ;
29+
myButton.setOnClickListener(lambda);
30+
31+
32+
// click the button
33+
myButton.click();
34+
}
35+
36+
37+
@Override
38+
public void onClick(Button button) {
39+
System.out.println(button.getName() +" Clicked! - implements interface");
40+
41+
}
42+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.hmkcode;
2+
3+
public class Button {
4+
5+
private OnClickListener onClickListener;
6+
private String name;
7+
8+
public void click(){
9+
this.onClickListener.onClick(this);
10+
}
11+
12+
13+
public String getName() {
14+
return name;
15+
}
16+
17+
public void setName(String name) {
18+
this.name = name;
19+
}
20+
21+
public OnClickListener getOnClickListener() {
22+
return onClickListener;
23+
}
24+
25+
public void setOnClickListener(OnClickListener onClickListener) {
26+
this.onClickListener = onClickListener;
27+
}
28+
29+
30+
31+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.hmkcode;
2+
3+
4+
public interface OnClickListener {
5+
void onClick(Button button);
6+
7+
}

0 commit comments

Comments
 (0)