Deal with initializing and configuring classes and objects
- Encapsulates object creation logic
- If we are making specific class selection logic changes, we make them in one place
- We can hide complex object creation
- This is not a real Pattern
- This is the preparation for the real Pattern
- It is used quite often
- Each time we add new type we need to modify the simple factory code
Provides an interface for creating objects in superclass, but allow subclasses to alter the type of objects that will be created.
- Objects are created by separate method(s)
- Produces objects as normal factory
- Adds an interface to the simple factory
- Add new factories and classes without breaking Open/Closed Principle
Lets you produce families of related objects without specifying their concrete classes.
- Abstraction in object creation
- Create a family of related objects
- The Abstract Factory Pattern defines interface for creating sets of linked objects
- Without knowing their concrete classes
- Used in systems that are frequently changed
- Provides flexible mechanism for replacement of different sets
- Separates the construction of a complex object (logic) from its representation (data) so that the same construction process can create different representations
- Separation of logic (multistep) and data
- Encapsulates the way an object is constructed
- Solves 3 problems
- Too many parameters
- Order dependent
- Different constructions
- Example: Building a XML document StringBuilder is not using the builder pattern
- Builder is used by Director
- Builder is implemented by a concrete builder
- Product is produced by the concrete builder
- The client uses director and concrete builder to produce product
- Factory Method
- Used when the factory can easily create the entire object within one method call
- Common interface to group factories
- Abstract Factory
- Interface for creating families of related or dependent objects
- Builder
- When you need a lot of things to build an object
- When construction is order-dependent
- The Singleton class is a class that is supposed to have only one (single) instance
- Access window manager / file system / console
- Access global application logger / DC / Mapper
- Sometimes Singleton is wrongly thought of as a global variable โ it is not!
- Possible problems:
- Lazy loading (created when first needed)
- Thread-safe
- Benefits
- It eliminates the (potentially expensive) overhead of initializing (construction) an object
- Hides the complexities of making new instances from the client
- Examples
- Copy of web resource (instead of downloading it every time it is needed)
- Getting copy of the current state of an object
- Hiding constructors and allowing cloning
- JavaScript is a prototypical language
- An implementation of an object-oriented API that aims to provide more readable code
- Reduce syntactical noise
- More clearly express what the code does
- Implemented by using method cascading
- Real world examples:
- iostream library in C++
- cout << "text" << 5;
- KendoUI Wrappers
- LINQ in .NET
- Using extension methods
- Describe ways to assemble objects to implement a new functionality
- Ease the design by identifying a simple way to realize relationships between entities
- These design patterns are all about class and object composition
- Structural class-creation patterns use inheritance to compose interfaces
- Structural object-patterns define ways to compose objects to obtain new functionality
- An object that provides a simplified interface to a larger body of code, such as class library
- Make a software library easier to use, understand and more readable
- Reduce dependencies of outside code
- Keeps the Principle of least knowledge
- Wrap a poorly designed APIs in a better one
- Composite Pattern allows to combine different types of objects in tree structures
- Gives the possibility to treat the same individual objects or groups of objects
- Used when
- We have different objects and we want to treat them the same way
- We want to present hierarchy of objects
- Tree-like structures
- Examples in .NET Framework
- Windows.Forms.Control and its derived classes
- System.Web.UI.Control and its derived classes
- System.Xml.XmlNode and its derived classes