Skip to content

Commit

Permalink
Alterada organização dos arquivos GoF
Browse files Browse the repository at this point in the history
  • Loading branch information
ArturRSA19 committed Jul 26, 2024
1 parent 73d7c36 commit a3e05bb
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,76 +59,74 @@ 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();
}

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

public void removeItem(String item) {
System.out.println("Removed " + item + " from 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");
}
}
}

// 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

0 comments on commit a3e05bb

Please sign in to comment.