[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,8 @@
|
||||
### Relevant Articles:
|
||||
- [Facade Design Pattern in Java](https://www.baeldung.com/java-facade-pattern)
|
||||
- [Proxy, Decorator, Adapter and Bridge Patterns](https://www.baeldung.com/java-structural-design-patterns)
|
||||
- [Composite Design Pattern in Java](https://www.baeldung.com/java-composite-pattern)
|
||||
- [The Decorator Pattern in Java](https://www.baeldung.com/java-decorator-pattern)
|
||||
- [The Adapter Pattern in Java](https://www.baeldung.com/java-adapter-pattern)
|
||||
- [The Proxy Pattern in Java](https://www.baeldung.com/java-proxy-pattern)
|
||||
- [The Bridge Pattern in Java](https://www.baeldung.com/java-bridge-pattern)
|
||||
@@ -0,0 +1,25 @@
|
||||
<?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>design-patterns-structural</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>design-patterns-structural</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>patterns-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
+20
@@ -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.");
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.adapter;
|
||||
|
||||
public class AstonMartin implements Movable {
|
||||
@Override
|
||||
public double getSpeed() {
|
||||
return 220;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.adapter;
|
||||
|
||||
public class BugattiVeyron implements Movable {
|
||||
@Override
|
||||
public double getSpeed() {
|
||||
return 268;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.adapter;
|
||||
|
||||
public class McLaren implements Movable {
|
||||
@Override
|
||||
public double getSpeed() {
|
||||
return 241;
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.adapter;
|
||||
|
||||
public interface Movable {
|
||||
// returns speed in MPH
|
||||
double getSpeed();
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.adapter;
|
||||
|
||||
public interface MovableAdapter {
|
||||
// returns speed in KMPH
|
||||
double getSpeed();
|
||||
}
|
||||
+19
@@ -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;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.bridge;
|
||||
|
||||
public class Blue implements Color {
|
||||
@Override
|
||||
public String fill() {
|
||||
return "Color is Blue";
|
||||
}
|
||||
}
|
||||
+14
@@ -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());
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.bridge;
|
||||
|
||||
public interface Color {
|
||||
String fill();
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.bridge;
|
||||
|
||||
public class Red implements Color {
|
||||
|
||||
@Override
|
||||
public String fill() {
|
||||
return "Color is Red";
|
||||
}
|
||||
|
||||
}
|
||||
+11
@@ -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();
|
||||
}
|
||||
+13
@@ -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();
|
||||
}
|
||||
}
|
||||
+13
@@ -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();
|
||||
}
|
||||
}
|
||||
+19
@@ -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();
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.composite;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/1/2018.
|
||||
*/
|
||||
public interface Department {
|
||||
|
||||
void printDepartmentName();
|
||||
}
|
||||
+35
@@ -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;
|
||||
}
|
||||
}
|
||||
+33
@@ -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);
|
||||
}
|
||||
}
|
||||
+35
@@ -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;
|
||||
}
|
||||
}
|
||||
+16
@@ -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";
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.decorator;
|
||||
|
||||
public interface ChristmasTree {
|
||||
String decorate();
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.decorator;
|
||||
|
||||
public class ChristmasTreeImpl implements ChristmasTree {
|
||||
|
||||
@Override
|
||||
public String decorate() {
|
||||
return "Christmas tree";
|
||||
}
|
||||
|
||||
}
|
||||
+18
@@ -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());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+16
@@ -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";
|
||||
}
|
||||
}
|
||||
+15
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
+32
@@ -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();
|
||||
}
|
||||
}
|
||||
+19
@@ -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.");
|
||||
}
|
||||
}
|
||||
+13
@@ -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..");
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user