Browse Source

docs: adding subjects for quest 7 of the java piscine (#2065)

pull/2082/head
zanninso 1 year ago committed by GitHub
parent
commit
ec30312c08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 108
      subjects/java-piscine/Quest7-DesingPattern/Builder/README.md
  2. 94
      subjects/java-piscine/Quest7-DesingPattern/Decorator/README.md
  3. 78
      subjects/java-piscine/Quest7-DesingPattern/Factory/README.md
  4. 102
      subjects/java-piscine/Quest7-DesingPattern/Observer/README.md
  5. 48
      subjects/java-piscine/Quest7-DesingPattern/Singleton/README.md
  6. 84
      subjects/java-piscine/Quest7-DesingPattern/Strategy/README.md

108
subjects/java-piscine/Quest7-DesingPattern/Builder/README.md

@ -0,0 +1,108 @@
## Builder
### Instructions
Now let's implement the Builder Design Pattern
```mermaid
classDiagram
class Housing {
<<interface>>
+setSize(int size)*
+setPrice(int price)*
+setRooms(int rooms)*
+setName(String name)*
}
class House {
+setSize(int size)
+setPrice(int price)
+setRooms(int rooms)
+setName(String name)
+toString() String
}
class Apartment {
+setSize(int size)
+setPrice(int price)
+setRooms(int rooms)
+setName(String name)
+toString() String
}
class HousingBuilder {
<<interface>>
+setSize(int size)* HousingBuilder
+setPrice(int price)* HousingBuilder
+setRooms(int rooms)* HousingBuilder
+setName(String name)* HousingBuilder
+build()* Housing
}
class HouseBuilder {
-House house
+setSize(int size) HousingBuilder
+setPrice(int price) HousingBuilder
+setRooms(int rooms) HousingBuilder
+setName(String name) HousingBuilder
+build() Housing
}
class ApartmentBuilder {
-Apartment apartment
+setSize(int size) HousingBuilder
+setPrice(int price) HousingBuilder
+setRooms(int rooms) HousingBuilder
+setName(String name) HousingBuilder
+build() Housing
}
Housing <|.. Apartment
Housing <|.. House
HousingBuilder <|.. ApartmentBuilder
HousingBuilder <|.. HouseBuilder
```
Here is the matching class diagram. Create the matching classes in the matching files.
The `Apartment` and `House` class should have an empty constructor.
The `toString` method of `Apartment` should return : `Apartment{size=<size>, price=<price>, rooms=<rooms>, name='<name>'}`
The `toString` method of `House` should return : `House{size=<size>, price=<price>, rooms=<rooms>, name='<name>'}`
### Usage
Here is a possible ExerciseRunner.java to test your function :
```java
public class ExerciseRunner {
public static void main(String[] args) {
Housing house = new HouseBuilder()
.setName("Maison")
.setRooms(4)
.setSize(80)
.setPrice(100000)
.build();
Housing apartment = new ApartmentBuilder()
.setName("Appart")
.setRooms(2)
.setSize(30)
.setPrice(25000)
.build();
System.out.println(house);
System.out.println(apartment);
}
}
```
and its output :
```shell
$ javac *.java -d build
$ java -cp build ExerciseRunner
House{size=80, price=100000, rooms=4, name='Maison'}
Apartment{size=30, price=25000, rooms=2, name='Appart'}
$
```
### Notions
[Class diagram](https://fr.wikipedia.org/wiki/Diagramme_de_classes)

94
subjects/java-piscine/Quest7-DesingPattern/Decorator/README.md

@ -0,0 +1,94 @@
## Decorator
### Instructions
Now let's implement the Decorator Design Pattern
```mermaid
classDiagram
class Raclette {
<<interface>>
+getCalories()* String
+getIngredients()* String
}
class BaseRaclette {
+getCalories() String
+getIngredients() String
}
class RacletteDecorator {
<<abstract>>
-Raclette raclette
+RacletteDecorator(Raclette raclette)
+getCalories() String
+getIngredients() String
}
class WithPickles {
-Raclette raclette
+RacletteDecorator(Raclette raclette)
+getCalories() String
+getIngredients() String
}
class WithColdMeats {
-Raclette decoratedRaclette
+RacletteDecorator(Raclette raclette)
+getCalories() String
+getIngredients() String
}
Raclette <|.. BaseRaclette
Raclette <|.. RacletteDecorator
RacletteDecorator <|-- WithPickles
RacletteDecorator <|-- WithColdMeats
```
Here is the matching class diagram. Create the matching classes in the matching files.
Here is the description :
* `BaseRaclette` :
* `getCalories` should return 1000
* `getIngredients` should return "Patate, fromage Ă  raclette"
* `RacletteDecorator` :
* `getCalories` should return the `getCalories` of the `decoratedRaclette` attribute
* `getIngredients` should return the `getIngredients` of the `decoratedRaclette` attribute
* `WithPickles` :
* `getCalories` should return the addition of 50 and the `getCalories` of the `decoratedRaclette` attribute
* `getIngredients` should return the concatenation of `getIngredients` of the `decoratedRaclette` attribute and ", cornichons"
* `WithColdMeats` :
* `getCalories` should return the addition of 350 and the `getCalories` of the `decoratedRaclette` attribute
* `getIngredients` should return the concatenation of `getIngredients` of the `decoratedRaclette` attribute and ", charcuterie"
For each class, implement the `toString` method to return "`getIngredients()` pour `getCalories()` calories".
### Usage
Here is a possible ExerciseRunner.java to test your function :
```java
public class ExerciseRunner {
public static void main(String[] args) {
Raclette r = new BaseRaclette();
System.out.println(r);
r = new WithPickles(r);
System.out.println(r);
r = new WithColdMeats(r);
System.out.println(r);
}
}
```
and its output :
```shell
$ javac *.java -d build
$ java -cp build ExerciseRunner
Patate, fromage Ă  raclette pour 1000 calories
Patate, fromage Ă  raclette, cornichons pour 1050 calories
Patate, fromage Ă  raclette, cornichons, charcuterie pour 1400 calories
$
```
### Notions
[Class diagram](https://fr.wikipedia.org/wiki/Diagramme_de_classes)

78
subjects/java-piscine/Quest7-DesingPattern/Factory/README.md

@ -0,0 +1,78 @@
## Factory
### Instructions
Now let's implement the Factory Design Pattern
```mermaid
classDiagram
class Driver {
<<abstract>>
+createTransport()* Transport
}
class CarDriver {
+createTransport() Transport
}
class PlaneDriver {
+createTransport() Transport
}
class Transport {
<<interface>>
+getDistance()* int
}
class Car {
+getDistance() int
}
class Plane {
+getDistance() int
}
class DriverFactory {
+ getDriver(Type: String) Driver
}
class TransportFactory {
+ getTransport(Type: String) int
}
Driver <|-- CarDriver
Driver <|-- PlaneDriver
Transport <|-- Car
Transport <|-- Plane
```
Here is the matching class diagram. Create the matching classes in the matching files.
The method createTransport of CarDriver should build and return a Car using TransportFactory, and for the PlaneDriver class, it should build and return a Plane.
The car should return 600 and the plane should return 10000.
### Usage
Here is a possible ExerciseRunner.java to test your function :
```java
public class ExerciseRunner {
public static void main(String[] args) {
System.out.println(DriverFactory.getDriver("Car").createTransport().getDistance());
System.out.println(DriverFactory.getDriver("Plane").createTransport().getDistance());
}
}
```
and its output :
```shell
$ javac *.java -d build
$ java -cp build ExerciseRunner
600
10000
$
```
### Notions
[Class diagram](https://fr.wikipedia.org/wiki/Diagramme_de_classes)

102
subjects/java-piscine/Quest7-DesingPattern/Observer/README.md

@ -0,0 +1,102 @@
## Observer
### Instructions
Now let's implement the Observer Design Pattern
```mermaid
classDiagram
class ValuePublisher {
-List~NumericBaseObserver~ observers
+updateState(int value)
+subscribe(NumericBaseObserver observer)
+unsubscribe(NumericBaseObserver observer)
}
class NumericBaseObserver {
<<interface>>
+updateState(int state)*
+getEvents()* List~String~
}
class BinaryBaseObserver {
-List~String~ events
+updateState(int state)
+getEvents() List<String>
}
class DecimalBaseObserver {
-List~String~ events
+updateState(int state)
+getEvents() List<String>
}
class HexaBaseObserver {
-List~String~ events
+updateState(int state)
+getEvents() List<String>
}
ValuePublisher o-- NumericBaseObserver
NumericBaseObserver <|.. BinaryBaseObserver
NumericBaseObserver <|.. DecimalBaseObserver
NumericBaseObserver <|.. HexaBaseObserver
```
Here is the matching class diagram. Create the matching classes in the matching files.
Here is the description :
* For `ValuePublisher` :
* `updateState` calls the `updateState` method of all observers.
* `subscribe` should add the observer in the list of observers.
* `unsubscribe` should remove the observer of the list of observers.
* For `BinaryBaseObserver` :
* `updateState` should add the binary String of the value in parameter to the list of events (e.g. for 13, should add "1101")
* `getEvents` should return the list of events.
* For `DecimalBaseObserver` :
* `updateState` should add the decimal String of the value in parameter to the list of events (e.g. for 13, should add "13")
* `getEvents` should return the list of events.
* For `HexaBaseObserver` :
* `updateState` should add the hexadecimal String of the value in parameter to the list of events (e.g. for 13, should add "d")
* `getEvents` should return the list of events.
### Usage
Here is a possible ExerciseRunner.java to test your function :
```java
public class ExerciseRunner {
public static void main(String[] args) {
ValuePublisher publisher = new ValuePublisher();
BinaryBaseObserver binaryObserver = new BinaryBaseObserver();
DecimalBaseObserver decimalObserver = new DecimalBaseObserver();
HexaBaseObserver hexaObserver = new HexaBaseObserver();
publisher.subscribe(binaryObserver);
publisher.subscribe(decimalObserver);
publisher.subscribe(hexaObserver);
publisher.updateState(1254);
publisher.updateState(45839);
publisher.updateState(5984382);
System.out.println("Binary " + binaryObserver.getEvents());
System.out.println("Decimal " + decimalObserver.getEvents());
System.out.println("Hexa " + hexaObserver.getEvents());
}
}
```
and its output :
```shell
$ javac *.java -d build
$ java -cp build ExerciseRunner
Binary [10011100110, 1011001100001111, 10110110101000001111110]
Decimal [1254, 45839, 5984382]
Hexa [4e6, b30f, 5b507e]
$
```
### Notions
[Class diagram](https://fr.wikipedia.org/wiki/Diagramme_de_classes)

48
subjects/java-piscine/Quest7-DesingPattern/Singleton/README.md

@ -0,0 +1,48 @@
## Singleton
### Instructions
In this quest, we will implement some design patterns.
For first, we will implement the singleton pattern.
```mermaid
classDiagram
class Excalibur{
-String name
-Excalibur INSTANCE$
-Excalibur(String name)
+getName() String
+getInstance()$ Excalibur
}
Excalibur <-- Excalibur
```
Here is the matching class diagram. Create the matching class in the matching file.
When calling the getInstance method, an instance of Excalibur with name "Sword"
### Usage
Here is a possible ExerciseRunner.java to test your function :
```java
public class ExerciseRunner {
public static void main(String[] args) {
System.out.println(Excalibur.getInstance().getName());
}
}
```
and its output :
```shell
$ javac *.java -d build
$ java -cp build ExerciseRunner
Sword
$
```
### Notions
[Class diagram](https://fr.wikipedia.org/wiki/Diagramme_de_classes)

84
subjects/java-piscine/Quest7-DesingPattern/Strategy/README.md

@ -0,0 +1,84 @@
## Strategy
### Instructions
Now let's implement the Visitor Design Pattern
```mermaid
classDiagram
class Context {
-OperationStrategy operationStrategy
+Context()
+changeStrategy(OperationStrategy operationStrategy)
+execute(int a, int b) int
}
class OperationStrategy {
+execute(int a, int b)* int
}
class AddStrategy {
+execute(int a, int b) int
}
class ConcatStrategy {
+execute(int a, int b) int
}
class MaxStrategy {
+execute(int a, int b) int
}
Context o-- OperationStrategy
OperationStrategy <|.. AddStrategy
OperationStrategy <|.. ConcatStrategy
OperationStrategy <|.. MaxStrategy
```
Here is the matching class diagram. Create the matching classes in the matching files.
Here is the description :
* For `Context` :
* `operationStrategy` field should be initialized with a `AddStrategy`
* `changeStrategy` should update the `operationStrategy` field
* `execute` should return the result of `operationStrategy.execute`
* For `AddStrategy` :
* `execute` should return the sum of the two integers (e.g. a = 2, b = 4, then should return 6)
* For `ConcatStrategy` :
* `execute` should return the concatenation of the two integers (e.g. a = 2, b = 4, then should return 24)
* For `MaxStrategy` :
* `execute` should return the max of the two integers (e.g. a = 2, b = 4, then should return 4)
### Usage
Here is a possible ExerciseRunner.java to test your function :
```java
public class ExerciseRunner {
public static void main(String[] args) {
Context context = new Context();
System.out.println(context.execute(23, 43));
context.changeStrategy(new MaxStrategy());
System.out.println(context.execute(23, 43));
context.changeStrategy(new ConcatStrategy());
System.out.println(context.execute(23, 43));
}
}
```
and its output :
```shell
$ javac *.java -d build
$ java -cp build ExerciseRunner
66
43
2343
$
```
### Notions
[Class diagram](https://fr.wikipedia.org/wiki/Diagramme_de_classes)
Loading…
Cancel
Save