Understanding Factory Patterns, Dynamic Proxy, and XML in Java

Factory Patterns

Design patterns in software engineering represent proven solutions to common design problems. They enhance code reusability, readability, and maintainability by providing standardized approaches to structuring code.

Simple Factory Pattern

The simple factory pattern encapsulates object creation logic within a dedicated factory class. Instead of instantiating objects directly using new, clients request objects from the factory, which handles their instantiation.

abstract class Vehicle {
    protected String model;
    protected String color;
    protected double price;

    public abstract void start();

    public Vehicle(String model, String color, double price) {
        this.model = model;
        this.color = color;
        this.price = price;
    }

    public String getModel() { return model; }
    public void setModel(String model) { this.model = model; }
    public String getColor() { return color; }
    public void setColor(String color) { this.color = color; }
    public double getPrice() { return price; }
    public void setPrice(double price) { this.price = price; }
}
class LuxuryCar extends Vehicle {
    public LuxuryCar() {
        super("Luxury Model", "Silver", 80000);
    }

    @Override
    public void start() {
        System.out.println("Starting luxury car: " + model + " in " + color);
    }
}
class SportsCar extends Vehicle {
    public SportsCar() {
        super("Sports Model", "Red", 60000);
    }

    @Override
    public void start() {
        System.out.println("Starting sports car: " + model + " in " + color);
    }
}
public class VehicleFactory {
    public static Vehicle createVehicle(String type) {
        switch (type.toLowerCase()) {
            case "luxury":
                return new LuxuryCar();
            case "sports":
                return new SportsCar();
            default:
                throw new IllegalArgumentException("Unknown vehicle type");
        }
    }
}

Usage example:

public class TestFactory {
    public static void main(String[] args) {
        Vehicle luxury = VehicleFactory.createVehicle("luxury");
        luxury.start();
        
        Vehicle sport = VehicleFactory.createVehicle("sports");
        sport.start();
    }
}

Dynamic Proxy

Dynamic proxies enable the creation of proxy objects at runtime. These are particularly useful for implementing cross-cutting concerns like logging, security checks, or transaction management without modifying the core business logic.

XML Basics

XML (eXtensible Markup Language) is a markup language designed for storing and transporting data. It allows defining custom tags and provides a structured way to represent hierarchical information.

Basic Syntax

  • XML documents begin with an encoding declaration: <?xml version="1.0" encoding="UTF-8"?>
  • There must be exactly one root element
  • All elements must be properly nested and closed
  • Attributes must be quoted
  • Element names are case-sensitive

Components of XML

  1. Declaration: Defines version and encoding
  2. Tags: Custom-defined elements
  3. Attributes: Key-value pairs within opening tags
  4. Text Content: Data between start and end tags
  5. Commments: Enclosed in <!-- comment -->

XML Validation

XML validation ensures document integrity through constraints defined in DTD or Schema files.

DTD Example

<!ELEMENT catalog (book+)>
<!ELEMENT book (title,author)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ATTLIST book id ID #REQUIRED>

Schema Example

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="catalog">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="book" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="title" type="xs:string"/>
              <xs:element name="author" type="xs:string"/>
            </xs:sequence>
            <xs:attribute name="id" type="xs:string" use="required"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Parsing XML

Libraries like DOM4J and XPath facilitate parsing and querying XML structures programmatically.

Tags: java factory-pattern dynamic-proxy XML design-patterns

Posted on Sat, 16 May 2026 04:30:14 +0000 by robburne