Skip to content

Instantly share code, notes, and snippets.

View josefigueredo's full-sized avatar
🚀

Jose Figueredo josefigueredo

🚀
View GitHub Profile
package com.josefigueredo.patterns;
import java.util.function.UnaryOperator;
public class StrategyPatternRunner {
public static void main(String[] args) {
String originalText = "This is a test from December 8th, 2020";
String expectedText = "ThisisatestfromDecember8th,2020";
package com.josefigueredo.patterns;
public class StrategyPatternRunner {
public static void main(String[] args) {
String originalText = "This is a test from December 8th, 2020";
String expectedText = "ThisisatestfromDecember8th,2020";
RemoveStrategy removeStrategy = s -> s.replace(" ", "");
noWhitespaces = Remover.remove(originalText, removeStrategy);
package com.josefigueredo.patterns;
import com.josefigueredo.patterns.strategies.*;
public class StrategyPatternRunner {
public static void main(String[] args) {
String originalText = "This is a test from December 8th, 2020";
String expectedText = "ThisisatestfromDecember8th,2020";
package com.josefigueredo.patterns.strategies;
import com.josefigueredo.patterns.RemoveStrategy;
import java.util.stream.Collectors;
public class WhitespacesRemoverFunctionalByStdLib implements RemoveStrategy {
@Override
public String execute(String s) {
return s.chars()
.filter(c -> !Character.isWhitespace(c))
package com.josefigueredo.patterns.strategies;
import com.josefigueredo.patterns.RemoveStrategy;
public class WhitespacesRemoverImperative implements RemoveStrategy {
@Override
public String execute(String s) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (!Character.isWhitespace(s.charAt(i))) {
package com.josefigueredo.patterns;
import java.util.Objects;
public final class Remover {
private Remover() {
throw new AssertionError("Remover cannot be instantiated");
}
package com.josefigueredo.patterns;
@FunctionalInterface
public interface RemoveStrategy {
String execute(String s);
}
SRP The Single Responsibility Principle A class should have one and only one reason to change
OCP The Open Closed Principle You should be able to extend a classes behavior without modifying it
LSP The Liskov Substitution Principle Derived classes must be substitutable for their base classes
ISP The Interface Segregation Principle Make fine grained interfaces that are client specific
DIP The Dependency Inversion Principle Depend on abstractions not on concretions
curl -H "Accept: text/event-stream" \
http://localhost:8080/stream
curl -X POST \
-H "Content-type: application/json" \
-d '{"text": "A simple tweet"}' \
http://localhost:8080/tweet