chore: organize homework into first-part directory
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* This file was generated by the Gradle 'init' task.
|
||||
*
|
||||
* This generated file contains a sample Java application project to get you started.
|
||||
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/9.6.1/userguide/building_java_projects.html in the Gradle documentation.
|
||||
* This project uses @Incubating APIs which are subject to change.
|
||||
*/
|
||||
|
||||
plugins {
|
||||
// Apply the application plugin to add support for building a CLI application in Java.
|
||||
application
|
||||
}
|
||||
|
||||
repositories {
|
||||
// Use Maven Central for resolving dependencies.
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
// This dependency is used by the application.
|
||||
implementation(libs.guava)
|
||||
}
|
||||
|
||||
testing {
|
||||
suites {
|
||||
// Configure the built-in test suite
|
||||
val test = named<JvmTestSuite>("test") {
|
||||
// Use JUnit Jupiter test framework
|
||||
useJUnitJupiter("6.0.1")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Apply a specific Java toolchain to ease working on different environments.
|
||||
java {
|
||||
toolchain {
|
||||
languageVersion = JavaLanguageVersion.of(21)
|
||||
}
|
||||
}
|
||||
|
||||
application {
|
||||
// Define the main class for the application.
|
||||
mainClass = "org.example.App"
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* This source file was generated by the Gradle 'init' task
|
||||
*/
|
||||
package org.example;
|
||||
|
||||
import org.example.adapter.*;
|
||||
import org.example.builder.*;
|
||||
import org.example.chain.*;
|
||||
import org.example.decorator.*;
|
||||
import org.example.proxy.*;
|
||||
import org.example.strategy.*;
|
||||
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
run();
|
||||
}
|
||||
|
||||
private static void run() {
|
||||
checkAdapterPattern();
|
||||
|
||||
checkBuilderPattern();
|
||||
|
||||
checkChainPattern();
|
||||
|
||||
checkDecoratorPattern();
|
||||
|
||||
checkProxyPattern();
|
||||
|
||||
checkStrategyPattern();
|
||||
}
|
||||
|
||||
private static void checkAdapterPattern() {
|
||||
Adaptee adaptee = new Adaptee();
|
||||
Target target = new Adapter(adaptee);
|
||||
|
||||
target.methodWithoutArguments();
|
||||
}
|
||||
|
||||
private static void checkBuilderPattern() {
|
||||
Product product =
|
||||
new ProductBuilder().setFirst("first").setSecond("second").setThird(3).build();
|
||||
|
||||
System.out.println(product);
|
||||
}
|
||||
|
||||
private static void checkChainPattern() {
|
||||
Handler handler = new ConcreteHandlerA();
|
||||
handler.setNext(new ConcreteHandlerB());
|
||||
|
||||
handler.handle(42);
|
||||
handler.handle("Hello");
|
||||
}
|
||||
|
||||
private static void checkDecoratorPattern() {
|
||||
Component component = new ConcreteComponent();
|
||||
Component decorator = new ConcreteDecorator(component);
|
||||
|
||||
decorator.operation();
|
||||
}
|
||||
|
||||
private static void checkProxyPattern() {
|
||||
Service service = new ConcreteService();
|
||||
Service proxy = new ServiceProxy(service);
|
||||
|
||||
proxy.execute();
|
||||
}
|
||||
|
||||
private static void checkStrategyPattern() {
|
||||
Context context = new Context(new ConcreteStrategyA());
|
||||
|
||||
boolean crayfishWhistled = true; // Если рак на горе свистнул
|
||||
|
||||
if (crayfishWhistled) {
|
||||
context.setStrategy(new ConcreteStrategyB());
|
||||
}
|
||||
|
||||
context.executeStrategy();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.example.builder;
|
||||
|
||||
/** Product */
|
||||
public class Product {
|
||||
|
||||
public Product(String first, String second, int third) {
|
||||
this.first = first;
|
||||
this.second = second;
|
||||
this.third = third;
|
||||
}
|
||||
|
||||
public String getFirst() {
|
||||
return first;
|
||||
}
|
||||
|
||||
public String getSecond() {
|
||||
return second;
|
||||
}
|
||||
|
||||
public int getThird() {
|
||||
return third;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Product{"
|
||||
+ "first='"
|
||||
+ first
|
||||
+ '\''
|
||||
+ ", second='"
|
||||
+ second
|
||||
+ '\''
|
||||
+ ", third="
|
||||
+ third
|
||||
+ '}';
|
||||
}
|
||||
|
||||
private final String first;
|
||||
private final String second;
|
||||
private final int third;
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package org.example.builder;
|
||||
|
||||
/** Builder */
|
||||
public class ProductBuilder {
|
||||
|
||||
public ProductBuilder setFirst(String first) {
|
||||
this.first = first;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ProductBuilder setSecond(String second) {
|
||||
this.second = second;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ProductBuilder setThird(int third) {
|
||||
this.third = third;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Product build() {
|
||||
return new Product(first, second, third);
|
||||
}
|
||||
|
||||
private String first;
|
||||
private String second;
|
||||
private int third;
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package org.example.chain;
|
||||
|
||||
/** Abstract Handler */
|
||||
public abstract class AbstractHandler implements Handler {
|
||||
|
||||
@Override
|
||||
public void setNext(Handler handler) {
|
||||
this.next = handler;
|
||||
}
|
||||
|
||||
protected void handleNext(Object request) {
|
||||
if (next != null) {
|
||||
next.handle(request);
|
||||
}
|
||||
}
|
||||
|
||||
private Handler next;
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package org.example.chain;
|
||||
|
||||
/** Concrete Handler A */
|
||||
public class ConcreteHandlerA extends AbstractHandler {
|
||||
|
||||
@Override
|
||||
public void handle(Object request) {
|
||||
if (request instanceof Integer) {
|
||||
System.out.println("ConcreteHandlerA обработал int: " + request);
|
||||
} else {
|
||||
handleNext(request);
|
||||
}
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package org.example.chain;
|
||||
|
||||
/** Concrete Handler B */
|
||||
public class ConcreteHandlerB extends AbstractHandler {
|
||||
|
||||
@Override
|
||||
public void handle(Object request) {
|
||||
if (request instanceof String) {
|
||||
System.out.println("ConcreteHandlerB обработал String: " + request);
|
||||
} else {
|
||||
handleNext(request);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package org.example.chain;
|
||||
|
||||
/** Handler */
|
||||
public interface Handler {
|
||||
void setNext(Handler handler);
|
||||
|
||||
void handle(Object request);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package org.example.decorator;
|
||||
|
||||
/** Component */
|
||||
public interface Component {
|
||||
void operation();
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package org.example.decorator;
|
||||
|
||||
/** Concrete Component */
|
||||
public class ConcreteComponent implements Component {
|
||||
|
||||
@Override
|
||||
public void operation() {
|
||||
System.out.println("ConcreteComponent выполняет операцию");
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package org.example.decorator;
|
||||
|
||||
/** Concrete Decorator */
|
||||
public class ConcreteDecorator extends Decorator {
|
||||
|
||||
public ConcreteDecorator(Component component) {
|
||||
super(component);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void operation() {
|
||||
component.operation();
|
||||
System.out.println("ConcreteDecorator добавляет поведение");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.example.decorator;
|
||||
|
||||
/** Decorator */
|
||||
public abstract class Decorator implements Component {
|
||||
|
||||
protected Decorator(Component component) {
|
||||
this.component = component;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void operation() {
|
||||
component.operation();
|
||||
}
|
||||
|
||||
protected final Component component;
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package org.example.proxy;
|
||||
|
||||
/** Concrete Service */
|
||||
public class ConcreteService implements Service {
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
System.out.println("ConcreteService выполняет операцию");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package org.example.proxy;
|
||||
|
||||
/** Service */
|
||||
public interface Service {
|
||||
void execute();
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.example.proxy;
|
||||
|
||||
/** Service Proxy */
|
||||
public class ServiceProxy implements Service {
|
||||
|
||||
public ServiceProxy(Service service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
System.out.println("ServiceProxy разрешает выполнение операции");
|
||||
service.execute();
|
||||
}
|
||||
|
||||
private final Service service;
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package org.example.strategy;
|
||||
|
||||
/** Concrete Strategy A */
|
||||
public class ConcreteStrategyA implements Strategy {
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
System.out.println("ConcreteStrategyA выполняет операцию");
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package org.example.strategy;
|
||||
|
||||
/** Concrete Strategy B */
|
||||
public class ConcreteStrategyB implements Strategy {
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
System.out.println("ConcreteStrategyB выполняет операцию");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.example.strategy;
|
||||
|
||||
/** Context */
|
||||
public class Context {
|
||||
|
||||
public Context(Strategy strategy) {
|
||||
this.strategy = strategy;
|
||||
}
|
||||
|
||||
public void setStrategy(Strategy strategy) {
|
||||
this.strategy = strategy;
|
||||
}
|
||||
|
||||
public void executeStrategy() {
|
||||
strategy.execute();
|
||||
}
|
||||
|
||||
private Strategy strategy;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package org.example.strategy;
|
||||
|
||||
/** Strategy */
|
||||
public interface Strategy {
|
||||
void execute();
|
||||
}
|
||||
Reference in New Issue
Block a user