Skip to content

Business Concept

This content is for Java. Switch to the latest version for up-to-date documentation.

Business concepts in Java programming refer to how you model real-world business entities, processes, and rules in your applications.

Domain-Driven Design is an approach to software development that focuses on understanding the business domain and creating a model that reflects it.

  • Entities: Objects with a distinct identity (e.g., Customer, Order)
  • Value Objects: Objects defined by their attributes (e.g., Address, Money)
  • Aggregates: Clusters of entities and value objects treated as a unit
  • Repositories: Objects that handle persistence of aggregates
  • Services: Business operations that don’t naturally belong to entities
// Entity example
public class Customer {
private UUID id;
private String name;
private String email;
private List<Order> orders;
// Constructor, getters, setters, business methods
}
// Value object example
public class Address {
private final String street;
private final String city;
private final String zipCode;
// Immutable, equals/hashCode based on all fields
}
public class OrderService {
private OrderRepository orderRepository;
private CustomerRepository customerRepository;
private PaymentGateway paymentGateway;
public Order placeOrder(UUID customerId, List<OrderItem> items, PaymentDetails payment) {
// Business logic for placing an order
Customer customer = customerRepository.findById(customerId);
Order order = new Order(customer, items);
boolean paymentSuccessful = paymentGateway.processPayment(payment, order.getTotalAmount());
if (paymentSuccessful) {
order.setStatus(OrderStatus.PAID);
orderRepository.save(order);
return order;
} else {
throw new PaymentFailedException("Payment could not be processed");
}
}
}

Business rules can be implemented in various ways:

public class Order {
// Properties
public void addItem(Product product, int quantity) {
// Business rule: Cannot add out-of-stock items
if (!product.isInStock(quantity)) {
throw new InsufficientStockException();
}
OrderItem item = new OrderItem(product, quantity);
items.add(item);
recalculateTotal();
}
}
public interface OrderSpecification {
boolean isSatisfiedBy(Order order);
}
public class CanBeShippedSpecification implements OrderSpecification {
public boolean isSatisfiedBy(Order order) {
return order.getStatus() == OrderStatus.PAID &&
order.getItems().stream().allMatch(item -> item.getProduct().isInStock(item.getQuantity()));
}
}

For simple applications, business logic can be organized in transaction scripts:

public class InvoiceService {
private final DataSource dataSource;
public Invoice generateMonthlyInvoice(int customerId, int month, int year) {
try (Connection conn = dataSource.getConnection()) {
// Get customer data
Customer customer = getCustomer(conn, customerId);
// Get usage data for billing period
List<UsageRecord> usageRecords = getUsageRecords(conn, customerId, month, year);
// Calculate charges
BigDecimal total = calculateCharges(usageRecords, customer.getPricingPlan());
// Create invoice
Invoice invoice = new Invoice(customer, month, year, total);
// Save invoice
saveInvoice(conn, invoice);
return invoice;
} catch (SQLException e) {
throw new InvoiceGenerationException("Failed to generate invoice", e);
}
}
// Helper methods...
}
  1. Separate business logic from infrastructure concerns
  2. Make business rules explicit rather than implicit
  3. Use a ubiquitous language shared by developers and domain experts
  4. Keep business logic testable with unit tests
  5. Design for change as business rules frequently evolve
  6. Document business rules with comments and documentation
Built with passion by Ngineer Lab