feat(hw-03): implement Adapter pattern
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user