Skip to content

Commit 9604e80

Browse files
Tij generic 170530 (#4)
* 170537-Generic * 0528-Generic * Chapter-15-Generic-btp0530
1 parent 3018663 commit 9604e80

9 files changed

Lines changed: 789 additions & 0 deletions

src/btp/oneP/ApplyTest.java

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package btp.oneP;
2+
3+
import java.lang.reflect.Method;
4+
import java.util.ArrayList;
5+
import java.util.Iterator;
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
9+
public class ApplyTest {
10+
11+
public static void main(String[] args) throws NoSuchMethodException, SecurityException {
12+
List<Shape> shapes = new ArrayList<Shape>();
13+
for(int i=0;i<10;i++){
14+
shapes.add(new Shape());
15+
}
16+
Apply.apply(shapes, Shape.class.getMethod("rotate"));
17+
18+
Apply.apply(shapes, Shape.class.getMethod("resize", int.class), 5);
19+
20+
21+
List<Square> squares = new ArrayList<Square>();
22+
for(int i=0;i<10;i++){
23+
squares.add(new Square());
24+
}
25+
Apply.apply(squares, Square.class.getMethod("rotate"));
26+
27+
Apply.apply(squares, Square.class.getMethod("resize", int.class), 5);
28+
29+
30+
Apply.apply(new FilledList<Shape>(Shape.class,5), Shape.class.getMethod("rotate"));
31+
Apply.apply(new FilledList<Shape>(Square.class,18), Shape.class.getMethod("resize", int.class),15);
32+
33+
SimpleQueue<Shape> shapeQ = new SimpleQueue<Shape>();
34+
for(int i=0;i<5;i++){
35+
shapeQ.add(new Shape());
36+
shapeQ.add(new Square());
37+
}
38+
Apply.apply(shapeQ, Shape.class.getMethod("rotate"));
39+
}
40+
41+
}
42+
43+
class Apply{
44+
public static <T,S extends Iterable<? extends T>> void apply(S seq,Method f,Object...args){
45+
try{
46+
for(T t:seq){
47+
f.invoke(t, args);
48+
}
49+
}catch(Exception e){
50+
throw new RuntimeException();
51+
}
52+
}
53+
}
54+
55+
class Shape{
56+
public void rotate(){
57+
System.out.println(this+" rotate");
58+
}
59+
public void resize(int newSize){
60+
System.out.println(this+" resize"+newSize);
61+
}
62+
}
63+
64+
class Square extends Shape{}
65+
66+
class FilledList<T> extends ArrayList<T>{
67+
public FilledList(Class<? extends T> type,int size){
68+
try{
69+
for(int i = 0;i<size;i++){
70+
this.add(type.newInstance());
71+
}
72+
}catch(Exception e){
73+
throw new RuntimeException();
74+
}
75+
}
76+
}
77+
78+
class SimpleQueue<T> implements Iterable<T>{
79+
private LinkedList<T> storage = new LinkedList<T>();
80+
public void add(T t){
81+
this.storage.offer(t);
82+
}
83+
public T get(){
84+
return this.storage.poll();
85+
}
86+
@Override
87+
public Iterator<T> iterator() {
88+
return this.storage.iterator();
89+
}
90+
91+
}
92+
93+

src/btp/oneP/Decoration.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package btp.oneP;
2+
3+
import java.util.Date;
4+
5+
public class Decoration {
6+
7+
public static void main(String[] args) {
8+
TimeStamped t = new TimeStamped(new Basic());
9+
TimeStamped t2 = new TimeStamped(new SerialNumbered(new Basic()));
10+
//t2.getSerialNumber();//Not available
11+
System.out.println(t2.getStamp());
12+
SerialNumbered s = new SerialNumbered(new Basic());
13+
SerialNumbered s2 = new SerialNumbered(new TimeStamped(new Basic()));
14+
SerialNumbered s3 = new SerialNumbered(new TimeStamped(new Basic()));
15+
System.out.println(s.getSerialNumber());
16+
System.out.println(s2.getSerialNumber());
17+
System.out.println(s3.getSerialNumber());
18+
//s2.getStamp();//Not available
19+
}
20+
21+
}
22+
23+
24+
class Basic{
25+
private String value;
26+
public void set(String value){
27+
this.value = value;
28+
}
29+
public String get(){
30+
return value;
31+
}
32+
}
33+
34+
class Decorator extends Basic{
35+
protected Basic basic;
36+
public Decorator(Basic basic){
37+
this.basic = basic;
38+
}
39+
public void set(String val){
40+
basic.set(val);
41+
}
42+
public String get(){
43+
return basic.get();
44+
}
45+
}
46+
47+
class TimeStamped extends Decorator{
48+
private final long timeStamp;
49+
public TimeStamped(Basic basic) {
50+
super(basic);
51+
timeStamp = new Date().getTime();
52+
}
53+
public long getStamp(){
54+
return timeStamp;
55+
}
56+
}
57+
58+
class SerialNumbered extends Decorator{
59+
private static long counter = 1;
60+
private final long serialNumber = counter++;
61+
public SerialNumbered(Basic basic) {
62+
super(basic);
63+
}
64+
public long getSerialNumber(){
65+
return serialNumber;
66+
}
67+
}

src/btp/oneP/DecoratorTest.java

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package btp.oneP;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class DecoratorTest {
7+
8+
@SuppressWarnings("static-access")
9+
public static void main(String[] args) {
10+
OriginalCoffee originalCoffee = new OriginalCoffee();
11+
double priceMilk = 10.0;
12+
Milk milkCoffee = new Milk(originalCoffee,priceMilk);
13+
milkCoffee.mixing();
14+
System.out.println("当前咖啡中的配料:"+milkCoffee.foodList);
15+
System.out.println("当前咖啡价格:"+milkCoffee.getPrice());
16+
System.out.println("---------------------------");
17+
double priceMatcha = 20.0;
18+
Matcha matchaCoffee = new Matcha(milkCoffee,priceMatcha);
19+
matchaCoffee.mixing();
20+
System.out.println("当前咖啡中的配料:"+matchaCoffee.foodList);
21+
System.out.println("当前咖啡价格:"+matchaCoffee.getPrice());
22+
System.out.println("---------------------------");
23+
double priceChocolate = 30.0;
24+
Chocolate chocolateCoffee = new Chocolate(matchaCoffee,priceChocolate);
25+
chocolateCoffee.mixing();
26+
System.out.println("当前咖啡中的配料:"+chocolateCoffee.foodList);
27+
System.out.println("当前咖啡价格:"+chocolateCoffee.getPrice());
28+
System.out.println("---------------------------");
29+
}
30+
31+
}
32+
33+
/**
34+
* 装饰品和被装饰器的共同接口
35+
*
36+
*/
37+
interface CoffeeTaste{
38+
//返回价格
39+
double getPrice();
40+
//搅拌咖啡
41+
void mixing();
42+
}
43+
44+
/**
45+
* 原始的被装饰器
46+
*
47+
*/
48+
class OriginalCoffee implements CoffeeTaste{
49+
private String name;
50+
public OriginalCoffee(){
51+
name = "原味咖啡";
52+
System.out.println("原味咖啡的价格是:50");
53+
}
54+
@Override
55+
public double getPrice() {
56+
//原味咖啡的价钱
57+
return 50;
58+
}
59+
60+
@Override
61+
public void mixing() {
62+
System.out.println("--搅拌咖啡");
63+
}
64+
65+
}
66+
67+
/**
68+
* 装饰品的共同父类
69+
*
70+
*/
71+
abstract class SnacksAndDesserts implements CoffeeTaste{
72+
public static List<String> foodList= new ArrayList<String>();
73+
//小食和甜品的价格
74+
private double additionalPrice;
75+
//要加入小食和甜品的咖啡
76+
CoffeeTaste coffeeTaste;
77+
public SnacksAndDesserts(CoffeeTaste coffeeTaste,double additionalPrice){
78+
this.coffeeTaste = coffeeTaste;
79+
this.additionalPrice = additionalPrice;
80+
}
81+
//返回价格
82+
@Override
83+
public double getPrice() {
84+
double priceNow = coffeeTaste.getPrice()+additionalPrice;
85+
return priceNow;
86+
}
87+
88+
abstract void add();
89+
}
90+
91+
/**
92+
* 牛奶类
93+
*
94+
*/
95+
class Milk extends SnacksAndDesserts{
96+
97+
public Milk(CoffeeTaste coffeeTaste, double additionalPrice) {
98+
super(coffeeTaste, additionalPrice);
99+
System.out.println("O(∩_∩)O~~牛奶的价格是:"+additionalPrice);
100+
}
101+
102+
@Override
103+
void add() {
104+
System.out.println("--咖啡中加入牛奶");
105+
SnacksAndDesserts.foodList.add("牛奶");
106+
}
107+
108+
@Override
109+
public void mixing() {
110+
add();
111+
System.out.println("--牛奶加入咖啡中搅拌");
112+
}
113+
114+
115+
}
116+
117+
118+
/**
119+
* 抹茶类
120+
*/
121+
class Matcha extends SnacksAndDesserts{
122+
123+
public Matcha(CoffeeTaste coffeeTaste, double additionalPrice) {
124+
super(coffeeTaste, additionalPrice);
125+
System.out.println("O(∩_∩)O~~抹茶的价格是:"+additionalPrice);
126+
}
127+
128+
@Override
129+
void add() {
130+
System.out.println("--咖啡中混入抹茶");
131+
SnacksAndDesserts.foodList.add("抹茶");
132+
}
133+
134+
@Override
135+
public void mixing() {
136+
add();
137+
System.out.println("--抹茶加入咖啡中搅拌");
138+
}
139+
}
140+
141+
/**
142+
* 巧克力类
143+
*/
144+
145+
class Chocolate extends SnacksAndDesserts{
146+
147+
public Chocolate(CoffeeTaste coffeeTaste, double additionalPrice) {
148+
super(coffeeTaste, additionalPrice);
149+
System.out.println("O(∩_∩)O~~巧克力的价格是:"+additionalPrice);
150+
}
151+
152+
@Override
153+
void add() {
154+
System.out.println("--咖啡中加入巧克力");
155+
SnacksAndDesserts.foodList.add("巧克力");
156+
}
157+
158+
@Override
159+
public void mixing() {
160+
add();
161+
System.out.println("--咖啡加入咖啡中搅拌");
162+
}
163+
}

src/btp/oneP/DogsAndRobots.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package btp.oneP;
2+
3+
public class DogsAndRobots {
4+
5+
public static void main(String[] args) {
6+
PerformingDog d = new PerformingDog();
7+
Robott r = new Robott();
8+
Communicate.perform(d);
9+
Communicate.perform(r);
10+
}
11+
12+
}
13+
14+
interface Performs{
15+
void speak();
16+
void sit();
17+
}
18+
19+
class PerformingDog extends Dog implements Performs{
20+
21+
@Override
22+
public void speak() {
23+
System.out.println("Woof!");
24+
}
25+
26+
@Override
27+
public void sit() {
28+
System.out.println("Sitting!");
29+
}
30+
31+
public void reproduce(){}
32+
}
33+
34+
class Robott implements Performs{
35+
36+
@Override
37+
public void speak() {
38+
System.out.println("Click!");
39+
}
40+
41+
@Override
42+
public void sit() {
43+
System.out.println("Clank!");
44+
}
45+
46+
public void oilChange(){}
47+
48+
}
49+
50+
class Communicate{
51+
public static <T extends Performs> void perform(T performs){
52+
performs.speak();
53+
performs.sit();
54+
}
55+
56+
//下面和上面的一样,不构成重载
57+
/*public static void perform(Performs p){
58+
p.speak();
59+
p.sit();
60+
}*/
61+
}

0 commit comments

Comments
 (0)