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 1778d54

Browse files
added Proxy pattern
1 parent d7e868b commit 1778d54

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package design_patterns
2+
3+
/**
4+
*
5+
* Proxy is a structural design pattern that provides an object
6+
*
7+
* that controls access to another object by intercepting all calls.
8+
*
9+
* There are several types of this pattern:
10+
*
11+
* 1) Logging proxy - saves all calls to a real object with their parameters to the log (shown in this example)
12+
* 2) Remote proxy - provides communication with an object which is located in a different address space or on a remote machine (Java Remote Procedure Call)
13+
* 3) Virtual proxy - ensures that a real object is created only when it is actually needed (shown in this example)
14+
* 4) Protection proxy - checks whether the caller has the necessary rights to execute the request
15+
* 5) Caching proxy - provides temporary storage of calculation results
16+
* 6) Synchronizing proxy - performs synchronized access control to an object in a multi-threaded environment (Collections.synchronized** methods)
17+
* 7) Smart reference proxy - performs additional actions when a link is created to an object
18+
*
19+
*/
20+
21+
interface AudioPlayer {
22+
val currentState: String
23+
24+
fun play(audio: String)
25+
fun pause()
26+
fun resume()
27+
fun stop()
28+
}
29+
30+
// we have the simple player simulation
31+
class AudioPlayerImpl : AudioPlayer {
32+
33+
private var currentAudio: String = ""
34+
private var currentStatus = MediaPlayerStatus.NOT_INITIALIZED
35+
36+
override val currentState: String
37+
get() = """
38+
current audio: $currentAudio
39+
current status: $currentStatus
40+
""".trimIndent()
41+
42+
override fun play(audio: String) {
43+
if (audio.isNotBlank()) {
44+
currentAudio = audio
45+
currentStatus = MediaPlayerStatus.RUNNING
46+
}
47+
}
48+
override fun pause() {
49+
if (currentStatus == MediaPlayerStatus.RUNNING) {
50+
currentStatus = MediaPlayerStatus.PAUSED
51+
}
52+
}
53+
54+
override fun resume() {
55+
if (currentStatus == MediaPlayerStatus.PAUSED) {
56+
currentStatus = MediaPlayerStatus.RUNNING
57+
}
58+
}
59+
60+
override fun stop() {
61+
if (currentStatus != MediaPlayerStatus.NOT_INITIALIZED) {
62+
currentAudio = ""
63+
currentStatus = MediaPlayerStatus.NOT_INITIALIZED
64+
}
65+
}
66+
67+
enum class MediaPlayerStatus {
68+
NOT_INITIALIZED, PAUSED, RUNNING
69+
}
70+
71+
}
72+
73+
// MediaPlayerProxy defers the creation of a real object (virtual proxy) and logs all its calls (logging proxy)
74+
class AudioPlayerProxy : AudioPlayer {
75+
76+
// deferred creation of the real player
77+
private val player: AudioPlayer by lazy { AudioPlayerImpl() }
78+
79+
override val currentState: String
80+
get() = player.currentState
81+
82+
override fun play(audio: String) {
83+
player.play(audio)
84+
// logs real player calls
85+
println("play audio: $audio")
86+
}
87+
88+
override fun pause() {
89+
player.pause()
90+
println("pause")
91+
}
92+
93+
override fun resume() {
94+
player.resume()
95+
println("resume")
96+
}
97+
98+
override fun stop() {
99+
player.stop()
100+
println("stop")
101+
}
102+
103+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package design_patterns
2+
3+
import org.junit.Test
4+
import org.junit.Assert.assertEquals
5+
6+
class ProxyTest {
7+
8+
@Test
9+
fun test() {
10+
// we use the proxy object instead of a real one
11+
val mediaPlayer = AudioPlayerProxy()
12+
13+
mediaPlayer.play("track_0.mp3")
14+
assertEquals("""
15+
current audio: track_0.mp3
16+
current status: RUNNING
17+
""".trimIndent(), mediaPlayer.currentState)
18+
19+
mediaPlayer.pause()
20+
assertEquals("""
21+
current audio: track_0.mp3
22+
current status: PAUSED
23+
""".trimIndent(), mediaPlayer.currentState)
24+
25+
mediaPlayer.resume()
26+
assertEquals("""
27+
current audio: track_0.mp3
28+
current status: RUNNING
29+
""".trimIndent(), mediaPlayer.currentState)
30+
31+
mediaPlayer.stop()
32+
assertEquals("""
33+
current audio:
34+
current status: NOT_INITIALIZED
35+
""".trimIndent(), mediaPlayer.currentState)
36+
}
37+
38+
}

0 commit comments

Comments
(0)

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