Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Common/src/main/java/pl/mperor/lab/common/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ default E getThird() {
return getNext(2);
}

default E getForth() {
return getNext(3);
}

private E getNext(int index) {
if (this.isEmpty() && index >= size()) {
throw new NoSuchElementException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.NoSuchElementException;
import java.util.stream.Stream;

class UtilsTest {
class ReadableOutputTest {

@Test
public void testReadableOutputReturnsReadableList() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package pl.mperor.lab.java.design.pattern.structural.proxy;

public interface ExecutableService {
void execute();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package pl.mperor.lab.java.design.pattern.structural.proxy;

class ServiceImpl implements ExecutableService {

ServiceImpl() {
System.out.println("Service has been initialized!");
}

@Override
public void execute() {
System.out.println("Method has been executed!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package pl.mperor.lab.java.design.pattern.structural.proxy;

import java.util.function.Supplier;

public enum ServiceProvider implements Supplier<ExecutableService> {
INSTANCE;

@Override
public ExecutableService get() {
return new ServiceProxy();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package pl.mperor.lab.java.design.pattern.structural.proxy;

class ServiceProxy implements ExecutableService {

private ExecutableService service;

@Override
public void execute() {
System.out.println("Before executing service method!");
lazyLoadingService();
service.execute();
System.out.println("After executing service method!");
}

private void lazyLoadingService() {
if (service == null) {
service = new ServiceImpl();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package pl.mperor.lab.java.design.pattern.structural.proxy;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.util.StringUtils;
import pl.mperor.lab.common.TestUtils;

public class ServiceProviderTest {

@Test
public void testServiceUsingProxyUnderneath() {
var out = TestUtils.setTempSystemOut();
var service = ServiceProvider.INSTANCE.get();
Assertions.assertTrue(StringUtils.isBlank(out.all()),
"Lazy loading service should not be initialized!");
Assertions.assertInstanceOf(ServiceProxy.class, service);

service.execute();
var outLines = out.lines();
Assertions.assertEquals(outLines.getFirst(), "Before executing service method!");
Assertions.assertEquals(outLines.getSecond(), "Service has been initialized!");
Assertions.assertEquals(outLines.getThird(), "Method has been executed!");
Assertions.assertEquals(outLines.getForth(), "After executing service method!");
TestUtils.resetSystemOut();
}

}
Loading