Skip to content

Commit d451d49

Browse files
author
turingfly
committed
Design Patterns and Principles
1 parent 0a769f2 commit d451d49

7 files changed

+336
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package designPatternsAndPrinciples;
2+
3+
/**
4+
*
5+
* @author chengfeili Jun 4, 2017 11:12:31 PM
6+
*
7+
* In object‐oriented design, we refer to object composition as the
8+
* property of constructing a class using references to other classes in
9+
* order to reuse the functionality of the other classes.
10+
*/
11+
12+
class Flippers {
13+
public void flap() {
14+
System.out.println("The flippers flap back and forth");
15+
}
16+
}
17+
18+
class WebbedFeet {
19+
public void kick() {
20+
System.out.println("The webbed feet kick to and fro");
21+
}
22+
}
23+
24+
public class ComposingObjects {
25+
private final Flippers flippers;
26+
private final WebbedFeet webbedFeet;
27+
28+
public ComposingObjects() {
29+
this.flippers = new Flippers();
30+
this.webbedFeet = new WebbedFeet();
31+
}
32+
33+
public void flap() {
34+
this.flippers.flap();
35+
}
36+
37+
public void kick() {
38+
39+
this.webbedFeet.kick();
40+
}
41+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package designPatternsAndPrinciples;
2+
3+
/**
4+
*
5+
* @author chengfeili Jun 4, 2017 10:10:08 PM
6+
*
7+
*/
8+
@FunctionalInterface
9+
public interface DefineFunctionalInterface {
10+
public void sprint(String s);
11+
}
12+
13+
// 1. valid, extends from DefineFunctionalInterface
14+
interface Run1 extends DefineFunctionalInterface {
15+
}
16+
17+
// 2. valid, override
18+
19+
interface SprintFaster extends DefineFunctionalInterface {
20+
public void sprint(String s);
21+
}
22+
23+
// 3. valid, neither default and static is abstract
24+
25+
interface Skip extends DefineFunctionalInterface {
26+
public default int getHopCount(String s) {
27+
return 10;
28+
}
29+
30+
public static void skip(int speed) {
31+
32+
}
33+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package designPatternsAndPrinciples;
2+
3+
/**
4+
*
5+
* @author chengfeili
6+
* Jun 4, 2017 9:50:52 PM
7+
*
8+
*/
9+
interface Fly {
10+
public int getWingSpan() throws Exception;
11+
12+
public static final int MAX_SPEED = 100;
13+
14+
// default method is optionally overridden in the subclass
15+
public default void land() {
16+
System.out.println("Animal is landing");
17+
}
18+
19+
// it is available without an instance of the interface
20+
public static double calculateSpeed(float distance, double time) {
21+
return distance / time;
22+
}
23+
}
24+
25+
public class DesingInterface implements Fly {
26+
27+
@Override
28+
public int getWingSpan() throws Exception {
29+
// TODO Auto-generated method stub
30+
return 15;
31+
}
32+
33+
public void land() {
34+
System.out.println("Eagle is diving fast");
35+
}
36+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package designPatternsAndPrinciples;
2+
3+
/**
4+
*
5+
* @author chengfeili
6+
* Jun 4, 2017 9:59:54 PM
7+
*
8+
*/
9+
interface Walk {
10+
boolean isQuadruped();
11+
12+
abstract double getMaxSpeed();
13+
}
14+
15+
interface Run extends Walk {
16+
public abstract boolean canHuntWhileRunning();
17+
18+
abstract double getMaxSpeed();
19+
}
20+
21+
public class ExtendInterface implements Run {
22+
23+
@Override
24+
public boolean isQuadruped() {
25+
// TODO Auto-generated method stub
26+
return true;
27+
}
28+
29+
@Override
30+
public boolean canHuntWhileRunning() {
31+
// TODO Auto-generated method stub
32+
return true;
33+
}
34+
35+
@Override
36+
public double getMaxSpeed() {
37+
// TODO Auto-generated method stub
38+
return 100;
39+
}
40+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package designPatternsAndPrinciples;
2+
3+
import java.util.function.Predicate;
4+
5+
/**
6+
*
7+
* @author chengfeili
8+
* Jun 4, 2017 10:18:55 PM
9+
*
10+
*
11+
* ---------Valid lambda syntax:------------------
12+
* () -> new Duck()
13+
* d -> {return d.quack();}
14+
* (Duck d) -> d.quack()
15+
* (Animal a, Duck d) -> d.quack()
16+
*
17+
* () - true
18+
* a -> {return a.startsWith("a");}
19+
* (String a) -> a.startsWith("a)
20+
* (int x) -> {}
21+
* (int y) -> {return;}
22+
* (a, b) -> a.startsWith("a)
23+
*
24+
*
25+
* -----------Invalid lambda syntax: ---------------
26+
* Duck d -> d.quack() ==> (Duck d) -> d.quack()
27+
* a,d -> d.quack() ==> (a, d) -> d.quack()
28+
* Animal a, Duck d -> d.quack() ==> (Animal a, Duck d) -> d.quack()
29+
*
30+
* a, b - a.startsWith("a") ==> (a, b) -> a.startsWith("a")
31+
*
32+
* (when one parameter has a data type listed, though,
33+
* all parameters must provide a data type)
34+
* (int y, z) -> {int x = 1; return y + 10;}
35+
*
36+
* (a, b) -> {int a = 0; return 5;} (a is redeclared)
37+
*/
38+
class Animal {
39+
private String species;
40+
private boolean canHop;
41+
private boolean canSwim;
42+
43+
public Animal(String species, boolean canHop, boolean canSwim) {
44+
this.species = species;
45+
this.canHop = canHop;
46+
this.canSwim = canSwim;
47+
}
48+
49+
public boolean canHop() {
50+
return canHop;
51+
}
52+
53+
public boolean canSwim() {
54+
return canSwim;
55+
}
56+
57+
public String toString() {
58+
return species;
59+
}
60+
}
61+
62+
interface CheckTrait {
63+
public boolean test(Animal a);
64+
}
65+
66+
public class ImplementingFunctionalInterfacesWithLambdas {
67+
68+
private static void print(Animal animal, Predicate<Animal> trait) {
69+
if (trait.test(animal))
70+
System.out.println(animal);
71+
}
72+
73+
public static void main(String[] args) {
74+
75+
// Java relies on context when figuring out what lambda expressions mean
76+
// a -> a.canHop() ==> (Animal a) -> {return a.canHop();}
77+
print(new Animal("fish", false, true), a -> a.canHop());
78+
print(new Animal("kangaroo", true, false), a -> a.canHop());
79+
}
80+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package designPatternsAndPrinciples;
2+
3+
/**
4+
*
5+
* @author chengfeili
6+
* Jun 4, 2017 10:45:20 PM
7+
*
8+
* Polymorphism is the ability of a single interface to support
9+
* multiple underlying forms. In Java, this allows multiple types
10+
* of objects to be passed to a single method or class.
11+
*/
12+
interface LivesInOcean {
13+
public void makeSound();
14+
}
15+
16+
class Dolphin implements LivesInOcean {
17+
public void makeSound() {
18+
System.out.println("whistle");
19+
}
20+
}
21+
22+
class Whale implements LivesInOcean {
23+
public void makeSound() {
24+
System.out.println("sing");
25+
}
26+
}
27+
28+
public class ImplementingPolymorphism {
29+
public void checkSound(LivesInOcean animal) {
30+
animal.makeSound();
31+
}
32+
33+
public static void main(String[] args) {
34+
ImplementingPolymorphism o = new ImplementingPolymorphism();
35+
o.checkSound(new Dolphin()); // whistle
36+
o.checkSound(new Whale()); // sing
37+
}
38+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package designPatternsAndPrinciples;
2+
3+
/**
4+
*
5+
* @author chengfeili Jun 4, 2017 10:52:45 PM
6+
*
7+
* If you use a variable to refer to an object, then only the methods or
8+
* variables that are part of the variable’s reference type can be
9+
* called without an explicit
10+
*
11+
* --------Object and reference--------
12+
* In Java, all objects are
13+
* accessed by reference, so as a developer you never have direct access
14+
* to the memory of the object itself.
15+
*
16+
* 1. The type of the object determines which properties exist within
17+
* the object in memory.
18+
* 2. The type of the reference to the object
19+
* determines which methods and variables are accessible to the Java
20+
* program.
21+
*
22+
*
23+
* --------Casting Object reference--------
24+
*
25+
* 1. Casting an object from a subclass to a superclass doesn’t require
26+
* an explicit cast.
27+
* 2. Casting an object from a superclass to a
28+
* subclass requires an explicit cast.
29+
* 3. The compiler will not allow
30+
* casts to unrelated types.
31+
* 4. Even when the code compiles without
32+
* issue, an exception may be thrown at runtime if the object being cast
33+
* is not actually an instance of that class.
34+
*/
35+
36+
class Primate {
37+
public boolean hasHair() {
38+
return true;
39+
}
40+
}
41+
42+
interface HasTail {
43+
public boolean isTailStriped();
44+
}
45+
46+
public class OneObjectTakeManyDifferentForms extends Primate implements HasTail {
47+
public int age = 10;
48+
49+
public boolean isTailStriped() {
50+
return false;
51+
}
52+
53+
public static void main(String[] args) {
54+
OneObjectTakeManyDifferentForms lemur = new OneObjectTakeManyDifferentForms();
55+
System.out.println(lemur.age); // 10
56+
57+
HasTail hasTail = lemur;
58+
System.out.println(hasTail.isTailStriped()); // false
59+
// System.out.println(hasTail.hasHair()); // Does not compile
60+
61+
Primate primate = lemur;
62+
// OneObjectTakeManyDifferentForms lemur2 = primate; // Does not compile
63+
// OneObjectTakeManyDifferentForms lemur2 =
64+
// OneObjectTakeManyDifferentForms)primate;
65+
System.out.println(primate.hasHair()); // true
66+
67+
}
68+
}

0 commit comments

Comments
 (0)