Skip to content

Commit d67476d

Browse files
committed
Add proxy example with test
1 parent 7ee18f3 commit d67476d

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package pl.mperor.lab.java.design.pattern.structural.proxy;
2+
3+
public interface ExecutableService {
4+
void execute();
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package pl.mperor.lab.java.design.pattern.structural.proxy;
2+
3+
class ServiceImpl implements ExecutableService {
4+
5+
ServiceImpl() {
6+
System.out.println("Service has been initialized!");
7+
}
8+
9+
@Override
10+
public void execute() {
11+
System.out.println("Method has been executed!");
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package pl.mperor.lab.java.design.pattern.structural.proxy;
2+
3+
import java.util.function.Supplier;
4+
5+
public enum ServiceProvider implements Supplier<ExecutableService> {
6+
INSTANCE;
7+
8+
@Override
9+
public ExecutableService get() {
10+
return new ServiceProxy();
11+
}
12+
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package pl.mperor.lab.java.design.pattern.structural.proxy;
2+
3+
class ServiceProxy implements ExecutableService {
4+
5+
private ExecutableService service;
6+
7+
@Override
8+
public void execute() {
9+
System.out.println("Before executing service method!");
10+
lazyLoadingService();
11+
service.execute();
12+
System.out.println("After executing service method!");
13+
}
14+
15+
private void lazyLoadingService() {
16+
if (service == null) {
17+
service = new ServiceImpl();
18+
}
19+
}
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package pl.mperor.lab.java.design.pattern.structural.proxy;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.platform.commons.util.StringUtils;
6+
import pl.mperor.lab.common.TestUtils;
7+
8+
public class ServiceProviderTest {
9+
10+
@Test
11+
public void testServiceUsingProxyUnderneath() {
12+
var out = TestUtils.setTempSystemOut();
13+
var service = ServiceProvider.INSTANCE.get();
14+
Assertions.assertTrue(StringUtils.isBlank(out.all()),
15+
"Lazy loading service should not be initialized!");
16+
Assertions.assertInstanceOf(ServiceProxy.class, service);
17+
18+
service.execute();
19+
var outLines = out.lines();
20+
Assertions.assertEquals(outLines.getFirst(), "Before executing service method!");
21+
Assertions.assertEquals(outLines.getSecond(), "Service has been initialized!");
22+
Assertions.assertEquals(outLines.getThird(), "Method has been executed!");
23+
Assertions.assertEquals(outLines.getForth(), "After executing service method!");
24+
TestUtils.resetSystemOut();
25+
}
26+
27+
}

0 commit comments

Comments
 (0)