added solid code (#6226)
This commit is contained in:
parent
f6bbd52fee
commit
4845bd82a1
|
@ -0,0 +1,23 @@
|
|||
<?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>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>solid/artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<!-- https://mvnrepository.com/artifact/junit/junit -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,4 @@
|
|||
package com.baeldung.d;
|
||||
|
||||
public class Keyboard {
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package com.baeldung.d;
|
||||
|
||||
public class Monitor {
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.d;
|
||||
|
||||
public class Windows98Machine {
|
||||
|
||||
private final Keyboard keyboard;
|
||||
private final Monitor monitor;
|
||||
|
||||
public Windows98Machine() {
|
||||
|
||||
monitor = new Monitor();
|
||||
keyboard = new Keyboard();
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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,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,10 @@
|
|||
package com.baeldung.o;
|
||||
|
||||
public class Guitar {
|
||||
|
||||
private String make;
|
||||
private String model;
|
||||
private int volume;
|
||||
|
||||
//Constructors, getters & setters
|
||||
}
|
|
@ -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){
|
||||
return text.replaceAll(word, text);
|
||||
}
|
||||
|
||||
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){
|
||||
return text.replaceAll(word, text);
|
||||
}
|
||||
|
||||
public boolean isWordInText(String word){
|
||||
return text.contains(word);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue