[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,7 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [A Solid Guide to Solid Principles](https://www.baeldung.com/solid-principles)
|
||||
- [Single Responsibility Principle in Java](https://www.baeldung.com/java-single-responsibility-principle)
|
||||
- [Open/Closed Principle in Java](https://www.baeldung.com/java-open-closed-principle)
|
||||
- [Interface Segregation Principle in Java](https://www.baeldung.com/java-interface-segregation)
|
||||
- [Liskov Substitution Principle in Java](https://www.baeldung.com/java-liskov-substitution-principle)
|
||||
@@ -0,0 +1,16 @@
|
||||
<?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>solid</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>solid</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>patterns-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
</project>
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
+15
@@ -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
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user