0% found this document useful (0 votes)
43 views2 pages

Overview of Creational and Structural Patterns

Uploaded by

gocrkey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views2 pages

Overview of Creational and Structural Patterns

Uploaded by

gocrkey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Singleton is a creational design pattern that lets you ensure that a class has only one

instance, while providing a global access point to this instance.

Prototype is a creational design pattern that lets you copy existing objects without
making your code dependent on their classes.

Factory Method is a creational design pattern that provides an interface for creating
objects in a superclass, but allows subclasses to alter the type of objects that will be
created.

Abstract Factory is a creational design pattern that lets you produce families of related
objects without specifying their concrete classes.

Builder is a creational design pattern that lets you construct complex objects step by
step. The pattern allows you to produce different types and representations of an object
using the same construction code.

Adapter is a structural design pattern that allows objects with incompatible interfaces to
collaborate.

Facade is a structural design pattern that provides a simplified interface to a library, a


framework, or any other complex set of classes.
1. Singleton: Imagine a logging class in a system. You want only one instance of this class to
handle all logging throughout the application. The Singleton pattern ensures this by allowing
only one instance of the logger class to exist and provides a global access point to it. It's
useful for managing resources such as database connections, thread pools, or
configuration settings.

2. Prototype: Consider a graphic editor where users can create complex shapes. Instead of
defining every shape class beforehand, the Prototype pattern allows users to create new
shapes by cloning existing ones. For instance, a user might create a circle and then clone it
to create variations like ellipses or rings without being dependent on specific shape classes.

3. Factory Method: Suppose you're developing a game with different character classes (e.g.,
warrior, mage, archer), each with its unique abilities. The Factory Method pattern defines an
interface for creating characters in a superclass (e.g., CharacterFactory), but lets
subclasses (e.g., WarriorFactory, MageFactory) decide which specific character to
instantiate. This allows for easy extension when adding new character types.

4. Abstract Factory: Imagine a user interface toolkit that needs to support different operating
systems (e.g., Windows, macOS). The Abstract Factory pattern provides a way to create
families of related objects, such as buttons, windows, and menus, without specifying their
concrete classes. For example, a WindowsAbstractFactory would create Windows-specific
UI components, while a MacAbstractFactory would create macOS-specific UI components.

5. Builder: Consider a meal ordering system where customers can customize their orders with
various options (e.g., burger with different toppings, sides, and drinks). The Builder pattern
allows you to construct complex objects (e.g., meal orders) step by step, offering different
combinations of options using the same construction code. It's useful when dealing with
objects with many configuration options or when creating immutable objects.

6. Adapter: Suppose you have an old library that provides functionality through an outdated
interface, but you want to use it in a modern system with a different interface. The Adapter
pattern acts as a bridge between the old and new interfaces, allowing objects with
incompatible interfaces to collaborate seamlessly. For instance, you could create an
adapter that translates requests from the new interface to the format expected by the old
library.

7. Facade: Imagine you're working with a complex subsystem such as a computer's BIOS
settings. The Facade pattern provides a simplified interface to this subsystem, hiding its
complexity behind a single interface. For example, instead of directly interacting with low-
level BIOS commands, you can use a BIOSFacade that offers high-level methods for
common tasks like booting up or changing settings, making it easier to use and understand.

You might also like