Skip to content

Commit be56a43

Browse files
author
turingfly
committed
Design Patterns and Principles
1 parent d9acb52 commit be56a43

8 files changed

+10
-21
lines changed

Java-8/src/designPatternsAndPrinciples/ComposingObjects.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
/**
44
*
5-
* @author chengfeili Jun 4, 2017 11:12:31 PM
5+
* @author chengfeili
6+
* Jun 4, 2017 11:12:31 PM
67
*
78
* In object‐oriented design, we refer to object composition as the
89
* property of constructing a class using references to other classes in

Java-8/src/designPatternsAndPrinciples/DefineFunctionalInterface.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
/**
44
*
5-
* @author chengfeili Jun 4, 2017 10:10:08 PM
5+
* @author chengfeili
6+
* Jun 4, 2017 10:10:08 PM
67
*
78
*/
89
@FunctionalInterface
@@ -15,13 +16,11 @@ interface Run1 extends DefineFunctionalInterface {
1516
}
1617

1718
// 2. valid, override
18-
1919
interface SprintFaster extends DefineFunctionalInterface {
2020
public void sprint(String s);
2121
}
2222

2323
// 3. valid, neither default and static is abstract
24-
2524
interface Skip extends DefineFunctionalInterface {
2625
public default int getHopCount(String s) {
2726
return 10;

Java-8/src/designPatternsAndPrinciples/DesingInterface.java

-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@ public static double calculateSpeed(float distance, double time) {
2323
}
2424

2525
public class DesingInterface implements Fly {
26-
2726
@Override
2827
public int getWingSpan() throws Exception {
29-
// TODO Auto-generated method stub
3028
return 15;
3129
}
3230

Java-8/src/designPatternsAndPrinciples/ExtendInterface.java

-4
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,18 @@ interface Run extends Walk {
1919
}
2020

2121
public class ExtendInterface implements Run {
22-
2322
@Override
2423
public boolean isQuadruped() {
25-
// TODO Auto-generated method stub
2624
return true;
2725
}
2826

2927
@Override
3028
public boolean canHuntWhileRunning() {
31-
// TODO Auto-generated method stub
3229
return true;
3330
}
3431

3532
@Override
3633
public double getMaxSpeed() {
37-
// TODO Auto-generated method stub
3834
return 100;
3935
}
4036
}

Java-8/src/designPatternsAndPrinciples/FactoryPattern.java

-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public static Food getFood(String animalName) {
6868
case "polar bear":
6969
return new Fish(10);
7070
}
71-
7271
// Good practice to throw an exception if no matching subclass could be
7372
// found
7473
throw new UnsupportedOperationException("Unsupported animal: " + animalName);

Java-8/src/designPatternsAndPrinciples/ImplementingFunctionalInterfacesWithLambdas.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* a,d -> d.quack() ==> (a, d) -> d.quack()
2929
* Animal a, Duck d -> d.quack() ==> (Animal a, Duck d) -> d.quack()
3030
*
31-
* a, b - a.startsWith("a") ==> (a, b) -> a.startsWith("a")
31+
* a, b -> a.startsWith("a") ==> (a, b) -> a.startsWith("a")
3232
*
3333
* (when one parameter has a data type listed, though,
3434
* all parameters must provide a data type)
@@ -48,7 +48,6 @@ public Animal(String species, boolean canHop, boolean canSwim) {
4848
}
4949

5050
public Animal(String species2, int age, List<String> favoriteFoods) {
51-
// TODO Auto-generated constructor stub
5251
}
5352

5453
public boolean canHop() {
@@ -64,19 +63,18 @@ public String toString() {
6463
}
6564
}
6665

66+
@FunctionalInterface
6767
interface CheckTrait {
6868
public boolean test(Animal a);
6969
}
7070

7171
public class ImplementingFunctionalInterfacesWithLambdas {
72-
7372
private static void print(Animal animal, Predicate<Animal> trait) {
7473
if (trait.test(animal))
7574
System.out.println(animal);
7675
}
7776

7877
public static void main(String[] args) {
79-
8078
// Java relies on context when figuring out what lambda expressions mean
8179
// a -> a.canHop() ==> (Animal a) -> {return a.canHop();}
8280
print(new Animal("fish", false, true), a -> a.canHop());

Java-8/src/designPatternsAndPrinciples/OneObjectTakeManyDifferentForms.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
/**
44
*
5-
* @author chengfeili Jun 4, 2017 10:52:45 PM
5+
* @author chengfeili
6+
* Jun 4, 2017 10:52:45 PM
67
*
78
* If you use a variable to refer to an object, then only the methods or
89
* variables that are part of the variable’s reference type can be
@@ -19,9 +20,7 @@
1920
* determines which methods and variables are accessible to the Java
2021
* program.
2122
*
22-
*
2323
* --------Casting Object reference--------
24-
*
2524
* 1. Casting an object from a subclass to a superclass doesn’t require
2625
* an explicit cast.
2726
* 2. Casting an object from a superclass to a
@@ -39,6 +38,7 @@ public boolean hasHair() {
3938
}
4039
}
4140

41+
@FunctionalInterface
4242
interface HasTail {
4343
public boolean isTailStriped();
4444
}
@@ -60,8 +60,7 @@ public static void main(String[] args) {
6060

6161
Primate primate = lemur;
6262
// OneObjectTakeManyDifferentForms lemur2 = primate; // Does not compile
63-
// OneObjectTakeManyDifferentForms lemur2 =
64-
// OneObjectTakeManyDifferentForms)primate;
63+
// OneObjectTakeManyDifferentForms lemur2 = (OneObjectTakeManyDifferentForms)primate; // explicit cast
6564
System.out.println(primate.hasHair()); // true
6665

6766
}

Java-8/src/designPatternsAndPrinciples/SingletonPattern3.java

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
*/
1111
// Method 3: Lazy Instantiation. Delay creation of the singleton until the first
1212
// time the getInstance() method is called:
13-
1413
public class SingletonPattern3 {
1514
private static SingletonPattern3 instance;
1615

0 commit comments

Comments
 (0)