Fundamentals 13 min read

Java Design Patterns: Singleton, Observer, Decorator, Adapter, Factory, Abstract Factory, and Proxy

This article introduces several core Java design patterns—including Singleton (lazy, eager, static inner class, enum, double‑checked locking), Observer, Decorator, Adapter, Factory, Abstract Factory, and Proxy—explaining their intent, providing concise code examples, and showing how they solve common software design problems.

Java Captain
Java Captain
Java Captain
Java Design Patterns: Singleton, Observer, Decorator, Adapter, Factory, Abstract Factory, and Proxy

The article begins with a brief explanation of the Singleton pattern, describing how a class can ensure only one instance exists by making the constructor private and providing a getInstance() method. It presents multiple implementations: the basic lazy (non‑thread‑safe) version, a thread‑safe lazy version using synchronized, the eager ("hungry") version, a static inner‑class version, an enum‑based version, and the double‑checked locking version with a volatile field.

Next, the Observer pattern is introduced, illustrating a one‑to‑many dependency where observers are notified of state changes. The article provides a simple Java example with a Person interface, concrete observer classes ( LaoWang and LaoLi), a subject class ( XiaoMei) that maintains a list of observers, and a test class that demonstrates notification.

The Decorator pattern follows, using a sandwich‑making analogy. A base Food class is defined, and concrete decorators ( Bread, Cream, Vegetable) wrap a Food object, each adding its own description in the make() method. A test shows how nested decorators build a complete sandwich description.

The Adapter pattern is then explained with a voltage‑conversion analogy. A Phone class expects a specific voltage, and a VoltageAdapter class converts the standard 220 V to the required level. Sample code demonstrates setting the adapter on a phone and charging.

Factory patterns are covered next. The Simple Factory example defines an abstract Car product with run() and stop() methods, concrete products ( Benz, Ford), and a Factory class that creates instances based on a string key. The Factory Method example introduces a Moveable interface, concrete products ( Plane, Broom), an abstract VehicleFactory, and concrete factories ( PlaneFactory, BroomFactory) that instantiate the appropriate product.

The Abstract Factory pattern is presented with an AbstractFactory that can create Vehicle, Weapon, and Food objects. A DefaultFactory implements these methods, returning concrete Car, AK47, and Apple instances, and a test demonstrates usage.

Finally, the Proxy pattern is discussed using a wedding‑planning analogy. An interface ProxyInterface defines a marry() method. WeddingCompany implements the interface and acts as a static proxy, performing pre‑wedding preparations before delegating to a real NormalHome implementation that simply prints a marriage message. A test class shows how the proxy orchestrates the workflow.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Design PatternsJavaProxySingletonDecoratorFactoryObserver
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.