[JAVA-14174] Renamed paterns to paterns-module (#12718)

* [JAVA-14174] Renamed paterns to paterns-module

* [JAVA-14174] naming fixes

Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
panos-kakos
2022-09-19 07:44:14 +01:00
committed by GitHub
parent 74dcaa0935
commit 63a9bfbad9
518 changed files with 32 additions and 32 deletions
@@ -0,0 +1,4 @@
package com.baeldung.d;
public interface Keyboard {
}
@@ -0,0 +1,6 @@
package com.baeldung.d;
public class Monitor {
}
@@ -0,0 +1,5 @@
package com.baeldung.d;
public class StandardKeyboard implements Keyboard {
}
@@ -0,0 +1,15 @@
package com.baeldung.d;
public class Windows98Machine {
private final StandardKeyboard keyboard;
private final Monitor monitor;
public Windows98Machine() {
monitor = new Monitor();
keyboard = new StandardKeyboard();
}
}
@@ -0,0 +1,12 @@
package com.baeldung.d;
public class Windows98MachineDI {
private final Keyboard keyboard;
private final Monitor monitor;
public Windows98MachineDI(Keyboard keyboard, Monitor monitor) {
this.keyboard = keyboard;
this.monitor = monitor;
}
}
@@ -0,0 +1,12 @@
package com.baeldung.i;
public class BearCarer implements BearCleaner, BearFeeder {
public void washTheBear() {
//I think we missed a spot..
}
public void feedTheBear() {
//Tuna tuesdays..
}
}
@@ -0,0 +1,5 @@
package com.baeldung.i;
public interface BearCleaner {
void washTheBear();
}
@@ -0,0 +1,5 @@
package com.baeldung.i;
public interface BearFeeder {
void feedTheBear();
}
@@ -0,0 +1,9 @@
package com.baeldung.i;
public interface BearKeeper {
void washTheBear();
void feedTheBear();
void petTheBear();
}
@@ -0,0 +1,5 @@
package com.baeldung.i;
public interface BearPetter {
void petTheBear();
}
@@ -0,0 +1,8 @@
package com.baeldung.i;
public class CrazyPerson implements BearPetter {
public void petTheBear() {
//Good luck with that!
}
}
@@ -0,0 +1,5 @@
package com.baeldung.i.fixed;
public interface Bank extends Payment {
void initiatePayments();
}
@@ -0,0 +1,21 @@
package com.baeldung.i.fixed;
import java.util.List;
public class BankPayment implements Bank {
@Override
public void initiatePayments() {
}
@Override
public Object status() {
return null;
}
@Override
public List<Object> getPayments() {
return null;
}
}
@@ -0,0 +1,6 @@
package com.baeldung.i.fixed;
public interface Loan extends Payment {
void intiateLoanSettlement();
void initiateRePayment();
}
@@ -0,0 +1,26 @@
package com.baeldung.i.fixed;
import java.util.List;
public class LoanPayment implements Loan {
@Override
public void intiateLoanSettlement() {
}
@Override
public void initiateRePayment() {
}
@Override
public Object status() {
return null;
}
@Override
public List<Object> getPayments() {
return null;
}
}
@@ -0,0 +1,8 @@
package com.baeldung.i.fixed;
import java.util.List;
public interface Payment {
Object status();
List<Object> getPayments();
}
@@ -0,0 +1,32 @@
package com.baeldung.i.polluted;
import java.util.Collections;
import java.util.List;
public class BankPayment implements Payment {
@Override
public void initiatePayments() {
}
@Override
public Object status() {
return null;
}
@Override
public List<Object> getPayments() {
return Collections.emptyList();
}
@Override
public void intiateLoanSettlement() {
throw new UnsupportedOperationException("This is not a loan payment");
}
@Override
public void initiateRePayment() {
throw new UnsupportedOperationException("This is not a loan payment");
}
}
@@ -0,0 +1,31 @@
package com.baeldung.i.polluted;
import java.util.List;
public class LoanPayment implements Payment {
@Override
public void initiatePayments() {
}
@Override
public Object status() {
return null;
}
@Override
public List<Object> getPayments() {
return null;
}
@Override
public void intiateLoanSettlement() {
}
@Override
public void initiateRePayment() {
}
}
@@ -0,0 +1,13 @@
package com.baeldung.i.polluted;
import java.util.List;
public interface Payment {
void initiatePayments();
Object status();
List<Object> getPayments();
//Loan related methods
void intiateLoanSettlement();
void initiateRePayment();
}
@@ -0,0 +1,8 @@
package com.baeldung.l;
public interface Car {
void turnOnEngine();
void accelerate();
}
@@ -0,0 +1,12 @@
package com.baeldung.l;
public class ElectricCar implements Car {
public void turnOnEngine() {
throw new AssertionError("I don't have an engine!");
}
public void accelerate() {
//this acceleration is crazy!
}
}
@@ -0,0 +1,13 @@
package com.baeldung.l;
public class Engine {
public void on(){
//vroom.
}
public void powerOn(int amount){
//do something
}
}
@@ -0,0 +1,18 @@
package com.baeldung.l;
public class MotorCar implements Car {
private Engine engine;
//Constructors, getters + setters
public void turnOnEngine() {
//turn on the engine!
engine.on();
}
public void accelerate() {
//move forward!
engine.powerOn(1000);
}
}
@@ -0,0 +1,17 @@
package com.baeldung.l.advanced;
import java.math.BigDecimal;
public abstract class Account {
protected abstract void deposit(BigDecimal amount);
/**
* Reduces the account balance by the specified amount
* provided given amount > 0 and account meets minimum available
* balance criteria.
*
* @param amount
*/
protected abstract void withdraw(BigDecimal amount);
}
@@ -0,0 +1,15 @@
package com.baeldung.l.advanced;
import java.math.BigDecimal;
public class BankingAppWithdrawalService {
private Account account;
public BankingAppWithdrawalService(Account account) {
this.account = account;
}
public void withdraw(BigDecimal amount) {
account.withdraw(amount);
}
}
@@ -0,0 +1,27 @@
package com.baeldung.l.advanced;
public class Bar extends Foo {
@Override
// precondition: 0 < num <= 10
public void doStuff(int num) {
if (num <= 0 || num > 10) {
throw new IllegalArgumentException("Input out of range 1-10");
}
// some logic here...
}
@Override
// precondition: 0 < num <= 3
public void doOtherStuff(int num) {
if (num <= 0 || num > 3) {
throw new IllegalArgumentException("Input out of range 1-3");
}
// some logic here...
}
@Override
public Integer generateNumber() {
return new Integer(10);
}
}
@@ -0,0 +1,26 @@
package com.baeldung.l.advanced;
public abstract class Car {
protected int limit;
// invariant: speed < limit;
protected int speed;
// Allowed to be set once at the time of creation.
// Value can only increment thereafter.
// Value cannot be reset.
protected int mileage;
public Car(int mileage) {
this.mileage = mileage;
}
protected abstract void turnOnEngine();
// postcondition: speed < limit
protected abstract void accelerate();
// postcondition: speed must reduce
protected abstract void brake();
}
@@ -0,0 +1,15 @@
package com.baeldung.l.advanced;
import java.math.BigDecimal;
public class CurrentAccount extends Account {
@Override
protected void deposit(BigDecimal amount) {
// Deposit into CurrentAccount
}
@Override
protected void withdraw(BigDecimal amount) {
// Withdraw from CurrentAccount
}
}
@@ -0,0 +1,23 @@
package com.baeldung.l.advanced;
public class ElectricCar extends Car {
public ElectricCar(int mileage) {
super(mileage);
}
@Override
protected void turnOnEngine() {
throw new AssertionError("I am an Electric Car. I don't have an engine!");
}
@Override
protected void accelerate() {
// this acceleration is crazy!
}
@Override
protected void brake() {
// Apply ElectricCar brake
}
}
@@ -0,0 +1,18 @@
package com.baeldung.l.advanced;
import java.io.IOException;
public class FilePurgingJob {
private FileSystem fileSystem;
public FilePurgingJob(FileSystem fileSystem) {
this.fileSystem = fileSystem;
}
public void purgeOldestFile(String path) throws IOException {
if (!(fileSystem instanceof ReadOnlyFileSystem)) {
// code to detect oldest file
fileSystem.deleteFile(path);
}
}
}
@@ -0,0 +1,10 @@
package com.baeldung.l.advanced;
import java.io.File;
import java.io.IOException;
public interface FileSystem {
File[] listFiles(String path);
void deleteFile(String path) throws IOException;
}
@@ -0,0 +1,15 @@
package com.baeldung.l.advanced;
import java.math.BigDecimal;
public class FixedTermDepositAccount extends Account {
@Override
protected void deposit(BigDecimal amount) {
// Deposit into this account
}
@Override
protected void withdraw(BigDecimal amount) {
throw new UnsupportedOperationException("Withdrawals are not supported by FixedTermDepositAccount!!");
}
}
@@ -0,0 +1,22 @@
package com.baeldung.l.advanced;
public abstract class Foo {
// precondition: 0 < num <=5
public void doStuff(int num) {
if (num <= 0 || num > 5) {
throw new IllegalArgumentException("Input out of range 1-5");
}
// some logic here...
}
// precondition: 0 < num <=5
public void doOtherStuff(int num) {
if (num <= 0 || num > 5) {
throw new IllegalArgumentException("Input out of range 1-5");
}
// some logic here...
}
public abstract Number generateNumber();
}
@@ -0,0 +1,28 @@
package com.baeldung.l.advanced;
public class HybridCar extends Car {
// invariant: charge >= 0;
private int charge;
public HybridCar(int mileage) {
super(mileage);
}
@Override
protected void turnOnEngine() {
// Start HybridCar
}
@Override
// postcondition: speed < limit
protected void accelerate() {
// Accelerate HybridCar speed < limit
}
@Override
// postcondition: speed must reduce
// postcondition: charge must increase
protected void brake() {
// Apply HybridCar brake
}
}
@@ -0,0 +1,25 @@
package com.baeldung.l.advanced;
public class MotorCar extends Car {
public MotorCar(int mileage) {
super(mileage);
}
@Override
protected void turnOnEngine() {
// Start MotorCar
}
@Override
// postcondition: speed < limit
protected void accelerate() {
// Accelerate MotorCar
}
@Override
// postcondition: speed must reduce
protected void brake() {
// Apply MotorCar brake
}
}
@@ -0,0 +1,16 @@
package com.baeldung.l.advanced;
import java.io.File;
import java.io.IOException;
public class ReadOnlyFileSystem implements FileSystem {
public File[] listFiles(String path) {
// code to list files
return new File[0];
}
public void deleteFile(String path) throws IOException {
// Do nothing.
// deleteFile operation is not supported on a read-only file system
}
}
@@ -0,0 +1,15 @@
package com.baeldung.l.advanced;
import java.math.BigDecimal;
public class SavingsAccount extends Account {
@Override
protected void deposit(BigDecimal amount) {
// Deposit into SavingsAccount
}
@Override
protected void withdraw(BigDecimal amount) {
// Withdraw from SavingsAccount
}
}
@@ -0,0 +1,24 @@
package com.baeldung.l.advanced;
public class ToyCar extends Car {
public ToyCar(int mileage) {
super(mileage);
}
protected void turnOnEngine() {
}
protected void accelerate() {
}
protected void brake() {
}
public void reset() {
mileage = 0;
}
}
@@ -0,0 +1,7 @@
package com.baeldung.l.advanced.refactored;
import java.math.BigDecimal;
public abstract class Account {
protected abstract void deposit(BigDecimal amount);
}
@@ -0,0 +1,17 @@
package com.baeldung.l.advanced.refactored;
import com.baeldung.l.advanced.Account;
import java.math.BigDecimal;
public class BankingAppWithdrawalService {
private WithdrawableAccount withdrawableAccount;
public BankingAppWithdrawalService(WithdrawableAccount withdrawableAccount) {
this.withdrawableAccount = withdrawableAccount;
}
public void withdraw(BigDecimal amount) {
withdrawableAccount.withdraw(amount);
}
}
@@ -0,0 +1,13 @@
package com.baeldung.l.advanced.refactored;
import java.math.BigDecimal;
public class CurrentAccount extends WithdrawableAccount {
protected void deposit(BigDecimal amount) {
// Deposit into CurrentAccount
}
protected void withdraw(BigDecimal amount) {
// Withdraw from CurrentAccount
}
}
@@ -0,0 +1,9 @@
package com.baeldung.l.advanced.refactored;
import java.math.BigDecimal;
public class FixedTermDepositAccount extends Account {
protected void deposit(BigDecimal amount) {
// Deposit into this account
}
}
@@ -0,0 +1,13 @@
package com.baeldung.l.advanced.refactored;
import java.math.BigDecimal;
public class SavingsAccount extends WithdrawableAccount {
protected void deposit(BigDecimal amount) {
// Deposit into SavingsAccount
}
protected void withdraw(BigDecimal amount) {
// Withdraw from SavingsAccount
}
}
@@ -0,0 +1,14 @@
package com.baeldung.l.advanced.refactored;
import java.math.BigDecimal;
public abstract class WithdrawableAccount extends Account {
/**
* Reduces the account balance by the specified amount
* provided given amount > 0 and account meets minimum available
* balance criteria.
*
* @param amount
*/
protected abstract void withdraw(BigDecimal amount);
}
@@ -0,0 +1,7 @@
package com.baeldung.o;
public abstract class AbstractCalculatorOperation {
abstract void perform();
}
@@ -0,0 +1,41 @@
package com.baeldung.o;
public class Addition implements CalculatorOperation {
private double left;
private double right;
private double result = 0.0;
public Addition(double left, double right) {
this.left = left;
this.right = right;
}
public double getLeft() {
return left;
}
public void setLeft(double left) {
this.left = left;
}
public double getRight() {
return right;
}
public void setRight(double right) {
this.right = right;
}
public double getResult() {
return result;
}
public void setResult(double result) {
this.result = result;
}
@Override
public void perform() {
result = left + right;
}
}
@@ -0,0 +1,14 @@
package com.baeldung.o;
import java.security.InvalidParameterException;
public class Calculator {
public void calculate(CalculatorOperation operation) {
if (operation == null) {
throw new InvalidParameterException("Can not perform operation");
}
operation.perform();
}
}
@@ -0,0 +1,7 @@
package com.baeldung.o;
public interface CalculatorOperation {
void perform();
}
@@ -0,0 +1,43 @@
package com.baeldung.o;
public class Division implements CalculatorOperation {
private double left;
private double right;
private double result = 0.0;
public Division(double left, double right) {
this.left = left;
this.right = right;
}
public double getLeft() {
return left;
}
public void setLeft(double left) {
this.left = left;
}
public double getRight() {
return right;
}
public void setRight(double right) {
this.right = right;
}
public double getResult() {
return result;
}
public void setResult(double result) {
this.result = result;
}
@Override
public void perform() {
if (right != 0) {
result = left / right;
}
}
}
@@ -0,0 +1,10 @@
package com.baeldung.o;
public class Guitar {
private String make;
private String model;
private int volume;
//Constructors, getters & setters
}
@@ -0,0 +1,41 @@
package com.baeldung.o;
public class Subtraction implements CalculatorOperation {
private double left;
private double right;
private double result = 0.0;
public Subtraction(double left, double right) {
this.left = left;
this.right = right;
}
public double getLeft() {
return left;
}
public void setLeft(double left) {
this.left = left;
}
public double getRight() {
return right;
}
public void setRight(double right) {
this.right = right;
}
public double getResult() {
return result;
}
public void setResult(double result) {
this.result = result;
}
@Override
public void perform() {
result = left - right;
}
}
@@ -0,0 +1,9 @@
package com.baeldung.o;
public class SuperCoolGuitarWithFlames extends Guitar {
private String flameColour;
//constructor, getters + setters
}
@@ -0,0 +1,27 @@
package com.baeldung.s;
public class BadBook {
private String name;
private String author;
private String text;
//constructor, getters and setters
//methods that directly relate to the book properties
public String replaceWordInText(String word, String replacementWord){
return text.replaceAll(word, replacementWord);
}
public boolean isWordInText(String word){
return text.contains(word);
}
//methods for outputting text to console - should this really be here?
void printTextToConsole(){
//our code for formatting and printing the text
}
}
@@ -0,0 +1,13 @@
package com.baeldung.s;
public class BookPrinter {
//methods for outputting text
void printTextToConsole(String text){
//our code for formatting and printing the text
}
void printTextToAnotherMedium(String text){
//code for writing to any other location..
}
}
@@ -0,0 +1,20 @@
package com.baeldung.s;
public class GoodBook {
private String name;
private String author;
private String text;
//constructor, getters and setters
//methods that directly relate to the book properties
public String replaceWordInText(String word, String replacementWord){
return text.replaceAll(word, replacementWord);
}
public boolean isWordInText(String word){
return text.contains(word);
}
}
@@ -0,0 +1,37 @@
package com.baeldung.s;
public class TextManipulator {
private String text;
public TextManipulator(String text) {
this.text = text;
}
public String getText() {
return text;
}
public void appendText(String newText) {
text = text.concat(newText);
}
public String findWordAndReplace(String word, String replacementWord) {
if (text.contains(word)) {
text = text.replace(word, replacementWord);
}
return text;
}
public String findWordAndDelete(String word) {
if (text.contains(word)) {
text = text.replace(word, "");
}
return text;
}
/*
* Bad practice when implementing SRP principle, not in the scope of this class
public void printText() {
System.out.println(textManipulator.getText());
}*/
}
@@ -0,0 +1,23 @@
package com.baeldung.s;
import java.util.Arrays;
public class TextPrinter {
TextManipulator textManipulator;
public TextPrinter(TextManipulator textManipulator) {
this.textManipulator = textManipulator;
}
public void printText() {
System.out.println(textManipulator.getText());
}
public void printOutEachWordOfText() {
System.out.println(Arrays.toString(textManipulator.getText().split(" ")));
}
public void printRangeOfCharacters(int startingIndex, int endIndex) {
System.out.println(textManipulator.getText().substring(startingIndex, endIndex));
}
}