diff --git a/pom.xml b/pom.xml index ba42fd763..efa30a704 100644 --- a/pom.xml +++ b/pom.xml @@ -220,6 +220,7 @@ gateway slob server-session + virtual-proxy diff --git a/virtual-proxy/.gitignore b/virtual-proxy/.gitignore new file mode 100644 index 000000000..0d952be47 --- /dev/null +++ b/virtual-proxy/.gitignore @@ -0,0 +1,59 @@ +################## Eclipse ###################### +target +.metadata +.settings +.classpath +.project +*.class +tmp/ +*.tmp +*.bak +*~.nib +local.properties +.loadpath +.recommenders +.DS_Store + +####### Java annotation processor (APT) ######## +.factorypath + +################ Package Files ################## +*.jar +*.war +*.ear +*.swp +datanucleus.log +/bin/ +*.log +event-sourcing/Journal.json + +################## Checkstyle ################### +.checkstyle + +##################### STS ####################### +.apt_generated +.springBeans +.sts4-cache + +################# IntelliJ IDEA ################# +.idea +*.iws +*.iml +*.ipr + +################### NetBeans #################### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +#################### VS Code #################### +.vscode/ + +#################### Java Design Patterns ####### +etc/Java Design Patterns.urm.puml +serialized-entity/output.txt \ No newline at end of file diff --git a/virtual-proxy/README.md b/virtual-proxy/README.md new file mode 100644 index 000000000..10b856760 --- /dev/null +++ b/virtual-proxy/README.md @@ -0,0 +1,128 @@ +--- +title: Virtual Proxy +category: Structural +language: en +tag: + - Gang Of Four + - Decoupling +--- + +## Also known as + +Surrogate + +## Intent + +Provide a surrogate or placeholder for another object to control its creation and access, particularly when dealing with resource-intensive operations. + +## Explanation + +Real-world example + +> Consider an online video streaming platform where video objects are resource-intensive due to their large data size and required processing power. To efficiently manage resources, the system uses a virtual proxy to handle video objects. The virtual proxy defers the creation of actual video objects until they are explicitly required for playback, thus saving system resources and improving response times for users. + +In plain words + +> The virtual proxy pattern allows a representative class to stand in for another class to control access to it, particularly for resource-intensive operations. + +**Programmatic Example** + +Given our example of a video streaming service, here is how it might be implemented: + +First, we define an `ExpensiveObject` interface, which outlines the method for processing video. + +```java +public interface ExpensiveObject { + void process(); +} +``` + +Here’s the implementation of a `RealVideoObject` that represents an expensive-to-create video object. + +```java +@Getter +public class RealVideoObject implements ExpensiveObject { + private String videoData; + + public RealVideoObject() { + this.videoData = heavyInitialConfiguration(); + } + + private String heavyInitialConfiguration() { + System.out.println("Loading video data from the database or heavy computation..."); + return "Some loaded video data"; + } + + @Override + public void process() { + System.out.println("Playing video content with data: " + videoData); + } +} +``` + +The `VideoObjectProxy` serves as a stand-in for `RealExpensiveObject`. + +```java +@Getter +public class VideoObjectProxy implements ExpensiveObject { + private RealVideoObject realVideoObject; + + public void setRealVideoObject(RealVideoObject realVideoObject) { + this.realVideoObject = realVideoObject; + } + + @Override + public void process() { + if (realVideoObject == null) { + System.out.println("RealVideoObject is created on demand."); + realVideoObject = new RealVideoObject(); + } + realVideoObject.process(); + } +} +``` + +And here’s how the proxy is used in the system. + +``` +ExpensiveObject object = new VirtualProxy(); + object.process(); // The real object is created at this point + object.process(); // Uses the already created object +``` + +Program output: + +``` +Creating RealExpensiveObject only when it is needed. +Processing and playing video content... +Processing and playing video content... +``` + +## Class diagram + +![alt text](./etc/virtual.proxy.urm.png "Virtual Proxy pattern class diagram") + +## Applicability + +The virtual proxy pattern is useful when: + +* Object creation is resource-intensive, and not all instances are utilized immediately or ever. +* The performance of a system can be significantly improved by deferring the creation of objects until they are needed. +* There is a need for control over resource usage in systems dealing with large quantities of high-overhead objects. + +The virtual proxy pattern is typically used to: + +* Lazy Initialization: Create objects only when they are actually needed. +* Resource Management: Efficiently manage resources by creating heavy objects only on demand. +* Access Control: Include logic to check conditions before delegating calls to the actual object. +* Performance Optimization: Incorporate caching of results or states that are expensive to obtain or compute. +* Data Binding and Integration: Delay integration and binding processes until the data is actually needed. + +## Related patterns + +* [Proxy](https://github.com/iluwatar/java-design-patterns/tree/master/proxy) + +## Credits + +* [The Proxy Pattern in Java](https://www.baeldung.com/java-proxy-pattern) +* [What is the virtual proxy design pattern?](https://www.educative.io/answers/what-is-the-virtual-proxy-design-pattern) diff --git a/virtual-proxy/etc/virtual.proxy.urm.png b/virtual-proxy/etc/virtual.proxy.urm.png new file mode 100644 index 000000000..1baf530bd Binary files /dev/null and b/virtual-proxy/etc/virtual.proxy.urm.png differ diff --git a/virtual-proxy/etc/virtual.proxy.urm.puml b/virtual-proxy/etc/virtual.proxy.urm.puml new file mode 100644 index 000000000..9d3cc5d5d --- /dev/null +++ b/virtual-proxy/etc/virtual.proxy.urm.puml @@ -0,0 +1,29 @@ +@startuml +class Client { + + main(args : String[]) {static} +} + +class RealVideoObject { + - videoData : String + + RealVideoObject() + + process() + + getVideoData() : String + - heavyInitialConfiguration() : void +} + +interface ExpensiveObject { + + process() {abstract} +} + +class VideoObjectProxy { + - realVideoObject : RealVideoObject + + VideoObjectProxy() + + process() + + setRealVideoObject(realVideoObject : RealVideoObject) : void + + getRealVideoObject() : RealVideoObject +} + +VideoObjectProxy --> "-realVideoObject" RealVideoObject +RealVideoObject ..|> ExpensiveObject +VideoObjectProxy ..|> ExpensiveObject +@enduml diff --git a/virtual-proxy/pom.xml b/virtual-proxy/pom.xml new file mode 100644 index 000000000..8f3fbc8f2 --- /dev/null +++ b/virtual-proxy/pom.xml @@ -0,0 +1,73 @@ + + + + + 4.0.0 + + com.iluwatar + java-design-patterns + 1.26.0-SNAPSHOT + + virtual-proxy + + + org.junit.jupiter + junit-jupiter-engine + test + + + org.mockito + mockito-core + test + + + junit + junit + test + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + + + + App + + + + + + + + + \ No newline at end of file diff --git a/virtual-proxy/src/main/java/com/iluwatar/virtual/proxy/App.java b/virtual-proxy/src/main/java/com/iluwatar/virtual/proxy/App.java new file mode 100644 index 000000000..b29d6fd99 --- /dev/null +++ b/virtual-proxy/src/main/java/com/iluwatar/virtual/proxy/App.java @@ -0,0 +1,42 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.virtual.proxy; + +/** + * The main application class that sets up and runs the Virtual Proxy pattern demo. + */ +public class App { + /** + * The entry point of the application. + * + * @param args the command line arguments + */ + public static void main(String[] args) { + ExpensiveObject videoObject = new VideoObjectProxy(); + videoObject.process(); // The first call creates and plays the video + videoObject.process(); // Subsequent call uses the already created object + } +} \ No newline at end of file diff --git a/virtual-proxy/src/main/java/com/iluwatar/virtual/proxy/ExpensiveObject.java b/virtual-proxy/src/main/java/com/iluwatar/virtual/proxy/ExpensiveObject.java new file mode 100644 index 000000000..b5474e161 --- /dev/null +++ b/virtual-proxy/src/main/java/com/iluwatar/virtual/proxy/ExpensiveObject.java @@ -0,0 +1,32 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.virtual.proxy; + +/** + * Interface for expensive object and proxy object. + */ +public interface ExpensiveObject { + void process(); +} \ No newline at end of file diff --git a/virtual-proxy/src/main/java/com/iluwatar/virtual/proxy/RealVideoObject.java b/virtual-proxy/src/main/java/com/iluwatar/virtual/proxy/RealVideoObject.java new file mode 100644 index 000000000..dbdd4353f --- /dev/null +++ b/virtual-proxy/src/main/java/com/iluwatar/virtual/proxy/RealVideoObject.java @@ -0,0 +1,50 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.virtual.proxy; + +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; + +/** + * Represents a real video object that is expensive to create and manage. + */ +@Slf4j +@Getter +public class RealVideoObject implements ExpensiveObject { + + public RealVideoObject() { + heavyInitialConfiguration(); + } + + private void heavyInitialConfiguration() { + LOGGER.info("Loading initial video configurations..."); + } + + @Override + public void process() { + LOGGER.info("Processing and playing video content..."); + } +} \ No newline at end of file diff --git a/virtual-proxy/src/main/java/com/iluwatar/virtual/proxy/VideoObjectProxy.java b/virtual-proxy/src/main/java/com/iluwatar/virtual/proxy/VideoObjectProxy.java new file mode 100644 index 000000000..52b0e5f3f --- /dev/null +++ b/virtual-proxy/src/main/java/com/iluwatar/virtual/proxy/VideoObjectProxy.java @@ -0,0 +1,44 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.virtual.proxy; + +import lombok.Getter; + +/** + * A proxy class for the real video object, providing a layer of control over the object instantiation. + */ +@Getter +public class VideoObjectProxy implements ExpensiveObject { + private RealVideoObject realVideoObject; + + @Override + public void process() { + if (realVideoObject == null) { + realVideoObject = new RealVideoObject(); + } + realVideoObject.process(); + } +} \ No newline at end of file diff --git a/virtual-proxy/src/test/java/com/iluwatar/virtual/proxy/AppTest.java b/virtual-proxy/src/test/java/com/iluwatar/virtual/proxy/AppTest.java new file mode 100644 index 000000000..1f7cf8b6d --- /dev/null +++ b/virtual-proxy/src/test/java/com/iluwatar/virtual/proxy/AppTest.java @@ -0,0 +1,41 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.virtual.proxy; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +/** + * Application test + */ +class AppTest { + + @Test + void shouldExecuteApplicationWithoutException() { + assertDoesNotThrow(() -> App.main(new String[]{})); + } +} \ No newline at end of file diff --git a/virtual-proxy/src/test/java/com/iluwatar/virtual/proxy/RealVideoObjectTest.java b/virtual-proxy/src/test/java/com/iluwatar/virtual/proxy/RealVideoObjectTest.java new file mode 100644 index 000000000..90f203556 --- /dev/null +++ b/virtual-proxy/src/test/java/com/iluwatar/virtual/proxy/RealVideoObjectTest.java @@ -0,0 +1,53 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.virtual.proxy; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +import org.junit.jupiter.api.Test; + +/** + * Tests for RealVideoObject. + */ +class RealVideoObjectTest { + + @Test + void testVideoObject() { + var videoObject = new RealVideoObject(); + assertThat(videoObject, instanceOf(ExpensiveObject.class)); + } + + @Test + public void constructorDoesNotThrowException() { + assertDoesNotThrow(RealVideoObject::new, "Constructor should not throw any exception"); + } + + @Test + public void processDoesNotThrowException() { + RealVideoObject realVideoObject = new RealVideoObject(); + assertDoesNotThrow(realVideoObject::process, "Process method should not throw any exception"); + } +} \ No newline at end of file diff --git a/virtual-proxy/src/test/java/com/iluwatar/virtual/proxy/VideoObjectProxyTest.java b/virtual-proxy/src/test/java/com/iluwatar/virtual/proxy/VideoObjectProxyTest.java new file mode 100644 index 000000000..2ede2c8a4 --- /dev/null +++ b/virtual-proxy/src/test/java/com/iluwatar/virtual/proxy/VideoObjectProxyTest.java @@ -0,0 +1,53 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.virtual.proxy; + +import static org.hamcrest.core.IsInstanceOf.instanceOf; +import static org.junit.jupiter.api.Assertions.*; + +import org.hamcrest.MatcherAssert; +import org.junit.jupiter.api.Test; + +/** + * Tests for VideoObjectProxy. + */ +public class VideoObjectProxyTest { + @Test + void shouldBeInstanceOfExpensiveObject() { + MatcherAssert.assertThat(new VideoObjectProxy(), instanceOf(ExpensiveObject.class)); + } + + @Test + void constructorDoesNotThrowException() { + assertDoesNotThrow(VideoObjectProxy::new, "Constructor should not throw any exception"); + } + + @Test + void processDoesNotThrowException() { + assertDoesNotThrow(() -> new VideoObjectProxy().process(), "Process method should not throw any exception"); + } + +} \ No newline at end of file