Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2bfa529

Browse files
committed
Added first draft of Visitor pattern. Needs cleaning up.
1 parent e37e636 commit 2bfa529

File tree

14 files changed

+640
-6
lines changed

14 files changed

+640
-6
lines changed

‎README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Any comments/bugs/better ways of doing things, send 'em my way.
2323
1. Observer
2424
1. Command
2525
1. Template Method
26+
1. Visitor
2627

2728
### Creational Patterns
2829

@@ -36,9 +37,4 @@ Any comments/bugs/better ways of doing things, send 'em my way.
3637

3738
1. Adapter
3839
1. Decorator
39-
1. Facade
40-
41-
##### Patterns coming soon...
42-
43-
1. Proxy
44-
1. Interpreter
40+
1. Facade
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2016 Gareth Jon Lynch
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
* this software and associated documentation files (the "Software"), to deal in
8+
* the Software without restriction, including without limitation the rights to
9+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
* the Software, and to permit persons to whom the Software is furnished to do so,
11+
* subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
package com.gazbert.patterns.behavioural.visitor;
25+
26+
import java.math.BigDecimal;
27+
28+
/**
29+
* A domain class that represents a Trade in our class hierarchy/object graph.
30+
*
31+
* It is a Concrete Element and holds the Object Structure: it holds references to ALL objects that can be visited -
32+
* this allows us to iterate through and 'visit' them in the accept method.
33+
*
34+
* @author gazbert
35+
* @since 2016年02月27日
36+
*/
37+
public abstract class AbstractTrade implements Trade { // Trade interface modified to extend Visitable
38+
39+
private String currency;
40+
private BigDecimal price;
41+
42+
private BuyOrder buyOrder;
43+
private SellOrder sellOrder;
44+
private TransactionFee fee;
45+
46+
47+
public AbstractTrade(String currency, BigDecimal amount) {
48+
49+
this.currency = currency;
50+
this.price = amount;
51+
52+
// do all existing functionality...
53+
buyOrder = new BuyOrder();
54+
sellOrder = new SellOrder();
55+
fee = new TransactionFee();
56+
}
57+
58+
public BigDecimal getPrice() {
59+
return price;
60+
}
61+
62+
public String getCurrency() {
63+
return currency;
64+
}
65+
66+
public TransactionFee getFee() {
67+
return fee;
68+
}
69+
70+
/*
71+
* The new Visitor method. We iterate through the object graph/hierarchy to 'visit' them.
72+
*/
73+
@Override
74+
public void accept(TradeVisitor visitor) {
75+
76+
// visit each component first - this just sets the Visitor on each of Elements in our Trade object graph
77+
// - nothing happens yet...
78+
buyOrder.accept(visitor);
79+
sellOrder.accept(visitor);
80+
81+
// then we visit the receiver - the double dispatch (callback on invoker) - this triggers the Visitor logic
82+
// on all of our Elements in the Trade object graph.
83+
visitor.visit(this);
84+
}
85+
86+
public String toString() {
87+
return getClass().getName() + " : Price= " + this.getPrice();
88+
}
89+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2016 Gareth Jon Lynch
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
* this software and associated documentation files (the "Software"), to deal in
8+
* the Software without restriction, including without limitation the rights to
9+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
* the Software, and to permit persons to whom the Software is furnished to do so,
11+
* subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
package com.gazbert.patterns.behavioural.visitor;
25+
26+
import java.math.BigDecimal;
27+
28+
/**
29+
* The base domain class that makes up part of a Trade in our class hierarchy/object graph.
30+
*
31+
* Does not form part of Visitor pattern per se.
32+
*
33+
* @author gazbert
34+
* @since 2016年02月27日
35+
*/
36+
public abstract class AbstractTradePart {
37+
38+
private BigDecimal price;
39+
private String currency;
40+
// etc... etc..
41+
42+
43+
public String getCurrency() {
44+
return currency;
45+
}
46+
47+
public void setCurrency(String currency) {
48+
this.currency = currency;
49+
}
50+
51+
public BigDecimal getPrice() {
52+
return price;
53+
}
54+
55+
public void setPrice(BigDecimal price) {
56+
this.price = price;
57+
}
58+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2016 Gareth Jon Lynch
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
* this software and associated documentation files (the "Software"), to deal in
8+
* the Software without restriction, including without limitation the rights to
9+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
* the Software, and to permit persons to whom the Software is furnished to do so,
11+
* subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
package com.gazbert.patterns.behavioural.visitor;
25+
26+
/**
27+
* A domain class that makes up part of a Trade in our class hierarchy/object graph.
28+
*
29+
* It is a Concrete Element.
30+
*
31+
* It has been modified to implement the Visitable (Element).
32+
*
33+
* @author gazbert
34+
* @since 2016年02月27日
35+
*/
36+
// public class BuyOrder extends AbstractTradePart { // here previously
37+
public class BuyOrder extends AbstractTradePart implements Visitable {
38+
39+
@Override
40+
public void accept(TradeVisitor visitor) {
41+
visitor.visit(this); // makes callback to the Visitor
42+
}
43+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2016 Gareth Jon Lynch
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
* this software and associated documentation files (the "Software"), to deal in
8+
* the Software without restriction, including without limitation the rights to
9+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
* the Software, and to permit persons to whom the Software is furnished to do so,
11+
* subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
package com.gazbert.patterns.behavioural.visitor;
25+
26+
import java.math.BigDecimal;
27+
28+
/**
29+
* A Futures trade domain object.
30+
*
31+
* @author gazbert
32+
* @since 2016年02月27日
33+
*/
34+
public class FuturesTrade extends AbstractTrade {
35+
36+
public FuturesTrade(String currency, BigDecimal amount) {
37+
super(currency, amount);
38+
}
39+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2016 Gareth Jon Lynch
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
* this software and associated documentation files (the "Software"), to deal in
8+
* the Software without restriction, including without limitation the rights to
9+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
* the Software, and to permit persons to whom the Software is furnished to do so,
11+
* subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
package com.gazbert.patterns.behavioural.visitor;
25+
26+
/**
27+
* A domain class that makes up part of a Trade in our class hierarchy/object graph.
28+
*
29+
* It is a Concrete Element.
30+
*
31+
* It has been modified to implement the Visitable (Element).
32+
*
33+
* @author gazbert
34+
* @since 2016年02月27日
35+
*/
36+
// public class SellOrder extends AbstractTradePart { // here previously
37+
public class SellOrder extends AbstractTradePart implements Visitable {
38+
39+
@Override
40+
public void accept(TradeVisitor visitor) {
41+
visitor.visit(this); // makes callback to the Visitor
42+
}
43+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2016 Gareth Jon Lynch
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
* this software and associated documentation files (the "Software"), to deal in
8+
* the Software without restriction, including without limitation the rights to
9+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
* the Software, and to permit persons to whom the Software is furnished to do so,
11+
* subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
package com.gazbert.patterns.behavioural.visitor;
25+
26+
import java.math.BigDecimal;
27+
28+
/**
29+
* A Spot trade domain object.
30+
*
31+
* @author gazbert
32+
* @since 2016年02月27日
33+
*/
34+
public class SpotTrade extends AbstractTrade {
35+
36+
public SpotTrade(String currency, BigDecimal amount) {
37+
super(currency, amount);
38+
}
39+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2016 Gareth Jon Lynch
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
* this software and associated documentation files (the "Software"), to deal in
8+
* the Software without restriction, including without limitation the rights to
9+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
* the Software, and to permit persons to whom the Software is furnished to do so,
11+
* subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
package com.gazbert.patterns.behavioural.visitor;
25+
26+
import java.math.BigDecimal;
27+
28+
/**
29+
* A domain class that makes up part of a Trade in our class hierarchy/object graph.
30+
*
31+
* It has been modified to implement the Visitable (Element).
32+
*
33+
* @author gazbert
34+
* @since 2016年02月27日
35+
*/
36+
// public interface Trade { // here previously
37+
public interface Trade extends Visitable { // we impl Visitor pattern - we now extend Visitable
38+
39+
// existing methods
40+
BigDecimal getPrice();
41+
String getCurrency();
42+
43+
// new method to accept our Visitor
44+
void accept(TradeVisitor visitor);
45+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /