Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solução de padrões de projeto #1

Open
Juses24 opened this issue Sep 26, 2023 · 0 comments
Open

Solução de padrões de projeto #1

Juses24 opened this issue Sep 26, 2023 · 0 comments

Comments

@Juses24
Copy link

Juses24 commented Sep 26, 2023

public class Singleton {
private static Singleton instance;

private Singleton() {
    // Construtor privado para impedir a criação de instâncias diretas.
}

public static Singleton getInstance() {
    if (instance == null) {
        instance = new Singleton();
    }
    return instance;
}

}
// A classe Singleton garante que apenas uma instancia seja criada e fornecerá essa instancia sempre que getInstance() for chamado.


public interface PaymentStrategy {
void pay(int amount);
}

public class CreditCardPayment implements PaymentStrategy {
private String cardNumber;

public CreditCardPayment(String cardNumber) {
    this.cardNumber = cardNumber;
}

@Override
public void pay(int amount) {
    System.out.println("Pagamento de $" + amount + " com cartão de crédito " + cardNumber);
}

}

public class PayPalPayment implements PaymentStrategy {
private String email;

public PayPalPayment(String email) {
    this.email = email;
}

@Override
public void pay(int amount) {
    // Lógica para pagamento com PayPal
    System.out.println("Pagamento de $" + amount + " com PayPal usando o email " + email);
}

}

// Interface para a estratégia
// Implementações das estratégias
// Lógica para pagamento com PayPal
// Aqui, temos uma interface PaymentStrategy que define o contrato para as estratégias de pagamento. Em seguida, implementamos duas estratégias diferentes: CreditCardPayment e PayPalPayment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant