Design patterns are guidelines for solving repetitive problems.
Quote from Wikipedia:
Software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design.
Types of Design Patterns
- Generating Objects
- Patterns for Flexible Object Programming
- Performing and Representing Tasks
- Enterprise Patterns
- Database Patterns
The Singleton Pattern
The singleton pattern is a software design pattern that restricts the instantiation of a class to one object.
Purpose
A special class that generates one—and only one—object instance.
Example
Imagine a Settings class that holds application-level information.
- A Settings object should be available to any object in your system.
- A Settings object should not be stored in a global variable, which can be overwritten.
- There should be no more than one Preferences object in play in the system.
Implementation
Factory Method Pattern
Factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created.
Purpose
Building an inheritance hierarchy of creator classes.
Example
You need to create a variety of products of certain kinds.
Implementation
Abstract Factory Pattern
The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes.
Purpose
Grouping the creation of functionally related products
Example
You need to create a family of products of different types.
Implementation
Prototype
Prototype pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects.
Purpose
Using clone to generate objects.
Example
You need to create an object that is similar to an existing object.
Implementation
Service Locator
The service locator pattern is a design pattern used in software development to encapsulate the processes involved in obtaining a service with a strong abstraction layer.
Purpose
Asking your system for objects. To implement a loosely coupled architecture in order to get better testable, maintainable and extendable code.
Implementation
Dependency Injection
Dependency injection is a technique whereby one object (or static method) supplies the dependencies of another object.
Purpose
Letting your system give you objects. To implement a loosely coupled architecture in order to get better testable, maintainable and extendable code.
Implementation
Summary
This post covered some of the tricks that you can use to generate objects.
Marat Badykov
Full-stack web developer. I enjoy writing Php, Java, and Js.