Compare commits

...

5 Commits

Author SHA1 Message Date
Ilkka Seppälä
bbe68f6957 Update README.md 2020-09-06 20:06:54 +03:00
Ilkka Seppälä
ef326ee77e Update README.md 2020-09-06 19:56:07 +03:00
Ilkka Seppälä
8b5f532a50 Update README.md 2020-09-06 19:55:38 +03:00
Ilkka Seppälä
a1da1e4973 Cleanup factory 2020-09-06 19:46:13 +03:00
Ilkka Seppälä
9d21dff855 Merge pull request #1520 from iluwatar/all-contributors/add-ravening
docs: add ravening as a contributor
2020-09-06 19:39:08 +03:00
3 changed files with 31 additions and 36 deletions

View File

@@ -10,6 +10,10 @@ tags:
- Microservices
---
## Also knows as
Gateway Routing
## Intent
Aggregate calls to microservices in a single location, the API Gateway. The user makes a single call
@@ -146,6 +150,7 @@ public class ApiGateway {
```
## Class diagram
![alt text](./etc/api-gateway.png "API Gateway")
## Applicability
@@ -160,3 +165,4 @@ Use the API Gateway pattern when
* [NGINX - Building Microservices: Using an API Gateway](https://www.nginx.com/blog/building-microservices-using-an-api-gateway/)
* [Microservices Patterns: With examples in Java](https://www.amazon.com/gp/product/1617294543/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=1617294543&linkId=ac7b6a57f866ac006a309d9086e8cfbd)
* [Building Microservices: Designing Fine-Grained Systems](https://www.amazon.com/gp/product/1491950358/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=1491950358&linkId=4c95ca9831e05e3f0dadb08841d77bf1)
* [Gateway Routing pattern](https://docs.microsoft.com/en-us/azure/architecture/patterns/gateway-routing)

View File

@@ -15,35 +15,31 @@ tags:
## Intent
Providing a static method encapsulated in a class called factory, in order to hide the implementation logic and makes client code focus on usage rather then initialization new objects.
Providing a static method encapsulated in a class called factory, in order to hide the
implementation logic and makes client code focus on usage rather then initialization new objects.
## Explanation
Real world example
> Lets say we have a web application connected to SQLServer, but now we want to switch to Oracle. To do so without modifying existing source code, we need to implements Simple Factory pattern, in which a static method can be invoked to create connection to a given database.
> Lets say we have a web application connected to SQLServer, but now we want to switch to Oracle. To
> do so without modifying existing source code, we need to implements Simple Factory pattern, in
> which a static method can be invoked to create connection to a given database.
Wikipedia says
> Factory is an object for creating other objects formally a factory is a function or method that returns objects of a varying prototype or class.
> Factory is an object for creating other objects formally a factory is a function or method that
> returns objects of a varying prototype or class.
**Programmatic Example**
We have an interface 'Car' and tow implementations 'Ford' and 'Ferrari'.
We have an interface `Car` and two implementations `Ford` and `Ferrari`.
```java
/**
* Car interface.
*/
public interface Car {
public String getDescription();
String getDescription();
}
/**
* Ford implementation.
*/
public class Ford implements Car {
static final String DESCRIPTION = "This is Ford.";
@@ -54,9 +50,6 @@ public class Ford implements Car {
}
}
/**
* Ferrari implementation.
*/
public class Ferrari implements Car {
static final String DESCRIPTION = "This is Ferrari.";
@@ -68,14 +61,11 @@ public class Ferrari implements Car {
}
```
Enumeration above represents types of cars that we support (Ford and Ferrari).
Enumeration above represents types of cars that we support (`Ford` and `Ferrari`).
```java
public enum CarType {
/**
* Enumeration for different types of cars.
*/
FORD(Ford::new),
FERRARI(Ferrari::new);
@@ -90,17 +80,12 @@ public enum CarType {
}
}
```
Then we have the static method 'getCar' to create car objects encapsulated in the factory class 'CarSimpleFactory'.
Then we have the static method `getCar` to create car objects encapsulated in the factory class
`CarSimpleFactory`.
```java
/**
* Factory of cars.
*/
public class CarsFactory {
/**
* Factory method takes as parameter a car type and initiate the appropriate class.
*/
public static Car getCar(CarType type) {
return type.getConstructor().get();
}
@@ -122,23 +107,27 @@ Program output:
This is Ford.
This Ferrari.
```
## Class Diagram
![alt text](./etc/factory.urm.png "Factory pattern class diagram")
## Applicability
Use the Simple Factory pattern when you only care about the creation of a object, not how to create and manage it.
## Pros
Use the Simple Factory pattern when you only care about the creation of a object, not how to create
and manage it.
Pros
* Allows keeping all objects creation in one place and avoid of spreading 'new' key value across codebase.
* Allows to writs loosely coupled code. Some of its main advantages include better testability, easy-to-understand code, swappable components, scalability and isolated features.
## Cons
Cons
* The code becomes more complicated than it should be.
## Related patterns
[Factory Method](https://java-design-patterns.com/patterns/factory-method/)
[Factory Kit](https://java-design-patterns.com/patterns/factory-kit/)
[Abstract Factory](https://java-design-patterns.com/patterns/abstract-factory/)
* [Factory Method](https://java-design-patterns.com/patterns/factory-method/)
* [Factory Kit](https://java-design-patterns.com/patterns/factory-kit/)
* [Abstract Factory](https://java-design-patterns.com/patterns/abstract-factory/)

View File

@@ -5,6 +5,6 @@ package com.iluwatar.factory;
*/
public interface Car {
public String getDescription();
String getDescription();
}