File tree Expand file tree Collapse file tree 7 files changed +83
-1
lines changed
main/java/pl/mperor/lab/common
test/java/pl/mperor/lab/common
main/java/pl/mperor/lab/java/design/pattern/structural/proxy
test/java/pl/mperor/lab/java/design/pattern/structural/proxy Expand file tree Collapse file tree 7 files changed +83
-1
lines changed Original file line number Diff line number Diff line change @@ -47,6 +47,10 @@ default E getThird() {
4747 return getNext (2 );
4848 }
4949
50+ default E getForth () {
51+ return getNext (3 );
52+ }
53+
5054 private E getNext (int index ) {
5155 if (this .isEmpty () && index >= size ()) {
5256 throw new NoSuchElementException ();
Original file line number Diff line number Diff line change 66import java .util .NoSuchElementException ;
77import java .util .stream .Stream ;
88
9- class UtilsTest {
9+ class ReadableOutputTest {
1010
1111 @ Test
1212 public void testReadableOutputReturnsReadableList () {
Original file line number Diff line number Diff line change 1+ package pl .mperor .lab .java .design .pattern .structural .proxy ;
2+
3+ public interface ExecutableService {
4+ void execute ();
5+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments