Skip to content

Commit

Permalink
corrigindo estilo
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhermeSoaress committed Jul 26, 2024
1 parent 88f0dbb commit ed62a72
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 119 deletions.
102 changes: 52 additions & 50 deletions docs/PadroesDeProjeto/GoF's/Comportamentais/3.3.3.Command.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,74 +59,76 @@ OrderProcessor (Classe)
Este diagrama ilustra como o padrão Command separa a solicitação de uma ação do objeto que executa a ação, permitindo uma maior flexibilidade e reutilização de código.

### 3.2. Código
```java

// Command Interface
interface Command {
void execute();
}
// Command Interface
interface Command {
void execute();
}

// Receiver
class Cart {
public void addItem(String item) {
System.out.println("Added " + item + " to cart");
}
// Receiver
class Cart {
public void addItem(String item) {
System.out.println("Added " + item + " to cart");
}

public void removeItem(String item) {
System.out.println("Removed " + item + " from cart");
}
public void removeItem(String item) {
System.out.println("Removed " + item + " from cart");
}
}

// Concrete Commands
class AddItemCommand implements Command {
private Cart cart;
private String item;
// Concrete Commands
class AddItemCommand implements Command {
private Cart cart;
private String item;

public AddItemCommand(Cart cart, String item) {
this.cart = cart;
this.item = item;
}
public AddItemCommand(Cart cart, String item) {
this.cart = cart;
this.item = item;
}

@Override
public void execute() {
cart.addItem(item);
}
@Override
public void execute() {
cart.addItem(item);
}
}

class RemoveItemCommand implements Command {
private Cart cart;
private String item;
class RemoveItemCommand implements Command {
private Cart cart;
private String item;

public RemoveItemCommand(Cart cart, String item) {
this.cart = cart;
this.item = item;
}
public RemoveItemCommand(Cart cart, String item) {
this.cart = cart;
this.item = item;
}

@Override
public void execute() {
cart.removeItem(item);
}
@Override
public void execute() {
cart.removeItem(item);
}
}

// Invoker
class OrderProcessor {
public void execute(Command command) {
command.execute();
}
// Invoker
class OrderProcessor {
public void execute(Command command) {
command.execute();
}
}

// Uso do padrão
public class Main {
public static void main(String[] args) {
Cart cart = new Cart();
OrderProcessor orderProcessor = new OrderProcessor();
// Uso do padrão
public class Main {
public static void main(String[] args) {
Cart cart = new Cart();
OrderProcessor orderProcessor = new OrderProcessor();

Command addItemCommand = new AddItemCommand(cart, "Product 1");
Command removeItemCommand = new RemoveItemCommand(cart, "Product 1");
Command addItemCommand = new AddItemCommand(cart, "Product 1");
Command removeItemCommand = new RemoveItemCommand(cart, "Product 1");

orderProcessor.execute(addItemCommand);
orderProcessor.execute(removeItemCommand);
}
orderProcessor.execute(addItemCommand);
orderProcessor.execute(removeItemCommand);
}
}
```

## Referências

Expand Down
89 changes: 46 additions & 43 deletions docs/PadroesDeProjeto/GoF's/Criacionais/3.1.3.1.Prototype1.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,47 @@ Modelagem utilizando a ferramenta online [Miro](https://miro.com/app/board/).

### 3.2. Código

import java.util.HashMap;
import java.util.Map;

// Prototype Interface
interface Prototype<T> {
T clone();
}

// Product class implementing Prototype
class Product implements Prototype<Product> {
private String name;
private double price;

public Product(String name, double price) {
this.name = name;
this.price = price;
}

public String getName() {
return name;
}

public double getPrice() {
return price;
}

@Override
public Product clone() {
return new Product(this.name, this.price);
}

@Override
public String toString() {
return "Product{name='" + name + "', price=" + price + "}";
}
```java

import java.util.HashMap;
import java.util.Map;

// Prototype Interface
interface Prototype<T> {
T clone();
}

// Product class implementing Prototype
class Product implements Prototype<Product> {
private String name;
private double price;

public Product(String name, double price) {
this.name = name;
this.price = price;
}

public String getName() {
return name;
}

public double getPrice() {
return price;
}

@Override
public Product clone() {
return new Product(this.name, this.price);
}

@Override
public String toString() {
return "Product{name='" + name + "', price=" + price + "}";
}
}

// ProductRegistry to manage prototypes
class ProductRegistry {
// ProductRegistry to manage prototypes
class ProductRegistry {
private Map<String, Product> prototypes = new HashMap<>();

public void addPrototype(String key, Product prototype) {
Expand All @@ -71,13 +73,13 @@ Modelagem utilizando a ferramenta online [Miro](https://miro.com/app/board/).
} else {
throw new IllegalArgumentException("Prototype not found for key: " + key);
}
}
}
}

// Uso do padrão Prototype
public class Main {
public static void main(String[] args) {
ProductRegistry registry = new ProductRegistry();
// Uso do padrão Prototype
public class Main {
public static void main(String[] args) {
ProductRegistry registry = new ProductRegistry();

// Adicionando protótipos ao registro
Product bread = new Product("Bread", 2.50);
Expand All @@ -93,8 +95,9 @@ Modelagem utilizando a ferramenta online [Miro](https://miro.com/app/board/).
System.out.println(breadClone1);
System.out.println(breadClone2);
System.out.println(milkClone1);
}
}
}
```

## Referências

Expand Down
54 changes: 28 additions & 26 deletions docs/PadroesDeProjeto/GoF's/Estruturais/3.2.3.Adapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,41 +46,43 @@ Este diagrama ilustra como o padrão Adapter permite que uma classe existente (T

### 3.2. Código

// Adapter
class ThirdPartyPayment {
public void pay(int amount) {
System.out.println("Paid " + amount + " using third-party payment service");
}
```java
// Adapter
class ThirdPartyPayment {
public void pay(int amount) {
System.out.println("Paid " + amount + " using third-party payment service");
}
}

// Target
interface Payment {
void makePayment(int amount);
}
// Target
interface Payment {
void makePayment(int amount);
}

// Adapter
class PaymentAdapter implements Payment {
private ThirdPartyPayment thirdPartyPayment;
// Adapter
class PaymentAdapter implements Payment {
private ThirdPartyPayment thirdPartyPayment;

public PaymentAdapter(ThirdPartyPayment thirdPartyPayment) {
this.thirdPartyPayment = thirdPartyPayment;
}
public PaymentAdapter(ThirdPartyPayment thirdPartyPayment) {
this.thirdPartyPayment = thirdPartyPayment;
}

@Override
public void makePayment(int amount) {
thirdPartyPayment.pay(amount);
}
@Override
public void makePayment(int amount) {
thirdPartyPayment.pay(amount);
}
}

// Uso do padrão
public class Main {
public static void main(String[] args) {
ThirdPartyPayment thirdPartyPayment = new ThirdPartyPayment();
Payment payment = new PaymentAdapter(thirdPartyPayment);
// Uso do padrão
public class Main {
public static void main(String[] args) {
ThirdPartyPayment thirdPartyPayment = new ThirdPartyPayment();
Payment payment = new PaymentAdapter(thirdPartyPayment);

payment.makePayment(100);
}
payment.makePayment(100);
}
}
```

## Referências

Expand Down

0 comments on commit ed62a72

Please sign in to comment.