Share
Embed
Transcript
25 slides
-
HI, I’M ANMOLDEEP! Just a curious dev on his adventure
to explore the universe with bits (quite literally) I travel through space time with my Pals - Salesforce, Python, PHP, and NodeJS Salesforce Developer | AI Generalist | System Design Learner Let’s Connect https://www.linkedin.com/in/eduanmoldeep/ -
3.5 Hour Workshop Java Masterclass Syntax to Architecture Moving beyond
syntax: Deep Internals, Modern Patterns, and Production-Grade Spring Boot. -
The Interface Contract Think of an Interface as a standard
wall socket. The house wiring doesn't care what you plug in, as long as it fits the socket. The Wall Socket Interface): Defines the shape and voltage The Contract). The Device Implementation): A lamp, a laptop, or a vacuum can plug in The Implementation). -
Coupling: The Silent Killer Tight Coupling Bad If you want
to switch to a LinkedList for performance later, you have to rewrite the Service code. Loose Coupling Good You can swap new ArrayList() with new LinkedList() or new Vector() without breaking the rest of the code. public class // Hard dependency on ArrayList private new public class // Coding to the Interface private new -
Deep Dive: Why Abstraction? Swapability Easily switch database implementations MySQL
to MongoDB) if your Service only talks to a generic Repository interface. Testability Interfaces allow you to inject "Mock" objects during testing. You can't mock a tightly coupled new object easily. Maintainability Changes in the implementation details don't ripple out and break other parts of the system. -
Memory Layout: Lists ArrayList Array Backed) Contiguous memory block. Fast
access O1, slow resizing. LinkedList Node Backed) Scattered memory with pointers. Slow access O(n), fast insertion. -
The Contract Why do HashSets sometimes contain duplicates? Because you
broke the contract. • Rule 1 If a.equals(b) is true, then a.hashCode() MUST be equal to b.hashCode(). • Rule 2 If hashCodes are different, the objects are definitely different. • The Bug: If you override equals but not hashCode, your object gets lost in the HashMap buckets. -
Exceptions: The Debate Checked Exceptions // Must be caught or
declared Use Case: Recoverable errors. "The file is missing, ask user for a new path." Pros: Forces developers to handle failure. Cons: Can clutter code Boilerplate). Unchecked Runtime) // Silent failure unless caught Use Case: Logic errors. "NullPointerException", "IndexOutOfBounds". Modern Trend: Spring and modern frameworks prefer Unchecked exceptions to keep code clean. -
Pattern: Try-With-Resources The Old Way Risky The Modern Way Safe
Works with any class implementing AutoCloseable. new "path" try // usage finally if null // What if close() throws? // Auto-closes even if error occurs try new "path" // usage -
Break Time Take 30 minutes to recharge. We resume with
Streams & Spring. -
The Pipeline Visualization Don't think of "loops". Think of a
factory assembly line. 1. Source: The raw materials List, Set). 2. Intermediate Ops: The machines Filter, Map, Sort). These are lazy. Nothing happens until... 3 . Terminal Op: The packaging Collect, Count, ForEach). This triggers the flow. -
Code Evolution Imperative How Declarative What Reads like a sentence.
"Filter active users, map to names, collect to list." new for if -
Deep Dive: map() vs flatMap() map() One-to-One Transformation. User, User]
→ String, String] Input: Stream Output: Stream flatMap() One-to-Many Flattening). A, B, C → A, B, C Use Case: You have a List of Orders, and each Order has a List of Items. You want one big stream of all Items. -
The End of NullPointerExceptions Optional is a container object which
may or may not contain a non-null value. // Old Way if null return return "Unknown" // Optional Way return "Unknown" -
Separation of Concerns This separation allows you to change the
database without touching the Controller, or change the API format without touching the Database. • Controller: Handling HTTP Req/Res). NO business logic here. • Service: The Brains. Transactional business logic. • Repository: Data Access. Talking to DB. -
Pattern: DTO Data Transfer Object) The Problem Exposing your Database
Entity (e.g., User with password field) directly to the API consumer is a security risk and creates tight coupling. The Solution Create a separate POJO (e.g., UserDTO) that contains only the fields you want to show. DTO Entit y -
Deep Implementation: ControllerAdvice Avoid Try-Catch in Controllers Don't clutter every
controller method with try-catch blocks. Use AOP Aspect Oriented Programming) to handle errors globally. @RestControllerAdvice public class @ExceptionHandler public return "Not Found" -
DI Best Practices Field Injection Autowired private Service s; Avoid.
Hides dependencies, hard to test NullPointer in unit tests). Setter Injection Okay. Good for optional dependencies, but allows mutable state. Constructor Injection Best. Ensures object is fully initialized. Easy to pass Mocks in tests.