
Structural and Behavioral Design Patterns
Design patterns are proven solutions to recurring software‑design problems. They help developers write code that is maintainable , flexible , and easy to understand . This course focuses on…

In the provided BankAdapter example, what is the role of the BankAdapter class?
Which structural pattern is most appropriate for representing a file system hierarchy?
What is a key drawback of using many Adapter classes in a system?
In the Observer pattern, what problem does the "information pulling" variant aim to solve?
Which structural pattern explicitly separates abstraction from implementation, allowing both to vary independently?
When applying the Flyweight pattern, which condition must be true for it to be beneficial?
In the Maven build lifecycle, which phase directly follows "compile"?
What is the primary purpose of a Facade in a complex system?
Which behavioural pattern encapsulates a request as an object, enabling undo operations?
In the Observer UML diagram, which method is responsible for notifying all registered observers?
When would you choose the Proxy pattern over the Adapter pattern?
What is the effect of applying the Decorator pattern to a UI component in JavaFX?
Which Maven element uniquely identifies a project across all repositories?
In the Observer pattern example with JavaFX sliders, why is the drawCircle() method not called manually?
Understanding Structural and Behavioral Design Patterns
Design patterns are proven solutions to recurring software‑design problems. They help developers write code that is maintainable, flexible, and easy to understand. This course focuses on the most common structural patterns—Adapter, Composite, Bridge, and Flyweight—as well as the behavioral pattern Observer, with a special look at its "information pulling" variant. By the end of the lesson you will be able to decide when to apply each pattern, recognize their trade‑offs, and avoid common pitfalls.
When to Use an Adapter
The Adapter pattern lets two incompatible interfaces work together without changing the existing code. It is especially useful when you are dealing with third‑party libraries or legacy systems that you cannot modify.
- Prefer Adapter when the existing class cannot be changed because it is third‑party code.
- Do not use Adapter simply to add new methods; that is a sign you should extend or compose instead.
- If the class already implements the required interface, an Adapter adds unnecessary indirection.
In a typical BankAdapter example, the adapter implements a new PaymentProcessor interface and internally delegates calls to the legacy LegacyBankGateway. This keeps the rest of your application unaware of the legacy API while preserving the original gateway's functionality.
Composite Pattern for Hierarchical Structures
When you need to treat individual objects and compositions of objects uniformly, the Composite pattern is the answer. A classic scenario is a file‑system hierarchy where both files and folders share common operations such as getSize() or display().
- Composite treats files and folders uniformly, allowing client code to work with a single abstract component.
- It simplifies client logic: you can call
component.print()without checking whether the component is a leaf or a container. - Be aware that deep hierarchies can increase memory usage; use lazy loading if necessary.
Bridge Pattern – Decoupling Abstraction from Implementation
The Bridge pattern separates an abstraction from its implementation so that both can evolve independently. This is crucial when you have multiple dimensions of variation, such as different UI themes (abstraction) and multiple rendering engines (implementation).
- Bridge decouples abstraction and implementation hierarchies, enabling you to add new abstractions or implementations without affecting the other side.
- It is often confused with Adapter, but the intent differs: Bridge is about designing two parallel hierarchies, while Adapter is about making existing interfaces compatible.
Flyweight Pattern – Sharing Intrinsic State
Flyweight reduces memory consumption by sharing common, immutable data (the intrinsic state) among many objects. It is most beneficial when you have a large number of similar objects that differ only in a few extrinsic attributes.
- Flyweight works best when a large number of similar objects share common intrinsic state.
- Objects should be immutable regarding the shared state; otherwise, changes could affect all clients.
- Typical use cases include character rendering in a text editor, particle systems in games, or database connection pools.
Observer Pattern and the Information Pulling Variant
The Observer pattern establishes a one‑to‑many dependency so that when the subject changes, all its observers are notified. However, sending notifications indiscriminately can lead to wasted processing. The "information pulling" variant solves this by letting observers request data only when they actually need it.
- It avoids unnecessary notifications when the subject's state hasn't changed.
- Observers pull the required data after receiving a lightweight signal, reducing network traffic and CPU usage.
- Think of a newspaper subscription (push) versus buying a single article online (pull).
Key Takeaways
- The “information pulling” variant lets observers ask for data only when they need it.
- It prevents the subject from sending updates that the observer may not actually use.
- Thus, it avoids unnecessary notifications when the subject’s state hasn’t changed.
How to Remember
- Mnemonic: Pull = Prevent Unwanted Light (P‑U‑L) – pulling data stops “unwanted” notifications.
- Tip: Compare a newspaper subscription (push) with buying a single article (pull); pulling means you only get what matters.
Trade‑offs of Using Many Adapters
While adapters are powerful, overusing them can increase system complexity. Each new adapter introduces an additional interface and class, which can make the codebase harder to navigate.
- Overall code complexity rises due to added interfaces and classes.
- Performance impact is usually minimal, but excessive indirection may affect latency in high‑throughput systems.
- Maintainability suffers if adapters are not well‑documented or if their purpose overlaps.
Practical Example: Maven Build Lifecycle
Understanding build tools is essential for applying design patterns in real projects. In Maven, the lifecycle phases are ordered to ensure a smooth build process.
- After the
compilephase, Maven executes the test phase, which runs unit tests on the compiled code. - Subsequent phases include
package(creates JAR/WAR),verify,install, anddeploy.
Summary Checklist
- Use Adapter when you cannot modify a third‑party class.
- Apply Composite for tree‑like structures such as file systems.
- Choose Bridge to separate abstraction from implementation.
- Implement Flyweight when many objects share immutable intrinsic state.
- Prefer the information pulling variant of Observer to reduce unnecessary updates.
- Avoid proliferating adapters; keep the architecture clean.
- Remember Maven’s
testphase followscompile.
