Skip to content

Commit 82ad31e

Browse files
committed
facade added
1 parent 295d805 commit 82ad31e

14 files changed

+298
-0
lines changed

facade/README.md

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

facade/example/Main.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
public class Main {
2+
3+
private MediaPlayer mediaPlayer;
4+
5+
public static void main() {
6+
mediaPlayer = new MediaPlayer(HardwarePlayer.Source.INTERNAL, true);
7+
}
8+
9+
private void volumeDownClick() {
10+
mediaPlayer.decreaseVolume();
11+
}
12+
13+
private void volumeUpClick() {
14+
mediaPlayer.increaseVolume();
15+
}
16+
17+
private void nextSongClick() {
18+
mediaPlayer.nextSong();
19+
}
20+
21+
private void previousSongClick() {
22+
mediaPlayer.previousSong();
23+
}
24+
25+
private void stopClick() {
26+
mediaPlayer.stop();
27+
}
28+
29+
private void playClick() {
30+
if(mediaPlayer.isPlaying()) {
31+
mediaPlayer.pause();
32+
//change image for play
33+
}
34+
else {
35+
mediaPlayer.play();
36+
//change image for pause
37+
}
38+
}
39+
}

facade/example/MediaPlayer.java

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
public class MediaPlayer {
2+
3+
private FileManager fileManager;
4+
private HardwarePlayer hardwarePlayer;
5+
private SongFile songFile;
6+
private List<SongFile> songs;
7+
private int currentSong;
8+
private int currentVolume;
9+
private boolean isPlaying;
10+
11+
public MediaPlayer(Source hardwareSource, boolean isSdCard) {
12+
fileManager = new FileManager(isSdCard);
13+
hardwarePlayer = new HardwarePlayer(hardwareSource);
14+
songs = new ArrayList<>();
15+
currentSong = 0;
16+
currentVolume = 5;
17+
isPlaying = false;
18+
createPlaylist();
19+
}
20+
21+
public void play() {
22+
loadSong();
23+
hardwarePlayer.play();
24+
isPlaying = true;
25+
}
26+
27+
public void pause() {
28+
hardwarePlayer.pause();
29+
isPlaying = false;
30+
}
31+
32+
public void stop() {
33+
hardwarePlayer.stop();
34+
currentSong = 0;
35+
isPlaying = false;
36+
}
37+
38+
public void increaseVolume() {
39+
if(currentVolume < 10)
40+
currentVolume++;
41+
changeVolume();
42+
}
43+
44+
public void decreaseVolume() {
45+
if(currentVolume > 0)
46+
currentVolume--;
47+
changeVolume();
48+
}
49+
50+
public void nextSong() {
51+
if(currentSong == songs.size() + 1)
52+
currentSong = 0;
53+
else
54+
currentSong++;
55+
changeSong();
56+
}
57+
58+
public void previousSong() {
59+
if(currentSong == 0)
60+
currentSong = songs.size() - 1;
61+
else
62+
currentSong--;
63+
changeSong();
64+
}
65+
66+
public void loadSong() {
67+
SongBytes songBytes = Converter.convertToBytes(songFile);
68+
hardwarePlayer.load(songBytes);
69+
}
70+
71+
public void createPlaylist() {
72+
List<File> files = fileManager.readSongsList();
73+
songs = Converter.convertToSongFiles(files);
74+
}
75+
76+
public boolean isPlaying() {
77+
return isPlaying;
78+
}
79+
80+
private void changeVolume() {
81+
hardwarePlayer.pause();
82+
hardwarePlayer.setVolume(currentVolume);
83+
hardwarePlayer.start();
84+
}
85+
86+
private void changeSong() {
87+
songFile = songs.get(currentSong);
88+
hardwarePlayer.stop();
89+
play();
90+
}
91+
92+
//other methods
93+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public final class Converter {
2+
3+
public static SongBytes convertToBytes(SongFile songFile) {
4+
//implementation;
5+
return new SongBytes(); //mock
6+
}
7+
8+
public static List<SongFile> convertToSongFiles(List<File> file) {
9+
//implementation;
10+
return Collections.emptyList(); //mock
11+
}
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class FileManager {
2+
3+
public FileManager(boolean isSdCard) {
4+
//implementation
5+
}
6+
7+
public List<File> readSongsList() {
8+
//implementation
9+
return Collections.emptyList(); //mock
10+
}
11+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
public class HardwarePlayer {
2+
3+
//variables
4+
5+
public HardwarePlayer(Source source) {
6+
//implementation;
7+
}
8+
9+
public void load(SongBytes songBytes) {
10+
//implementation
11+
}
12+
13+
public void play() {
14+
//implementation
15+
}
16+
17+
public void start() {
18+
//implementation
19+
}
20+
21+
public void pause() {
22+
//implementation
23+
}
24+
25+
public void stop() {
26+
//implementation;
27+
}
28+
29+
public void setVolume(int volume) {
30+
//implementation
31+
}
32+
33+
public enum Source {
34+
INTERNAL(0),
35+
EXTERNAL(1);
36+
37+
int source;
38+
Source(int source) {
39+
this.source = source;
40+
}
41+
}
42+
43+
//other methods
44+
45+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public class SongBytes {
2+
3+
//implementation
4+
}

facade/example/elements/SongFile.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public class SongFile {
2+
3+
//implementation
4+
}

facade/pattern/Action.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public enum Action {
2+
3+
INCREASE(0),
4+
DECREASE(1),
5+
DOUBLE(2);
6+
7+
int action;
8+
Action(int action) {
9+
this.action = action;
10+
}
11+
}

facade/pattern/Element1.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class Element1 {
2+
3+
public void refreshState() {
4+
5+
}
6+
7+
public State getState() {
8+
return new State();
9+
}
10+
11+
public void increase() {
12+
13+
}
14+
15+
public void decrease() {
16+
17+
}
18+
19+
public void makeDouble() {
20+
21+
}
22+
}

facade/pattern/Element2.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public class Element2 {
2+
3+
public void restart() {
4+
5+
}
6+
7+
public void startAction(State state) {
8+
9+
}
10+
}

facade/pattern/Facade.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
public class Facade {
2+
3+
private Element1 element1;
4+
private Element2 element2;
5+
6+
public Facade() {
7+
element1 = new Element1();
8+
element2 = new Element2();
9+
}
10+
11+
public void operation1(Action action) {
12+
if(action == Action.INCREASE)
13+
element1.increase();
14+
else if(action == Action.DECREASE)
15+
element1.decrease();
16+
else if(action == Action.DOUBLE)
17+
element1.makeDouble();
18+
else
19+
element2.restart();
20+
}
21+
22+
public void operation2() {
23+
if(Utils.isAccessProvided()) {
24+
element2.startAction(element1.getState());
25+
element1.refreshState();
26+
}
27+
else
28+
Utils.printError();
29+
}
30+
}

facade/pattern/State.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public class State {
2+
3+
//implementation
4+
}

facade/pattern/Utils.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public final class Utils {
2+
3+
public static boolean isAccessProvided() {
4+
return true;
5+
}
6+
7+
public static void printError() {
8+
9+
}
10+
11+
}

0 commit comments

Comments
 (0)