[JAVA-14174] Renamed paterns to paterns-module (#12718)
* [JAVA-14174] Renamed paterns to paterns-module * [JAVA-14174] naming fixes Co-authored-by: panagiotiskakos <[email protected]>
This commit is contained in:
committed by
GitHub
co-authored by
panagiotiskakos
parent
74dcaa0935
commit
63a9bfbad9
@@ -0,0 +1,11 @@
|
||||
### Relevant Articles:
|
||||
- [The Observer Pattern in Java](https://www.baeldung.com/java-observer-pattern)
|
||||
- [Visitor Design Pattern in Java](https://www.baeldung.com/java-visitor-pattern)
|
||||
- [Interpreter Design Pattern in Java](https://www.baeldung.com/java-interpreter-pattern)
|
||||
- [State Design Pattern in Java](https://www.baeldung.com/java-state-design-pattern)
|
||||
- [Implementing the Template Method Pattern in Java](https://www.baeldung.com/java-template-method-pattern)
|
||||
- [Chain of Responsibility Design Pattern in Java](https://www.baeldung.com/chain-of-responsibility-pattern)
|
||||
- [The Command Pattern in Java](https://www.baeldung.com/java-command-pattern)
|
||||
- [The Mediator Pattern in Java](https://www.baeldung.com/java-mediator-pattern)
|
||||
- [Introduction to the Null Object Pattern](https://www.baeldung.com/java-null-object-pattern)
|
||||
- [Avoid Check for Null Statement in Java](https://www.baeldung.com/java-avoid-null-check)
|
||||
@@ -0,0 +1,46 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>design-patterns-behavioral</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>design-patterns-behavioral</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>patterns-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains</groupId>
|
||||
<artifactId>annotations</artifactId>
|
||||
<version>${intellij.annotations.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.findbugs</groupId>
|
||||
<artifactId>annotations</artifactId>
|
||||
<version>${findbugs.annotations.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<intellij.annotations.version>16.0.2</intellij.annotations.version>
|
||||
<findbugs.annotations.version>3.0.1</findbugs.annotations.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.chainofresponsibility;
|
||||
|
||||
public abstract class AuthenticationProcessor {
|
||||
|
||||
// next element in chain or responsibility
|
||||
public AuthenticationProcessor nextProcessor;
|
||||
|
||||
public AuthenticationProcessor(AuthenticationProcessor nextProcessor) {
|
||||
this.nextProcessor = nextProcessor;
|
||||
}
|
||||
|
||||
public abstract boolean isAuthorized(AuthenticationProvider authProvider);
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.chainofresponsibility;
|
||||
|
||||
public interface AuthenticationProvider {
|
||||
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.chainofresponsibility;
|
||||
|
||||
public class OAuthAuthenticationProcessor extends AuthenticationProcessor {
|
||||
|
||||
public OAuthAuthenticationProcessor(AuthenticationProcessor nextProcessor) {
|
||||
super(nextProcessor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAuthorized(AuthenticationProvider authProvider) {
|
||||
|
||||
if (authProvider instanceof OAuthTokenProvider) {
|
||||
return Boolean.TRUE;
|
||||
} else if (nextProcessor != null) {
|
||||
return nextProcessor.isAuthorized(authProvider);
|
||||
} else {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.chainofresponsibility;
|
||||
|
||||
public class OAuthTokenProvider implements AuthenticationProvider {
|
||||
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.chainofresponsibility;
|
||||
|
||||
public class SamlAuthenticationProvider implements AuthenticationProvider {
|
||||
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.chainofresponsibility;
|
||||
|
||||
public class UsernamePasswordAuthenticationProcessor extends AuthenticationProcessor {
|
||||
|
||||
public UsernamePasswordAuthenticationProcessor(AuthenticationProcessor nextProcessor) {
|
||||
super(nextProcessor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAuthorized(AuthenticationProvider authProvider) {
|
||||
if (authProvider instanceof UsernamePasswordProvider) {
|
||||
return Boolean.TRUE;
|
||||
} else if (nextProcessor != null) {
|
||||
return nextProcessor.isAuthorized(authProvider);
|
||||
} else {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.chainofresponsibility;
|
||||
|
||||
public class UsernamePasswordProvider implements AuthenticationProvider {
|
||||
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.command.client;
|
||||
|
||||
import com.baeldung.command.command.OpenTextFileOperation;
|
||||
import com.baeldung.command.command.SaveTextFileOperation;
|
||||
import com.baeldung.command.command.TextFileOperation;
|
||||
import com.baeldung.command.invoker.TextFileOperationExecutor;
|
||||
import com.baeldung.command.receiver.TextFile;
|
||||
|
||||
public class TextFileApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
TextFileOperation openTextFileOperation = new OpenTextFileOperation(new TextFile("file1.txt"));
|
||||
TextFileOperation saveTextFileOperation = new SaveTextFileOperation(new TextFile("file2.txt"));
|
||||
TextFileOperationExecutor textFileOperationExecutor = new TextFileOperationExecutor();
|
||||
System.out.println(textFileOperationExecutor.executeOperation(openTextFileOperation));
|
||||
System.out.println(textFileOperationExecutor.executeOperation(saveTextFileOperation));
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.command.command;
|
||||
|
||||
import com.baeldung.command.receiver.TextFile;
|
||||
|
||||
public class OpenTextFileOperation implements TextFileOperation {
|
||||
|
||||
private final TextFile textFile;
|
||||
|
||||
public OpenTextFileOperation(TextFile textFile) {
|
||||
this.textFile = textFile;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String execute() {
|
||||
return textFile.open();
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.command.command;
|
||||
|
||||
import com.baeldung.command.receiver.TextFile;
|
||||
|
||||
public class SaveTextFileOperation implements TextFileOperation {
|
||||
|
||||
private final TextFile textFile;
|
||||
|
||||
public SaveTextFileOperation(TextFile textFile) {
|
||||
this.textFile = textFile;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String execute() {
|
||||
return textFile.save();
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.command.command;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface TextFileOperation {
|
||||
|
||||
String execute();
|
||||
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.command.invoker;
|
||||
|
||||
import com.baeldung.command.command.TextFileOperation;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TextFileOperationExecutor {
|
||||
|
||||
private final List<TextFileOperation> textFileOperations = new ArrayList<>();
|
||||
|
||||
public String executeOperation(TextFileOperation textFileOperation) {
|
||||
textFileOperations.add(textFileOperation);
|
||||
return textFileOperation.execute();
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.command.receiver;
|
||||
|
||||
public class TextFile {
|
||||
|
||||
private final String name;
|
||||
|
||||
public TextFile(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String open() {
|
||||
return "Opening file " + name;
|
||||
}
|
||||
|
||||
public String read() {
|
||||
return "Reading file " + name;
|
||||
}
|
||||
|
||||
public String write() {
|
||||
return "Writing to file " + name;
|
||||
}
|
||||
|
||||
public String save() {
|
||||
return "Saving file " + name;
|
||||
}
|
||||
|
||||
public String copy() {
|
||||
return "Copying file " + name;
|
||||
}
|
||||
|
||||
public String paste() {
|
||||
return "Pasting file " + name;
|
||||
}
|
||||
}
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
package com.baeldung.interpreter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
class Context {
|
||||
|
||||
private static Map<String, List<Row>> tables = new HashMap<>();
|
||||
|
||||
static {
|
||||
List<Row> list = new ArrayList<>();
|
||||
list.add(new Row("John", "Doe"));
|
||||
list.add(new Row("Jan", "Kowalski"));
|
||||
list.add(new Row("Dominic", "Doom"));
|
||||
|
||||
tables.put("people", list);
|
||||
}
|
||||
|
||||
private String table;
|
||||
private String column;
|
||||
|
||||
/**
|
||||
* Index of column to be shown in result.
|
||||
* Calculated in {@link #setColumnMapper()}
|
||||
*/
|
||||
private int colIndex = -1;
|
||||
|
||||
/**
|
||||
* Default setup, used for clearing the context for next queries.
|
||||
* See {@link Context#clear()}
|
||||
*/
|
||||
private static final Predicate<String> matchAnyString = s -> s.length() > 0;
|
||||
private static final Function<String, Stream<? extends String>> matchAllColumns = Stream::of;
|
||||
/**
|
||||
* Varies based on setup in subclasses of {@link Expression}
|
||||
*/
|
||||
private Predicate<String> whereFilter = matchAnyString;
|
||||
private Function<String, Stream<? extends String>> columnMapper = matchAllColumns;
|
||||
|
||||
void setColumn(String column) {
|
||||
this.column = column;
|
||||
setColumnMapper();
|
||||
}
|
||||
|
||||
void setTable(String table) {
|
||||
this.table = table;
|
||||
}
|
||||
|
||||
void setFilter(Predicate<String> filter) {
|
||||
whereFilter = filter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the context to defaults.
|
||||
* No filters, match all columns.
|
||||
*/
|
||||
void clear() {
|
||||
column = "";
|
||||
columnMapper = matchAllColumns;
|
||||
whereFilter = matchAnyString;
|
||||
}
|
||||
|
||||
List<String> search() {
|
||||
|
||||
List<String> result = tables.entrySet()
|
||||
.stream()
|
||||
.filter(entry -> entry.getKey().equalsIgnoreCase(table))
|
||||
.flatMap(entry -> Stream.of(entry.getValue()))
|
||||
.flatMap(Collection::stream)
|
||||
.map(Row::toString)
|
||||
.flatMap(columnMapper)
|
||||
.filter(whereFilter)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
clear();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets column mapper based on {@link #column} attribute.
|
||||
* Note: If column is unknown, will remain to look for all columns.
|
||||
*/
|
||||
private void setColumnMapper() {
|
||||
switch (column) {
|
||||
case "*":
|
||||
colIndex = -1;
|
||||
break;
|
||||
case "name":
|
||||
colIndex = 0;
|
||||
break;
|
||||
case "surname":
|
||||
colIndex = 1;
|
||||
break;
|
||||
}
|
||||
if (colIndex != -1) {
|
||||
columnMapper = s -> Stream.of(s.split(" ")[colIndex]);
|
||||
}
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.interpreter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
interface Expression {
|
||||
List<String> interpret(Context ctx);
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.interpreter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
class From implements Expression {
|
||||
|
||||
private String table;
|
||||
private Where where;
|
||||
|
||||
From(String table) {
|
||||
this.table = table;
|
||||
}
|
||||
|
||||
From(String table, Where where) {
|
||||
this.table = table;
|
||||
this.where = where;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> interpret(Context ctx) {
|
||||
ctx.setTable(table);
|
||||
if (where == null) {
|
||||
return ctx.search();
|
||||
}
|
||||
return where.interpret(ctx);
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.interpreter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class InterpreterDemo {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Expression query = new Select("name", new From("people"));
|
||||
Context ctx = new Context();
|
||||
List<String> result = query.interpret(ctx);
|
||||
System.out.println(result);
|
||||
|
||||
Expression query2 = new Select("*", new From("people"));
|
||||
List<String> result2 = query2.interpret(ctx);
|
||||
System.out.println(result2);
|
||||
|
||||
Expression query3 = new Select("name", new From("people", new Where(name -> name.toLowerCase().startsWith("d"))));
|
||||
List<String> result3 = query3.interpret(ctx);
|
||||
System.out.println(result3);
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.interpreter;
|
||||
|
||||
class Row {
|
||||
|
||||
private String name;
|
||||
private String surname;
|
||||
|
||||
Row(String name, String surname) {
|
||||
this.name = name;
|
||||
this.surname = surname;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name + " " + surname;
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.interpreter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
class Select implements Expression {
|
||||
|
||||
private String column;
|
||||
private From from;
|
||||
|
||||
Select(String column, From from) {
|
||||
this.column = column;
|
||||
this.from = from;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> interpret(Context ctx) {
|
||||
ctx.setColumn(column);
|
||||
return from.interpret(ctx);
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.interpreter;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
class Where implements Expression {
|
||||
|
||||
private Predicate<String> filter;
|
||||
|
||||
Where(Predicate<String> filter) {
|
||||
this.filter = filter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> interpret(Context ctx) {
|
||||
ctx.setFilter(filter);
|
||||
return ctx.search();
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.mediator;
|
||||
|
||||
public class Button {
|
||||
private Mediator mediator;
|
||||
|
||||
public void setMediator(Mediator mediator) {
|
||||
this.mediator = mediator;
|
||||
}
|
||||
|
||||
public void press() {
|
||||
this.mediator.press();
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.mediator;
|
||||
|
||||
public class Fan {
|
||||
private Mediator mediator;
|
||||
private boolean isOn = false;
|
||||
|
||||
public void setMediator(Mediator mediator) {
|
||||
this.mediator = mediator;
|
||||
}
|
||||
|
||||
public boolean isOn() {
|
||||
return isOn;
|
||||
}
|
||||
|
||||
public void turnOn() {
|
||||
this.mediator.start();
|
||||
isOn = true;
|
||||
}
|
||||
|
||||
public void turnOff() {
|
||||
isOn = false;
|
||||
this.mediator.stop();
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.mediator;
|
||||
|
||||
public class Mediator {
|
||||
private Button button;
|
||||
private Fan fan;
|
||||
private PowerSupplier powerSupplier;
|
||||
|
||||
public void setButton(Button button) {
|
||||
this.button = button;
|
||||
this.button.setMediator(this);
|
||||
}
|
||||
|
||||
public void setFan(Fan fan) {
|
||||
this.fan = fan;
|
||||
this.fan.setMediator(this);
|
||||
}
|
||||
|
||||
public void setPowerSupplier(PowerSupplier powerSupplier) {
|
||||
this.powerSupplier = powerSupplier;
|
||||
}
|
||||
|
||||
public void press() {
|
||||
if (fan.isOn()) {
|
||||
fan.turnOff();
|
||||
} else {
|
||||
fan.turnOn();
|
||||
}
|
||||
}
|
||||
|
||||
public void start() {
|
||||
powerSupplier.turnOn();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
powerSupplier.turnOff();
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.mediator;
|
||||
|
||||
public class PowerSupplier {
|
||||
public void turnOn() {
|
||||
// implementation
|
||||
}
|
||||
|
||||
public void turnOff() {
|
||||
// implementation
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.nullobject;
|
||||
|
||||
public class JmsRouter implements Router {
|
||||
|
||||
@Override
|
||||
public void route(Message msg) {
|
||||
System.out.println("Routing to a JMS queue. Msg: " + msg);
|
||||
}
|
||||
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.nullobject;
|
||||
|
||||
public class Message {
|
||||
|
||||
private String body;
|
||||
|
||||
private String priority;
|
||||
|
||||
public Message(String body, String priority) {
|
||||
this.body = body;
|
||||
this.priority = priority;
|
||||
}
|
||||
|
||||
public String getPriority() {
|
||||
return priority;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{body='" + body + '\'' +
|
||||
", priority='" + priority + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.nullobject;
|
||||
|
||||
public class NullRouter implements Router {
|
||||
|
||||
@Override
|
||||
public void route(Message msg) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.nullobject;
|
||||
|
||||
public interface Router {
|
||||
|
||||
void route(Message msg);
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user