SOLID Principles Explained with Java Examples

SOLID principles are fundamental to object-oriented design. Here’s how to apply them in Java. S - Single Responsibility Principle Bad class User { public void save() { } public void sendEmail() { } public void generateReport() { } } Good class User { // Only user data } class UserRepository { public void save(User user) { } } class EmailService { public void sendEmail(User user) { } } O - Open/Closed Principle Bad class AreaCalculator { public double calculateArea(Object shape) { if (shape instanceof Circle) { // Calculate circle area } else if (shape instanceof Rectangle) { // Calculate rectangle area } } } Good interface Shape { double calculateArea(); } class Circle implements Shape { public double calculateArea() { return Math.PI * radius * radius; } } class Rectangle implements Shape { public double calculateArea() { return width * height; } } L - Liskov Substitution Principle Bad class Bird { public void fly() { } } class Penguin extends Bird { public void fly() { throw new UnsupportedOperationException(); } } Good interface Bird { void eat(); } interface FlyingBird extends Bird { void fly(); } class Sparrow implements FlyingBird { public void fly() { } } class Penguin implements Bird { // No fly method } I - Interface Segregation Principle Bad interface Worker { void work(); void eat(); void sleep(); } class Robot implements Worker { public void eat() { } // Not applicable public void sleep() { } // Not applicable } Good interface Workable { void work(); } interface Eatable { void eat(); } class Human implements Workable, Eatable { public void work() { } public void eat() { } } class Robot implements Workable { public void work() { } } D - Dependency Inversion Principle Bad class UserService { private MySQLDatabase database; public UserService() { this.database = new MySQLDatabase(); } } Good interface Database { void save(User user); } class UserService { private Database database; public UserService(Database database) { this.database = database; } } Conclusion SOLID principles help you write: ...

August 15, 2023 · 4126 views

Java Isn't Verbose - You Are: Writing Concise Java Code

Java is often criticized for verbosity, but modern Java can be concise. Here’s how to write cleaner code. Modern Java Features Records (Java 14+) // Old way public class User { private String name; private String email; public User(String name, String email) { this.name = name; this.email = email; } // Getters, equals, hashCode, toString... } // New way public record User(String name, String email) { } Pattern Matching (Java 17+) // Old way if (obj instanceof String) { String s = (String) obj; System.out.println(s.length()); } // New way if (obj instanceof String s) { System.out.println(s.length()); } Switch Expressions (Java 14+) // Old way String result; switch (day) { case MONDAY: case FRIDAY: result = "Weekday"; break; default: result = "Weekend"; } // New way String result = switch (day) { case MONDAY, FRIDAY -> "Weekday"; default -> "Weekend"; }; Best Practices Use Streams // Old way List<String> filtered = new ArrayList<>(); for (String name : names) { if (name.startsWith("J")) { filtered.add(name.toUpperCase()); } } // New way List<String> filtered = names.stream() .filter(name -> name.startsWith("J")) .map(String::toUpperCase) .toList(); Use Optional // Old way String result = null; if (user != null && user.getName() != null) { result = user.getName().toUpperCase(); } // New way String result = Optional.ofNullable(user) .map(User::getName) .map(String::toUpperCase) .orElse(null); Conclusion Modern Java is concise when you: ...

July 20, 2023 · 4015 views

Spring Boot Validation: Complete Guide with @Valid and @Validated

Validation is crucial in Spring Boot applications. Here’s a complete guide to using @Valid and @Validated. Basic Validation Dependencies <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> Bean Validation public class CreateUserRequest { @NotBlank(message = "Name is required") private String name; @Email(message = "Invalid email format") @NotBlank private String email; @Min(value = 18, message = "Age must be at least 18") @Max(value = 120, message = "Age must be at most 120") private Integer age; } @Valid vs @Validated @Valid @PostMapping("/users") public ResponseEntity<User> createUser(@Valid @RequestBody CreateUserRequest request) { // Validates request object } @Validated @RestController @Validated public class UserController { @GetMapping("/users/{id}") public User getUser(@PathVariable @Min(1) Long id) { // Validates path variable } } Custom Validators @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy = PhoneNumberValidator.class) public @interface PhoneNumber { String message() default "Invalid phone number"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; } public class PhoneNumberValidator implements ConstraintValidator<PhoneNumber, String> { @Override public boolean isValid(String value, ConstraintValidatorContext context) { return value != null && value.matches("^\\+?[1-9]\\d{1,14}$"); } } Validation Groups public interface CreateGroup {} public interface UpdateGroup {} public class UserRequest { @NotNull(groups = UpdateGroup.class) private Long id; @NotBlank(groups = {CreateGroup.class, UpdateGroup.class}) private String name; } @PostMapping("/users") public ResponseEntity<User> create(@Validated(CreateGroup.class) @RequestBody UserRequest request) { // Only validates CreateGroup fields } Error Handling @RestControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity<ErrorResponse> handleValidation( MethodArgumentNotValidException ex) { Map<String, String> errors = new HashMap<>(); ex.getBindingResult().getFieldErrors().forEach(error -> errors.put(error.getField(), error.getDefaultMessage()) ); return ResponseEntity.badRequest() .body(new ErrorResponse("Validation failed", errors)); } } Best Practices Validate at controller level Use appropriate annotations Create custom validators Handle validation errors Use validation groups Conclusion Spring Boot validation provides: ...

June 10, 2023 · 3818 views

Docker Best Practices: Building Efficient Images

Building efficient Docker images requires following best practices. Here’s how. 1. Use Multi-Stage Builds # Build stage FROM node:18 AS builder WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . RUN npm run build # Production stage FROM node:18-alpine WORKDIR /app COPY --from=builder /app/dist ./dist COPY --from=builder /app/node_modules ./node_modules CMD ["node", "dist/index.js"] 2. Layer Caching # Bad: Changes invalidate cache COPY . . RUN npm install # Good: Dependencies cached COPY package*.json ./ RUN npm install COPY . . 3. Use .dockerignore node_modules .git .env dist *.log 4. Minimize Layers # Bad: Multiple layers RUN apt-get update RUN apt-get install -y python RUN apt-get install -y git # Good: Single layer RUN apt-get update && \ apt-get install -y python git && \ rm -rf /var/lib/apt/lists/* 5. Use Specific Tags # Bad: Latest tag FROM node:latest # Good: Specific version FROM node:18.17.0-alpine Best Practices Multi-stage builds Optimize layer order Use .dockerignore Minimize image size Security scanning Conclusion Build efficient Docker images! 🐳

January 15, 2023 · 4187 views

Rust Error Handling: Complete Guide with Result and Option

Rust’s error handling is elegant and type-safe. Here’s how to use it effectively. Option Type fn find_user(id: u32) -> Option<String> { if id > 0 { Some(format!("User {}", id)) } else { None } } match find_user(1) { Some(name) => println!("Found: {}", name), None => println!("Not found"), } Result Type use std::fs::File; fn read_file(path: &str) -> Result<String, std::io::Error> { std::fs::read_to_string(path) } match read_file("data.txt") { Ok(content) => println!("Content: {}", content), Err(e) => println!("Error: {}", e), } Error Propagation fn read_config() -> Result<String, std::io::Error> { let content = std::fs::read_to_string("config.txt")?; Ok(content) } Custom Errors use std::fmt; #[derive(Debug)] struct CustomError { message: String, } impl fmt::Display for CustomError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.message) } } Best Practices Use Result for recoverable errors Use Option for optional values Propagate errors with ? Create custom errors when needed Handle errors explicitly Conclusion Handle errors elegantly in Rust! 🦀

April 15, 2022 · 3522 views

Migrating from Python 2 to Python 3: Complete Guide

Migrating from Python 2 to Python 3 requires careful planning. Here’s a step-by-step guide. Key Differences Print Statement # Python 2 print "Hello" # Python 3 print("Hello") Division # Python 2 5 / 2 # 2 # Python 3 5 / 2 # 2.5 5 // 2 # 2 Unicode # Python 2 s = u"Hello" # Python 3 s = "Hello" # Unicode by default Migration Tools # 2to3 tool 2to3 -w script.py # Modernize python-modernize script.py Best Practices Test thoroughly Update dependencies Use type hints Handle bytes/strings Update string formatting Conclusion Migrate to Python 3 for modern Python development! 🐍

December 15, 2021 · 3410 views