terminal

codeando_simple

terminal

menu

terminal

search_module

guest@codeandosimple: ~/system/search $ grep -r "" .

Press [ENTER] to execute search

Status

Engine: Ready

Database: Online

Index: V2.1.0_LATEST

bash -- cat oop-interfaces.md
guest@codeandosimple: ~/blog/oop $ cat interfaces.md

Interfaces_

// "Every achievement starts with the decision to try" - Gail Devers

Interfaces are a fundamental element in object-oriented programming. They facilitate the separation between the definition of tasks and their concrete implementation.

This chapter focuses on explaining what interfaces are, how they work, and why they are a valuable tool in robust and flexible software design.

# What are Interfaces?

An interface is a file that defines operations (and constant properties) without implementing any (there are languages today that do allow creating default implementations, but that escapes the core concept of an interface). 

It defines a contract that concrete classes implementing it must follow, specifying what should be done but not how it should be done.

At first, it might not seem sense, adding one more layer? that does nothing? what for?... But what is not seen is the potential it offers: interfaces are the communication points between layers... they allow an object to take the form of the interface... invert dependencies, isolate parts, make it extensible... it is what allows designing and building the architecture of a good system that is easy to maintain and grow. Here lies the importance of interfaces, pure polymorphism.

# UML

In UML, the text <<interface>> is usually added above the name. The name of the class and the methods are also written in italics.

In the case of Shape, our design could also see it as an interface that declares the draw() method so that each class implementing it redefines it.

interfaces

# Definition of Interfaces

Interfaces are defined using the interface keyword and have all their methods without implementation. 

public interface Payable {
    void pay(double amount);
}

In this example, any class that implements Payable must provide an implementation for the pay method.

# Implementing Multiple Interfaces

Unlike inheritance, Java allows a class to implement multiple interfaces, providing a kind of multiple inheritance, taking various forms.

This allows a class to have several interface contracts.

We define an Identifiable interface:

public interface Identifiable {
    String getId();
}

We use two interfaces in this class, one to get the id, another to pay:

public class Order implements Payable, Identifiable {
    // Implementation of both methods
    @Override
    public void pay(double amount) {
        // Payment logic
    }

    @Override
    public String getId() {
        // Logic to get ID
        return "12345";
    }
}

The remarkable thing about this is that an object of type Order can be treated as an object of type Order, Payable, or Identifiable.

# Example: E-commerce Payment Systems

Consider an e-commerce platform that needs to handle different types of payments, such as credit card, PayPal, and coupons.

We can define a PaymentSystem interface and then implement this interface for each type of payment system.

We define the Payment System Interface:

public interface PaymentSystem {
    void processPayment(double amount);
}

We implement the interface for the 3 concrete payment types:

public class CreditCardPayment implements PaymentSystem {
    @Override
    public void processPayment(double amount) {
        System.out.println("CC Payment for: $" + amount);
    }
}

public class PayPalPayment implements PaymentSystem {
    @Override
    public void processPayment(double amount) {
        System.out.println("PayPal Payment for: $" + amount);
    }
}

public class CouponPayment implements PaymentSystem {
    @Override
    public void processPayment(double amount) {
        System.out.println("Applying coupon of: $" + amount);
    }
}

We create the shopping cart that uses the payment system:

public class ShoppingCart {
    public void processPurchase(PaymentSystem paymentSystem, double amount) {
        paymentSystem.processPayment(amount);
    }
}

We use the interface and the shopping cart (could be in the main method):

// In the main method or any other test method
ShoppingCart cart = new ShoppingCart();
PaymentSystem card = new CreditCardPayment();
PaymentSystem paypal = new PayPalPayment();

cart.processPurchase(card, 100.0);
cart.processPurchase(paypal, 75.0);

# Conclusions

Interfaces are a powerful tool that facilitates the separation of definition and implementation, allowing for a more flexible and maintainable design.

By defining clear contracts through interfaces and allowing classes to implement multiple interfaces, one can build complex and robust systems with interchangeable and easily extensible components. They are a fundamental tool in system design and architecture and are the point of communication with other layers.

Understanding and using interfaces is essential for any programmer seeking to write clean and modular code.