Skip to content

Commit 6f53a38

Browse files
move random java code into a separate repo
1 parent 5ccb83c commit 6f53a38

File tree

88 files changed

+2594
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+2594
-1
lines changed

Gospel.ser

223 Bytes
Binary file not shown.

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# RandomJava
2-
Any random Java goes here.
2+
Please download spring-*.jar, apache-logging, JAF, Javamail and other necessary jars and put them onto your classpath or add them as external libraries, otherwise your IntelliJ/Eclipse won't smile. ^ ^
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package designPatterns.chapter_1_strategy_pattern;
2+
3+
public interface CookBehavior {
4+
5+
public void cook();
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package designPatterns.chapter_1_strategy_pattern;
2+
3+
public class CookNoodlesBehavior implements CookBehavior {
4+
5+
@Override
6+
public void cook() {
7+
System.out.println("I'm also fond of cooking Chinese noodles!");
8+
}
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package designPatterns.chapter_1_strategy_pattern;
2+
3+
public class CookRiceFlour implements CookBehavior {
4+
5+
@Override
6+
public void cook() {
7+
System.out.println("I'm cooking MiFen, aka, Rice Flour.");
8+
}
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package designPatterns.chapter_1_strategy_pattern;
2+
3+
public interface LoveBehavior {
4+
5+
public void showLove();
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package designPatterns.chapter_1_strategy_pattern;
2+
3+
public class LoveHusbandBehavior implements LoveBehavior {
4+
5+
@Override
6+
public void showLove() {
7+
System.out.println("I love my husband Steve Sun, he is just awesome!");
8+
}
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package designPatterns.chapter_1_strategy_pattern;
2+
3+
public class ModelWife extends Wife {
4+
5+
public ModelWife() {
6+
loveBehavior = new LoveHusbandBehavior();
7+
cookBehavior = new CookRiceFlour();
8+
}
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package designPatterns.chapter_1_strategy_pattern;
2+
3+
public class RealWife extends Wife {
4+
5+
public RealWife() {
6+
System.out.println("I'm the real wife - Sophie Yan! I'm Sophie Yan, the wife of Steve Sun, and I like cooking for my husband and family. ");
7+
this.cookBehavior = new CookRiceFlour();
8+
this.loveBehavior = new LoveHusbandBehavior();
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package designPatterns.chapter_1_strategy_pattern;
2+
3+
/**
4+
* The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable.
5+
* Strategy lets the algorithm vary independently from clients that use it.*/
6+
7+
/**Design principle:
8+
* Favor composition over inheritance.
9+
*
10+
* Not only does it let you encapsulate a family of algorithms into their own set of classes, but it also lets you change behavior at runtime.*/
11+
public class StrategtyPatternTestDrive {
12+
13+
public static void main(String[] args) {
14+
Wife wife = new RealWife();
15+
wife.performLove();
16+
wife.performCook();;
17+
18+
wife.setCookBehavior(new CookNoodlesBehavior());
19+
wife.performCook();
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package designPatterns.chapter_1_strategy_pattern;
2+
3+
public abstract class Wife {
4+
5+
CookBehavior cookBehavior;
6+
LoveBehavior loveBehavior;
7+
8+
public Wife () {
9+
System.out.println("I'm the abstract wife - Sophie Yan!");
10+
};
11+
12+
public void setCookBehavior(CookBehavior cb){
13+
cookBehavior = cb;
14+
}
15+
16+
public void setLoveBehavior(LoveBehavior lb){
17+
loveBehavior = lb;
18+
}
19+
20+
public void performCook(){
21+
cookBehavior.cook();
22+
}
23+
24+
public void performLove(){
25+
loveBehavior.showLove();
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package designPatterns.chapter_2_observer_pattern;
2+
3+
public interface CoreFamily {
4+
public void registerRelative(Relative relative);
5+
public void removeRelative(Relative relative);
6+
public void notifyRelative();
7+
public void setCoreFamilyAttributes(String coreFamilyNames, int hikingTimes, int marriedMonths, String bibleReadingProgress);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package designPatterns.chapter_2_observer_pattern;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class CoreFamilyImpl implements CoreFamily {
7+
8+
private String coreFamilyName;
9+
10+
private String bibleReadingProgress;
11+
12+
private int monthsWeMarried;
13+
14+
private int timesWeHiked;
15+
16+
private List<Relative> relatives;
17+
18+
public CoreFamilyImpl(String bibleReadingProgress, String coreFamilyName, int monthsWeMarried, int timesWeHiked) {
19+
this.relatives = new ArrayList<Relative>();
20+
this.bibleReadingProgress = bibleReadingProgress;
21+
this.coreFamilyName = coreFamilyName;
22+
this.monthsWeMarried = monthsWeMarried;
23+
this.timesWeHiked = timesWeHiked;
24+
}
25+
26+
@Override
27+
public void registerRelative(Relative relative) {
28+
relatives.add(relative);
29+
}
30+
31+
@Override
32+
public void removeRelative(Relative relative) {
33+
int i = relatives.indexOf(relative);
34+
if(i > 0){
35+
relatives.remove(i);
36+
}
37+
}
38+
39+
@Override
40+
public void notifyRelative() {
41+
for(int i = 0; i < relatives.size(); i++){
42+
Relative relative = (Relative) relatives.get(i);
43+
relative.update(coreFamilyName, timesWeHiked, monthsWeMarried, bibleReadingProgress);
44+
}
45+
}
46+
47+
public void hasUpdates(){
48+
notifyRelative();
49+
}
50+
51+
@Override
52+
public void setCoreFamilyAttributes(String coreFamilyNames,
53+
int hikingTimes, int marriedMonths, String bibleReadingProgress) {
54+
this.coreFamilyName = coreFamilyNames;
55+
this.timesWeHiked = hikingTimes;
56+
this.monthsWeMarried = marriedMonths;
57+
this.bibleReadingProgress = bibleReadingProgress;
58+
hasUpdates();
59+
}
60+
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package designPatterns.chapter_2_observer_pattern;
2+
3+
/**NOTE: I didn't really implement the Observer pattern from Head First book here.
4+
* Just implemented the introduction part here. Could continue if interests arise in the future. - 10/04/2015*/
5+
6+
/**
7+
* The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.
8+
*
9+
* Subjects + Observers = Observer Pattern */
10+
11+
/**Design principle:
12+
* Strive for loosely coupled designs between objects that interact.*/
13+
public class ObserverPatternTestDrive {
14+
15+
public static void main(String[] args) {
16+
CoreFamily coreFamily = new CoreFamilyImpl(" 1 Peter ", "Steve Sun & Sophie Yan", 1, 7);
17+
Relative eason = new RelativeImpl("Eason", coreFamily);
18+
coreFamily.registerRelative(eason);
19+
Relative motherInLaw = new RelativeImpl("Jinzhi Lin", coreFamily);
20+
coreFamily.registerRelative(motherInLaw);
21+
Relative fatherInLaw = new RelativeImpl("Jianlin Yan", coreFamily);
22+
coreFamily.registerRelative(fatherInLaw);
23+
24+
coreFamily.setCoreFamilyAttributes("Sophie and Steve NEW NAME", 8, 2, "1 John");
25+
System.out.print("Program ended.");
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package designPatterns.chapter_2_observer_pattern;
2+
3+
public interface Relative {
4+
public void update(String coreFamilyNames, int hikingTimes, int marriedMonths, String bibleReadingProgress);
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package designPatterns.chapter_2_observer_pattern;
2+
3+
public class RelativeImpl implements Relative {
4+
5+
private String relativeName;
6+
7+
private CoreFamily coreFamily;
8+
9+
private String coreFamilyName;
10+
11+
private String bibleReadingProgress;
12+
13+
private int monthsWeMarried;
14+
15+
private int timesWeHiked;
16+
17+
public RelativeImpl(String relativeName, CoreFamily coreFamily) {
18+
this.relativeName = relativeName;
19+
this.coreFamily = coreFamily;
20+
}
21+
22+
@Override
23+
public void update(String coreFamilyNames, int hikingTimes, int marriedMonths, String bibleReadingProgress) {
24+
System.out.print("Before updating:\n");
25+
display();
26+
this.bibleReadingProgress = bibleReadingProgress;
27+
this.coreFamilyName = coreFamilyNames;
28+
this.monthsWeMarried = marriedMonths;
29+
this.timesWeHiked = hikingTimes;
30+
System.out.print("After updating:\n");
31+
display();
32+
}
33+
34+
private void display() {
35+
System.out.println("Current CoreFamilyAttributes are, coreFamilyName is: " + coreFamilyName + "\tbibleReadingProgress is: "
36+
+ bibleReadingProgress + "\tmonthsWeMarried is: " + monthsWeMarried + "\ttimesWeHiked is: " + timesWeHiked);
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package designPatterns.chapter_3_decorator_pattern;
2+
3+
import lombok.Getter;
4+
5+
public abstract class Beverage {
6+
@Getter
7+
String description = "Unknown beverage";
8+
9+
public abstract double cost();
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package designPatterns.chapter_3_decorator_pattern;
2+
3+
public abstract class CondimentDecorator extends Beverage {
4+
public abstract String getDescription();
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package designPatterns.chapter_3_decorator_pattern;
2+
3+
/**Classes should be open for extension and closed for modification.
4+
*
5+
* Decorator pattern: attach additional responsibilities to an object dynamically.*/
6+
public class DecoratorPatternTestDrive {
7+
8+
public static void main(String... args){
9+
System.out.println("Program started.");
10+
Beverage espresso = new Espresso();
11+
print(espresso);
12+
13+
Beverage houseBlend = new HouseBlend();
14+
houseBlend = new Mocha(houseBlend);
15+
print(houseBlend);
16+
17+
houseBlend = new Soy(houseBlend);
18+
print(houseBlend);
19+
20+
houseBlend = new Whip(houseBlend);
21+
print(houseBlend);
22+
23+
System.out.println("Program ended.");
24+
}
25+
26+
/**
27+
* @param beverage
28+
*/
29+
protected static void print(Beverage beverage) {
30+
System.out.println(beverage.getDescription() + " $" + beverage.cost());
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package designPatterns.chapter_3_decorator_pattern;
2+
3+
public class Espresso extends Beverage {
4+
5+
public Espresso() {
6+
description = "Espresso";
7+
}
8+
9+
@Override
10+
public double cost() {
11+
return 1.99;
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package designPatterns.chapter_3_decorator_pattern;
2+
3+
public class HouseBlend extends Beverage {
4+
5+
public HouseBlend() {
6+
description = "House Blend Coffee";
7+
}
8+
9+
@Override
10+
public double cost() {
11+
return .89;
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package designPatterns.chapter_3_decorator_pattern;
2+
3+
public class Mocha extends CondimentDecorator {
4+
5+
Beverage beverage;
6+
7+
public Mocha(Beverage beverage){
8+
this.beverage = beverage;
9+
}
10+
11+
@Override
12+
public String getDescription() {
13+
return beverage.getDescription() + ", Mocha";
14+
}
15+
16+
@Override
17+
public double cost() {
18+
return .20 + beverage.cost();
19+
}
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package designPatterns.chapter_3_decorator_pattern;
2+
3+
public class Soy extends CondimentDecorator {
4+
5+
Beverage beverage;
6+
7+
public Soy(Beverage beverage){
8+
this.beverage = beverage;
9+
}
10+
11+
@Override
12+
public String getDescription() {
13+
return beverage.getDescription() + ", Soy";
14+
}
15+
16+
@Override
17+
public double cost() {
18+
return .99 + beverage.cost();
19+
}
20+
21+
}

0 commit comments

Comments
 (0)