feat(hw-03): implement Adapter pattern

This commit is contained in:
user
2026-08-08 18:57:55 +04:00
parent 2a5d70a1b2
commit 9c20913fed
4 changed files with 53 additions and 7 deletions
@@ -3,12 +3,21 @@
*/
package org.example;
public class App {
public String getGreeting() {
return "Hello World!";
}
import org.example.adapter.*;
public static void main(String[] args) {
System.out.println(new App().getGreeting());
}
public class App {
public static void main(String[] args) {
run();
}
private static void run() {
checkAdapterPattern();
}
private static void checkAdapterPattern() {
Adaptee adaptee = new Adaptee();
Target target = new Adapter(adaptee);
target.methodWithoutArguments();
}
}
@@ -0,0 +1,9 @@
package org.example.adapter;
/** Adaptee */
public class Adaptee {
public void methodWithArgument(int argument) {
System.out.println("Был передан аргумент " + argument);
}
}
@@ -0,0 +1,21 @@
package org.example.adapter;
import java.util.Random;
/** Adapter */
public class Adapter implements Target {
public Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
this.random = new Random();
}
@Override
public void methodWithoutArguments() {
int argument = random.nextInt();
adaptee.methodWithArgument(argument);
}
private final Adaptee adaptee;
private Random random;
}
@@ -0,0 +1,7 @@
package org.example.adapter;
/** Target */
public interface Target {
void methodWithoutArguments();
}