Mastering Design Patterns: Factory, Adapter & Proxy
Bruno Silva
Senior Fullstack Backend Focused Engineer | Java | AWS | Spring boot | Kafka | Microservices | Webflux | Senior Java Developer | Hibernates | Kubernets | k8s | Scrum | Jira | GitLab
The Factory pattern provides a method for creating objects without specifying the exact class of the object that will be created. It’s perfect for when the object creation process is complex or varies.
Example: Imagine you have a game where players can choose different types of characters (Warrior, Mage, Archer). Instead of creating each character manually, a Factory method can handle the creation.
public interface Character {
void attack();
}
public class Warrior implements Character {
public void attack() {
System.out.println("Warrior attacks!");
}
}
public class CharacterFactory {
public Character createCharacter(String type) {
if (type.equals("Warrior")) {
return new Warrior();
}
// Add more character types as needed
return null;
}
}
// Usage
CharacterFactory factory = new CharacterFactory();
Character warrior = factory.createCharacter("Warrior");
warrior.attack(); // Output: Warrior attacks!
2?? Adapter Pattern – Making Incompatible Interfaces Work Together ??
The Adapter pattern allows you to make incompatible interfaces work together. It acts as a bridge that converts one interface into another that the client expects.
Example: Suppose you have a system that uses an old payment gateway, and you need to integrate a new payment API without modifying the old system. You can use an adapter.
// Old payment system
public class OldPaymentSystem {
public void makePayment() {
System.out.println("Payment processed with Old System");
}
}
// New payment system
public interface NewPaymentSystem {
void processPayment();
}
public class NewPaymentAdapter implements NewPaymentSystem {
private OldPaymentSystem oldSystem;
public NewPaymentAdapter(OldPaymentSystem oldSystem) {
this.oldSystem = oldSystem;
}
@Override
public void processPayment() {
oldSystem.makePayment(); // Adapting old system to new interface
}
}
// Usage
OldPaymentSystem oldSystem = new OldPaymentSystem();
NewPaymentSystem adapter = new NewPaymentAdapter(oldSystem);
adapter.processPayment(); // Output: Payment processed with Old System
领英推荐
3?? Proxy Pattern – Controlling Access to Objects ??
The Proxy pattern provides a surrogate or placeholder for another object. It controls access to the real object, which is useful for tasks like lazy loading, access control, or logging.
Example: Imagine you have an image loading system where large images are loaded only when needed. A Proxy object can control the access to the image.
// Real image class
public class RealImage {
private String filename;
public RealImage(String filename) {
this.filename = filename;
}
public void display() {
System.out.println("Displaying image: " + filename);
}
}
// Proxy class
public class ImageProxy {
private RealImage realImage;
private String filename;
public ImageProxy(String filename) {
this.filename = filename;
}
public void display() {
if (realImage == null) {
realImage = new RealImage(filename); // Lazy loading
}
realImage.display();
}
}
// Usage
ImageProxy proxy = new ImageProxy("image.jpg");
proxy.display(); // Output: Displaying image: image.jpg (loaded only when accessed)
These design patterns are incredibly powerful tools in software development. They simplify complex problems and make your code more flexible and maintainable. Start using them in your projects today! ???
#SoftwareDevelopment #DesignPatterns #FactoryPattern #AdapterPattern #ProxyPattern #CleanCode #Programming #Java #TechInnovation #Java #SoftwareEngineer
Software Engineer | Java | Spring Boot | Back-End | Microservices | Azure | Docker | CI/CD | Full Stack | React
1 周Bruno, thanks for sharing!
Data Engineer | Python | SQL | PySpark | Databricks | Azure Certified: 5x
3 周Love your take on this—thank you! ??
Lead Fullstack Engineer | Typescript Software Engineer | Nestjs | Nodejs | Reactjs | AWS
3 周Concise and well-explained examples of these classic design patterns! This is a great resource for understanding and applying them effectively.
Senior Mobile Developer | Android Software Engineer | Jetpack Compose | GraphQL | Kotlin | Java | React Native | Swift
3 周Its always great to learn more about design pattern, thanks for sharing!
Back End Engineer | Software Engineer | TypeScript | NodeJS | ReactJS | AWS | MERN | GraphQL | Jenkins | Docker
4 周Good content! Thanks for sharing ??