added solid code (#6226)

This commit is contained in:
Sam Millington 2019-01-27 14:18:13 +00:00 committed by Grzegorz Piwowarek
parent f6bbd52fee
commit 4845bd82a1
20 changed files with 234 additions and 0 deletions

View File

@ -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>

View File

@ -0,0 +1,4 @@
package com.baeldung.d;
public class Keyboard {
}

View File

@ -0,0 +1,6 @@
package com.baeldung.d;
public class Monitor {
}

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -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..
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.i;
public interface BearCleaner {
void washTheBear();
}

View File

@ -0,0 +1,5 @@
package com.baeldung.i;
public interface BearFeeder {
void feedTheBear();
}

View File

@ -0,0 +1,9 @@
package com.baeldung.i;
public interface BearKeeper {
void washTheBear();
void feedTheBear();
void petTheBear();
}

View File

@ -0,0 +1,5 @@
package com.baeldung.i;
public interface BearPetter {
void petTheBear();
}

View File

@ -0,0 +1,8 @@
package com.baeldung.i;
public class CrazyPerson implements BearPetter {
public void petTheBear() {
//Good luck with that!
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.l;
public interface Car {
void turnOnEngine();
void accelerate();
}

View File

@ -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!
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.l;
public class Engine {
public void on(){
//vroom.
}
public void powerOn(int amount){
//do something
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.o;
public class Guitar {
private String make;
private String model;
private int volume;
//Constructors, getters & setters
}

View File

@ -0,0 +1,9 @@
package com.baeldung.o;
public class SuperCoolGuitarWithFlames extends Guitar {
private String flameColour;
//constructor, getters + setters
}

View File

@ -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
}
}

View File

@ -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..
}
}

View File

@ -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);
}
}