BAEL-16653 (#7691)
* added module 'design-patterns-behavioral' * added module 'design-patterns-structural' * added module 'design-patterns-creational' * added module 'design-patterns-architectural' * added module 'design-patterns-functional' * moved facade code examples from design-patterns to design-patterns-structural * moved singleton examples from design-patterns to design-patterns-creational * moved bridge examples from design-patterns to design-patterns-structural * moved creational examples from design-patterns to design-patterns-creational * moved observer examples from design-patterns to design-patterns-behavioral * moved flyweight examples from design-patterns to design-patterns-creational * moved service locator examples from design-patterns to design-patterns-architectural * moved double checked locking singleton examples from design-patterns to design-patterns-creational * moved composite examples from design-patterns to design-patterns-structural * moved visitor examples from design-patterns to design-patterns-behavioral * moved dao examples from design-patterns to design-patterns-architectural * moved interpreter examples from design-patterns to design-patterns-behavioral * moved state examples from design-patterns to design-patterns-behavioral * moved decorator examples from design-patterns to design-patterns-structural * renamed LogerUtil to LoggerUtil * moved template method examples from design-patterns to design-patterns-behavioral * moved chain of responsibility examples from design-patterns to design-patterns-behavioral * moved command examples from design-patterns to design-patterns-behavioral * moved constructor vs static factory method examples from design-patterns to design-patterns-creational * moved adapter examples from design-patterns to design-patterns-structural * moved currying examples from design-patterns to design-patterns-functional * moved proxy examples from design-patterns to design-patterns-structural * moved persistence.xml from design-patterns to design-patterns-architectural * deleted empty module: design-patterns * moved mediator examples from design-patterns-2 to design-patterns-behavioral * moved null object examples from design-patterns-2 to design-patterns-behavioral * moved null check examples from design-patterns to design-patterns-behavioral * moved freebuilder examples from design-patterns-2 to design-patterns-creational * added module design-patterns-behavioral-2 * moved memento examples from design-patterns-2 to design-patterns-behavioral-2 * removed empty module design-patterns-2 * changed http to https in readmes in modules design-patterns-*
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.adapter;
|
||||
|
||||
import static com.baeldung.util.LoggerUtil.LOG;
|
||||
|
||||
public class AdapterPatternDriver {
|
||||
|
||||
public static void main(String args[]) {
|
||||
Movable bugattiVeyron = new BugattiVeyron();
|
||||
MovableAdapter bugattiVeyronAdapter = new MovableAdapterImpl(bugattiVeyron);
|
||||
LOG.info("Bugatti Veyron Super Sport's top speed is " + bugattiVeyronAdapter.getSpeed() + " Kmph.");
|
||||
|
||||
Movable mcLaren = new McLaren();
|
||||
MovableAdapter mcLarenAdapter = new MovableAdapterImpl(mcLaren);
|
||||
LOG.info("McLaren F1 top speed is " + mcLarenAdapter.getSpeed() + " Kmph.");
|
||||
|
||||
Movable astonMartin = new AstonMartin();
|
||||
MovableAdapter astonMartinAdapter = new MovableAdapterImpl(astonMartin);
|
||||
LOG.info("McLaren F1 top speed is " + astonMartinAdapter.getSpeed() + " Kmph.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.adapter;
|
||||
|
||||
public class AstonMartin implements Movable {
|
||||
@Override
|
||||
public double getSpeed() {
|
||||
return 220;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.adapter;
|
||||
|
||||
public class BugattiVeyron implements Movable {
|
||||
@Override
|
||||
public double getSpeed() {
|
||||
return 268;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.adapter;
|
||||
|
||||
public class McLaren implements Movable {
|
||||
@Override
|
||||
public double getSpeed() {
|
||||
return 241;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.adapter;
|
||||
|
||||
public interface Movable {
|
||||
// returns speed in MPH
|
||||
double getSpeed();
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.adapter;
|
||||
|
||||
public interface MovableAdapter {
|
||||
// returns speed in KMPH
|
||||
double getSpeed();
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.adapter;
|
||||
|
||||
public class MovableAdapterImpl implements MovableAdapter {
|
||||
private Movable luxuryCars;
|
||||
|
||||
public MovableAdapterImpl(Movable luxuryCars) {
|
||||
this.luxuryCars = luxuryCars;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getSpeed() {
|
||||
double mph = luxuryCars.getSpeed();
|
||||
return convertMPHtoKMPH(mph);
|
||||
}
|
||||
|
||||
private double convertMPHtoKMPH(double mph) {
|
||||
return mph * 1.60934;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.bridge;
|
||||
|
||||
public class Blue implements Color {
|
||||
@Override
|
||||
public String fill() {
|
||||
return "Color is Blue";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.bridge;
|
||||
|
||||
public class BridgePatternDriver {
|
||||
|
||||
public static void main(String[] args) {
|
||||
//a square with red color
|
||||
Shape square = new Square(new Red());
|
||||
System.out.println(square.draw());
|
||||
|
||||
//a triangle with blue color
|
||||
Shape triangle = new Triangle(new Blue());
|
||||
System.out.println(triangle.draw());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.bridge;
|
||||
|
||||
public interface Color {
|
||||
String fill();
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.bridge;
|
||||
|
||||
public class Red implements Color {
|
||||
|
||||
@Override
|
||||
public String fill() {
|
||||
return "Color is Red";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.bridge;
|
||||
|
||||
public abstract class Shape {
|
||||
protected Color color;
|
||||
|
||||
public Shape(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
abstract public String draw();
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.bridge;
|
||||
|
||||
public class Square extends Shape {
|
||||
|
||||
public Square(Color color) {
|
||||
super(color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String draw() {
|
||||
return "Square drawn. " + color.fill();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.bridge;
|
||||
|
||||
public class Triangle extends Shape {
|
||||
|
||||
public Triangle(Color color) {
|
||||
super(color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String draw() {
|
||||
return "Triangle drawn. "+ color.fill();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.composite;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/3/2018.
|
||||
*/
|
||||
public class CompositeDemo {
|
||||
|
||||
public static void main(String args[]) {
|
||||
Department salesDepartment = new SalesDepartment(1, "Sales department");
|
||||
Department financialDepartment = new FinancialDepartment(2, "Financial department");
|
||||
|
||||
HeadDepartment headDepartment = new HeadDepartment(3, "Head department");
|
||||
|
||||
headDepartment.addDepartMent(salesDepartment);
|
||||
headDepartment.addDepartMent(financialDepartment);
|
||||
|
||||
headDepartment.printDepartmentName();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.composite;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/1/2018.
|
||||
*/
|
||||
public interface Department {
|
||||
|
||||
void printDepartmentName();
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.composite;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/1/2018.
|
||||
*/
|
||||
public class FinancialDepartment implements Department {
|
||||
|
||||
private Integer id;
|
||||
private String name;
|
||||
|
||||
public FinancialDepartment(Integer id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void printDepartmentName() {
|
||||
System.out.println(getClass().getSimpleName());
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.composite;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/1/2018.
|
||||
*/
|
||||
public class HeadDepartment implements Department {
|
||||
|
||||
private Integer id;
|
||||
private String name;
|
||||
|
||||
private List<Department> childDepartments;
|
||||
|
||||
public HeadDepartment(Integer id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.childDepartments = new ArrayList<Department>();
|
||||
}
|
||||
|
||||
public void printDepartmentName() {
|
||||
childDepartments.forEach(Department::printDepartmentName);
|
||||
}
|
||||
|
||||
public void addDepartMent(Department department) {
|
||||
childDepartments.add(department);
|
||||
}
|
||||
|
||||
public void removeDepartment(Department department) {
|
||||
childDepartments.remove(department);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.composite;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/1/2018.
|
||||
*/
|
||||
public class SalesDepartment implements Department {
|
||||
|
||||
private Integer id;
|
||||
private String name;
|
||||
|
||||
public SalesDepartment(Integer id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void printDepartmentName() {
|
||||
System.out.println(getClass().getSimpleName());
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.decorator;
|
||||
|
||||
public class BubbleLights extends TreeDecorator {
|
||||
|
||||
public BubbleLights(ChristmasTree tree) {
|
||||
super(tree);
|
||||
}
|
||||
|
||||
public String decorate() {
|
||||
return super.decorate() + decorateWithBubbleLights();
|
||||
}
|
||||
|
||||
private String decorateWithBubbleLights() {
|
||||
return " with Bubble Lights";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.decorator;
|
||||
|
||||
public interface ChristmasTree {
|
||||
String decorate();
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.decorator;
|
||||
|
||||
public class ChristmasTreeImpl implements ChristmasTree {
|
||||
|
||||
@Override
|
||||
public String decorate() {
|
||||
return "Christmas tree";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.decorator;
|
||||
|
||||
import static com.baeldung.util.LoggerUtil.LOG;
|
||||
|
||||
public class DecoratorPatternDriver {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// christmas tree with just one Garland
|
||||
ChristmasTree tree1 = new Garland(new ChristmasTreeImpl());
|
||||
LOG.info(tree1.decorate());
|
||||
|
||||
// christmas tree with two Garlands and one Bubble lights
|
||||
ChristmasTree tree2 = new BubbleLights(new Garland(new Garland(new ChristmasTreeImpl())));
|
||||
LOG.info(tree2.decorate());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.decorator;
|
||||
|
||||
public class Garland extends TreeDecorator {
|
||||
|
||||
public Garland(ChristmasTree tree) {
|
||||
super(tree);
|
||||
}
|
||||
|
||||
public String decorate() {
|
||||
return super.decorate() + decorateWithGarland();
|
||||
}
|
||||
|
||||
private String decorateWithGarland() {
|
||||
return " with Garland";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.decorator;
|
||||
|
||||
public abstract class TreeDecorator implements ChristmasTree {
|
||||
private ChristmasTree tree;
|
||||
|
||||
public TreeDecorator(ChristmasTree tree) {
|
||||
this.tree = tree;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String decorate() {
|
||||
return tree.decorate();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.facade;
|
||||
|
||||
import com.baeldung.facade.carsystem.*;
|
||||
|
||||
public class CarEngineFacade {
|
||||
private static final Integer DEFAULT_COOLING_TEMP = 90;
|
||||
private static final Integer MAX_ALLOWED_TEMP = 50;
|
||||
private FuelInjector fuelInjector = new FuelInjector();
|
||||
private AirFlowController airFlowController = new AirFlowController();
|
||||
private Starter starter = new Starter();
|
||||
private CoolingController coolingController = new CoolingController();
|
||||
private CatalyticConverter catalyticConverter = new CatalyticConverter();
|
||||
|
||||
public void startEngine(){
|
||||
fuelInjector.on();
|
||||
airFlowController.takeAir();
|
||||
fuelInjector.on();
|
||||
fuelInjector.inject();
|
||||
starter.start();
|
||||
coolingController.setTemperatureUpperLimit(DEFAULT_COOLING_TEMP);
|
||||
coolingController.run();
|
||||
catalyticConverter.on();
|
||||
}
|
||||
|
||||
public void stopEngine(){
|
||||
fuelInjector.off();
|
||||
catalyticConverter.off();
|
||||
coolingController.cool(MAX_ALLOWED_TEMP);
|
||||
coolingController.stop();
|
||||
airFlowController.off();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.facade.carsystem;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class AirFlowController {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(AirFlowController.class);
|
||||
private AirFlowMeter airFlowMeter = new AirFlowMeter();
|
||||
|
||||
public void takeAir() {
|
||||
airFlowMeter.getMeasurements();
|
||||
LOGGER.info("Air provided!");
|
||||
}
|
||||
|
||||
public void off() {
|
||||
LOGGER.info("Air controller switched off.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.facade.carsystem;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class AirFlowMeter {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(AirFlowMeter.class);
|
||||
|
||||
public void getMeasurements() {
|
||||
LOGGER.info("Getting air measurements..");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.facade.carsystem;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class CatalyticConverter {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CatalyticConverter.class);
|
||||
|
||||
public void on() {
|
||||
LOGGER.info("Catalytic Converter switched on!");
|
||||
}
|
||||
|
||||
public void off() {
|
||||
LOGGER.info("Catalytic Converter switched off!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.facade.carsystem;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class CoolingController {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CoolingController.class);
|
||||
private static final Integer DEFAULT_RADIATOR_SPEED = 10;
|
||||
|
||||
private Integer temperatureUpperLimit;
|
||||
private Radiator radiator = new Radiator();
|
||||
private TemperatureSensor temperatureSensor = new TemperatureSensor();
|
||||
|
||||
public void setTemperatureUpperLimit(Integer temperatureUpperLimit) {
|
||||
LOGGER.info("Setting temperature upper limit to {}", temperatureUpperLimit);
|
||||
this.temperatureUpperLimit = temperatureUpperLimit;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
LOGGER.info("Cooling Controller ready!");
|
||||
radiator.setSpeed(DEFAULT_RADIATOR_SPEED);
|
||||
}
|
||||
|
||||
public void cool(Integer maxAllowedTemp) {
|
||||
LOGGER.info("Scheduled cooling with maximum allowed temperature {}", maxAllowedTemp);
|
||||
temperatureSensor.getTemperature();
|
||||
radiator.on();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
LOGGER.info("Stopping Cooling Controller..");
|
||||
radiator.off();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.facade.carsystem;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class FuelInjector {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(FuelInjector.class);
|
||||
|
||||
private FuelPump fuelPump = new FuelPump();
|
||||
|
||||
public void on(){
|
||||
LOGGER.info("Fuel injector ready to inject fuel.");
|
||||
}
|
||||
|
||||
public void inject() {
|
||||
fuelPump.pump();
|
||||
LOGGER.info("Fuel injected.");
|
||||
}
|
||||
|
||||
public void off() {
|
||||
LOGGER.info("Stopping Fuel injector..");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.facade.carsystem;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class FuelPump {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(FuelPump.class);
|
||||
|
||||
public void pump() {
|
||||
LOGGER.info("Fuel Pump is pumping fuel..");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.facade.carsystem;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Radiator {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Radiator.class);
|
||||
|
||||
public void on(){
|
||||
LOGGER.info("Radiator switched on!");
|
||||
}
|
||||
|
||||
public void off(){
|
||||
LOGGER.info("Radiator switched off!");
|
||||
}
|
||||
|
||||
public void setSpeed(Integer speed){
|
||||
LOGGER.info("Setting radiator speed to {}",speed);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.facade.carsystem;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Starter {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Starter.class);
|
||||
|
||||
public void start() {
|
||||
LOGGER.info("Starting..");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.facade.carsystem;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class TemperatureSensor {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(TemperatureSensor.class);
|
||||
|
||||
public void getTemperature(){
|
||||
LOGGER.info("Getting temperature from the sensor..");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.proxy;
|
||||
|
||||
public interface ExpensiveObject {
|
||||
void process();
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.proxy;
|
||||
|
||||
import static com.baeldung.util.LoggerUtil.LOG;
|
||||
|
||||
public class ExpensiveObjectImpl implements ExpensiveObject {
|
||||
|
||||
public ExpensiveObjectImpl() {
|
||||
heavyInitialConfiguration();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
LOG.info("processing complete.");
|
||||
}
|
||||
|
||||
private void heavyInitialConfiguration() {
|
||||
LOG.info("Loading initial configuration..");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.proxy;
|
||||
|
||||
public class ExpensiveObjectProxy implements ExpensiveObject{
|
||||
private static ExpensiveObject object;
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
if(object == null) {
|
||||
object = new ExpensiveObjectImpl();
|
||||
}
|
||||
object.process();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.proxy;
|
||||
|
||||
public class ProxyPatternDriver {
|
||||
public static void main(String[] args) {
|
||||
ExpensiveObject object = new ExpensiveObjectProxy();
|
||||
object.process();
|
||||
object.process();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.util;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.apache.log4j.PropertyConfigurator;
|
||||
|
||||
public class LoggerUtil {
|
||||
|
||||
public final static Logger LOG = Logger.getLogger("GLOBAL");
|
||||
|
||||
static {
|
||||
configuration();
|
||||
}
|
||||
|
||||
private static void configuration() {
|
||||
Properties props = new Properties();
|
||||
try {
|
||||
props.load(
|
||||
new BufferedReader(
|
||||
new InputStreamReader(
|
||||
LoggerUtil.class.getResourceAsStream("/log4jstructuraldp.properties")
|
||||
)
|
||||
)
|
||||
);
|
||||
} catch (IOException e) {
|
||||
System.out.println("log4jstructuraldp.properties file not configured properly");
|
||||
System.exit(0);
|
||||
}
|
||||
PropertyConfigurator.configure(props);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
|
||||
# Root logger
|
||||
log4j.rootLogger=INFO, file, stdout
|
||||
|
||||
# Write to console
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.Target=System.out
|
||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.adapter;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class AdapterPatternIntegrationTest {
|
||||
@Test
|
||||
public void givenMovableAdapter_WhenConvertingMPHToKMPH_thenSuccessfullyConverted() {
|
||||
Movable bugattiVeyron = new BugattiVeyron();
|
||||
MovableAdapter bugattiVeyronAdapter = new MovableAdapterImpl(bugattiVeyron);
|
||||
assertEquals(bugattiVeyronAdapter.getSpeed(), 431.30312, 0.00001);
|
||||
|
||||
Movable mcLaren = new McLaren();
|
||||
MovableAdapter mcLarenAdapter = new MovableAdapterImpl(mcLaren);
|
||||
assertEquals(mcLarenAdapter.getSpeed(), 387.85094, 0.00001);
|
||||
|
||||
Movable astonMartin = new AstonMartin();
|
||||
MovableAdapter astonMartinAdapter = new MovableAdapterImpl(astonMartin);
|
||||
assertEquals(astonMartinAdapter.getSpeed(), 354.0548, 0.00001);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.bridge;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class BridgePatternIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void whenBridgePatternInvoked_thenConfigSuccess() {
|
||||
//a square with red color
|
||||
Shape square = new Square(new Red());
|
||||
assertEquals(square.draw(), "Square drawn. Color is Red");
|
||||
|
||||
//a triangle with blue color
|
||||
Shape triangle = new Triangle(new Blue());
|
||||
assertEquals(triangle.draw(), "Triangle drawn. Color is Blue");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.decorator;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class DecoratorPatternIntegrationTest {
|
||||
@Test
|
||||
public void givenDecoratorPattern_WhenDecoratorsInjectedAtRuntime_thenConfigSuccess() {
|
||||
ChristmasTree tree1 = new Garland(new ChristmasTreeImpl());
|
||||
assertEquals(tree1.decorate(),
|
||||
"Christmas tree with Garland");
|
||||
|
||||
ChristmasTree tree2 = new BubbleLights(
|
||||
new Garland(new Garland(new ChristmasTreeImpl())));
|
||||
assertEquals(tree2.decorate(),
|
||||
"Christmas tree with Garland with Garland with Bubble Lights");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.baeldung.facade;
|
||||
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.AppenderBase;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class CarEngineFacadeIntegrationTest {
|
||||
|
||||
|
||||
private InMemoryCustomTestAppender appender;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
appender = new InMemoryCustomTestAppender();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
appender.stop();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenStartEngine_thenAllNecessaryActionsPerformed() {
|
||||
CarEngineFacade carEngineFacade = new CarEngineFacade();
|
||||
carEngineFacade.startEngine();
|
||||
assertTrue(appender.logContains("Fuel injector ready to inject fuel."));
|
||||
assertTrue(appender.logContains("Getting air measurements.."));
|
||||
assertTrue(appender.logContains("Air provided!"));
|
||||
assertTrue(appender.logContains("Fuel injector ready to inject fuel."));
|
||||
assertTrue(appender.logContains("Fuel Pump is pumping fuel.."));
|
||||
assertTrue(appender.logContains("Fuel injected."));
|
||||
assertTrue(appender.logContains("Starting.."));
|
||||
assertTrue(appender.logContains("Setting temperature upper limit to 90"));
|
||||
assertTrue(appender.logContains("Cooling Controller ready!"));
|
||||
assertTrue(appender.logContains("Setting radiator speed to 10"));
|
||||
assertTrue(appender.logContains("Catalytic Converter switched on!"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenStopEngine_thenAllNecessaryActionsPerformed() {
|
||||
CarEngineFacade carEngineFacade = new CarEngineFacade();
|
||||
carEngineFacade.stopEngine();
|
||||
assertTrue(appender.logContains("Stopping Fuel injector.."));
|
||||
assertTrue(appender.logContains("Catalytic Converter switched off!"));
|
||||
assertTrue(appender.logContains("Scheduled cooling with maximum allowed temperature 50"));
|
||||
assertTrue(appender.logContains("Getting temperature from the sensor.."));
|
||||
assertTrue(appender.logContains("Radiator switched on!"));
|
||||
assertTrue(appender.logContains("Stopping Cooling Controller.."));
|
||||
assertTrue(appender.logContains("Radiator switched off!"));
|
||||
assertTrue(appender.logContains("Air controller switched off."));
|
||||
}
|
||||
|
||||
private class InMemoryCustomTestAppender extends AppenderBase<ILoggingEvent> {
|
||||
|
||||
private List<ILoggingEvent> logs = new ArrayList<>();
|
||||
|
||||
public InMemoryCustomTestAppender() {
|
||||
((Logger) LoggerFactory.getLogger("root")).addAppender(this);
|
||||
start();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void append(ILoggingEvent eventObject) {
|
||||
logs.add(eventObject);
|
||||
}
|
||||
|
||||
public boolean logContains(String message) {
|
||||
return logs.stream().anyMatch(event -> event.getFormattedMessage().equals(message));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.proxy;
|
||||
|
||||
import static com.baeldung.util.LoggerUtil.LOG;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.log4j.spi.LoggingEvent;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ProxyPatternIntegrationTest {
|
||||
public static TestAppenderDP appender;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
appender = new TestAppenderDP();
|
||||
LOG.addAppender(appender);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExpensiveObjectProxy_WhenObjectInitialized_thenInitializedOnlyOnce() {
|
||||
ExpensiveObject object = new ExpensiveObjectProxy();
|
||||
object.process();
|
||||
object.process();
|
||||
|
||||
final List<LoggingEvent> log = appender.getLog();
|
||||
|
||||
assertThat((String) log.get(0).getMessage(), is("Loading initial configuration.."));
|
||||
assertThat((String) log.get(1).getMessage(), is("processing complete."));
|
||||
assertThat((String) log.get(2).getMessage(), is("processing complete."));
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
LOG.removeAppender(appender);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.proxy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.log4j.AppenderSkeleton;
|
||||
import org.apache.log4j.spi.LoggingEvent;
|
||||
|
||||
public class TestAppenderDP extends AppenderSkeleton {
|
||||
private final List<LoggingEvent> log = new ArrayList<LoggingEvent>();
|
||||
|
||||
@Override
|
||||
public boolean requiresLayout() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void append(final LoggingEvent loggingEvent) {
|
||||
log.add(loggingEvent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
|
||||
public List<LoggingEvent> getLog() {
|
||||
return new ArrayList<LoggingEvent>(log);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user