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.
A special class that generates one—and only one—object instance.
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.
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.
Building an inheritance hierarchy of creator classes.
You need to create a variety of products of certain kinds.
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.
Grouping the creation of functionally related products
You need to create a family of products of different types.
Prototype pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects.
Using clone to generate objects.
You need to create an object that is similar to an existing object.
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.
Asking your system for objects. To implement a loosely coupled architecture in order to get better testable, maintainable and extendable code.
Dependency injection is a technique whereby one object (or static method) supplies the dependencies of another object.
Letting your system give you objects. To implement a loosely coupled architecture in order to get better testable, maintainable and extendable code.
This post covered some of the tricks that you can use to generate objects.
Full-stack web developer. I enjoy writing Php, Java, and Js.