-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathComposingObjects.java
More file actions
42 lines (34 loc) · 850 Bytes
/
ComposingObjects.java
File metadata and controls
42 lines (34 loc) · 850 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package designPatternsAndPrinciples;
/**
*
* @author chengfeili
* Jun 4, 2017 11:12:31 PM
*
* In object‐oriented design, we refer to object composition as the
* property of constructing a class using references to other classes in
* order to reuse the functionality of the other classes.
*/
class Flippers {
public void flap() {
System.out.println("The flippers flap back and forth");
}
}
class WebbedFeet {
public void kick() {
System.out.println("The webbed feet kick to and fro");
}
}
public class ComposingObjects {
private final Flippers flippers;
private final WebbedFeet webbedFeet;
public ComposingObjects() {
this.flippers = new Flippers();
this.webbedFeet = new WebbedFeet();
}
public void flap() {
this.flippers.flap();
}
public void kick() {
this.webbedFeet.kick();
}
}