feat(hw-03): implement Adapter pattern
This commit is contained in:
@@ -3,12 +3,21 @@
|
|||||||
*/
|
*/
|
||||||
package org.example;
|
package org.example;
|
||||||
|
|
||||||
public class App {
|
import org.example.adapter.*;
|
||||||
public String getGreeting() {
|
|
||||||
return "Hello World!";
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public class App {
|
||||||
System.out.println(new App().getGreeting());
|
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