Skip to content

Commit 1dad272

Browse files
committed
template method added
1 parent 7473c69 commit 1dad272

File tree

15 files changed

+326
-0
lines changed

15 files changed

+326
-0
lines changed

template-method/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# template-method
2+
This is repository of http://androidcode.pl blog. It shows uses of Template method in Android. It is a part of Design Patterns - Template method post in the blog.

template-method/example/Main.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class Main {
2+
3+
public static void main() {
4+
//check the user choice and set proper RouteFinder
5+
RouteFinder routeFinder = new CarRoute();
6+
7+
//get coordinates of choosen start and destination points
8+
Coordinates start = new Coordinates(52.409538, 16.931993); //Poznań
9+
Coordinates destination = new Coordinates(52.237049, 21.017532); //Warsaw
10+
11+
//find route
12+
//show progress
13+
boolean success = routeFinder.find(start, destination);
14+
//hide progress
15+
if(success) {
16+
Route route = routeFinder.getRoute();
17+
//show route on map with tips
18+
}
19+
else {
20+
//show proper message
21+
}
22+
}
23+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
public abstract class RouteFinder {
2+
3+
private Route route = new Route();
4+
5+
public Route find(Coordinates start, Coordinates destination) {
6+
route = new Route();
7+
Marker startPoint = Map.getMarker(start);
8+
Marker endPoint = Map.getMarker(destination);
9+
10+
logSearching("Search trace: " + start.toString() + " - " + destination.toString());
11+
12+
saveSearch(startPoint);
13+
saveSearch(destinationPoint);
14+
15+
List<Marker> trackPoints = getTrackPoints(startPoint, destinationPoint);
16+
if(trackPoints.size() > 2) {
17+
List<Tip> tips = getRouteTips(trackPoints);
18+
route.setTrackPoints(trackPoints);
19+
route.setTips(tips);
20+
return true;
21+
}
22+
else return false;
23+
}
24+
25+
public Route getRoute() {
26+
return route;
27+
}
28+
29+
protected abstract List<Marker> getTrackPoints(Marker start, Marker destination);
30+
protected abstract List<Tip> getRouteTips(List<Marker> trackPoints);
31+
32+
protected void saveSearch(Marker point) {
33+
//add search to local memory to improve search suggestions
34+
}
35+
36+
protected void logSearching(String text) {
37+
Logger.log(text);
38+
}
39+
40+
//some others operations
41+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public class Logger {
2+
3+
//some fields
4+
5+
public static void log(String text) {
6+
//log text
7+
}
8+
9+
//other methods
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class Map {
2+
3+
//some fields
4+
5+
public static Marker getMarker(Coordinates coordinates) {
6+
//get marker from Map data
7+
return new Marker("Name", "description", new Coordinates(52.409538, 16.931993), Type.CAR);
8+
}
9+
10+
//other methods
11+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Coordinates {
2+
3+
private double latitude;
4+
private double longitude;
5+
6+
public Coordinates(double latitude, double longitude) {
7+
this.latitude = latitude;
8+
this.longitude = longitude;
9+
}
10+
11+
public double getLatitude() {
12+
return latitude;
13+
}
14+
15+
public double getLongitude() {
16+
return longitude;
17+
}
18+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
public class Marker {
2+
3+
private String name;
4+
private String description;
5+
private Coordinates coordinates;
6+
private Type type;
7+
//other fields
8+
9+
public Marker(String name, String description, Coordinates coordinates, Type type) {
10+
this.name = name;
11+
this.description = description;
12+
this.coordinates = coordinates;
13+
this.type = type;
14+
}
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public String getDescription() {
21+
return description;
22+
}
23+
24+
public Coordinates getCoordinates() {
25+
return coordinates;
26+
}
27+
28+
public Type getType() {
29+
return type;
30+
}
31+
32+
public String toString() {
33+
return "Marker\n name: " + name + "\n" + "description: " + description + "\n"
34+
+ "coordinates: " + coordinates.getLatitude() + ", " + coordinates.getLongitude() + "\n"
35+
+ "type: " + type.toString();
36+
}
37+
38+
//other methods
39+
40+
public enum Type {
41+
42+
WALK(1),
43+
CAR(2),
44+
PUBLIC_TRANSPORT(3),
45+
TRUCK(4);
46+
//more types
47+
48+
private int type;
49+
50+
public Type(int type) {
51+
this.type = type;
52+
}
53+
54+
public String toString() {
55+
switch(type) {
56+
case WALK:
57+
return "WALK";
58+
case CAR:
59+
return "CAR";
60+
case PUBLIC_TRANSPORT:
61+
return "PUBLIC_TRANSPORT";
62+
case TRUCK:
63+
return "TRUCK";
64+
default:
65+
return "UNKNOWN";
66+
}
67+
}
68+
}
69+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
public class Route {
2+
3+
private List<Marker> markers;
4+
private List<Tip> tips;
5+
//other fields
6+
7+
public Route() {
8+
markers = new ArrayList();
9+
tips = new ArrayList();
10+
}
11+
12+
public void setMarkers(List<Marker> markers) {
13+
this.markers = markers;
14+
}
15+
16+
public void setTips(List<Tip> tips) {
17+
this.tips = tips;
18+
}
19+
20+
public List<Marker> getMarkers() {
21+
return markers;
22+
}
23+
24+
public List<Tip> getTips() {
25+
return tips;
26+
}
27+
28+
//other methods
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class Tip {
2+
3+
private String description;
4+
private Date time;
5+
private int distance;
6+
//other fields
7+
8+
public Tip(String description, Date time, int distance) {
9+
this.description = description;
10+
this.time = time;
11+
this.distance = distance;
12+
}
13+
14+
public String getDescription() {
15+
return description;
16+
}
17+
18+
public Date getStartTime() {
19+
return time;
20+
}
21+
22+
public int getDistance() {
23+
return distance;
24+
}
25+
26+
//other methods
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class CarRoute extends RouteFinder {
2+
3+
@Override
4+
protected List<Marker> getTrackPoints(Marker start, Marker destination) {
5+
List<Marker> markers = new ArrayList();
6+
//find possible roads
7+
//add track all track points from the road
8+
return markers;
9+
}
10+
11+
@Override
12+
protected List<Tip> getRouteTips(List<Marker> trackPoints) {
13+
List<Tip> tips = new ArrayList();
14+
//add tips for every crossing
15+
return tips;
16+
}
17+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class PublicTransportRoute extends RouteFinder {
2+
3+
@Override
4+
protected List<Marker> getTrackPoints(Marker start, Marker destination) {
5+
List<Marker> markers = new ArrayList();
6+
//find the nereast station to start Marker and the nearest station to destination
7+
//connect with public transport providers to get possible routes between stations
8+
//add walk or car track points between start Marker and start station
9+
//add stations to track points
10+
//add walk or car track points between end station and destination Marker
11+
//add trackpoints from start to nerest station
12+
return markers;
13+
}
14+
15+
@Override
16+
protected List<Tip> getRouteTips(List<Marker> trackPoints) {
17+
List<Tip> tips = new ArrayList();
18+
//connect with public transport providers to get schedule
19+
//add tips for every station
20+
return tips;
21+
}
22+
23+
@Override
24+
protected void logSearching(String text) {
25+
super.logSearching(text);
26+
//and send analytics data to public transport providers
27+
}
28+
29+
@Override
30+
protected void saveSearch(Marker point) {
31+
//do not save public transport searching
32+
}
33+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public abstract class AbstractClass {
2+
3+
public void templateMethod() {
4+
primitiveOperation1();
5+
primitiveOperation2();
6+
primitiveOperation3();
7+
}
8+
9+
protected abstract void primitiveOperation2();
10+
11+
private void primitiveOperation1() {
12+
//do something always
13+
}
14+
15+
protected void primitiveOperation3() {
16+
//do something optional
17+
}
18+
19+
//some others operations
20+
}

template-method/pattern/Main.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class Main {
2+
3+
public static void main() {
4+
AbstractClass object = new ConcreteClass1();
5+
object.templateMethod();
6+
}
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class ConcreteClass1 extends AbstractClass {
2+
3+
@Override
4+
protected void primitiveOperation2() {
5+
//do custom work for ConcreteClass1
6+
}
7+
8+
@Override
9+
protected void primitiveOperation3() {
10+
//do custom work for ConcreteClass2 by overriding default
11+
}
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class ConcreteClass2 extends AbstractClass {
2+
3+
@Override
4+
protected void primitiveOperation2() {
5+
//do custom work for ConcreteClass2
6+
}
7+
}

0 commit comments

Comments
 (0)