Design Patterns in Angular

DevCom — We do IT together
7 min readJun 23, 2022

--

There are many opinions that front-end programming patterns should not be used or existing patterns should not be used. In fact, programming patterns often help solve some specific issues and make it easier to maintain the code as a whole.

What is the design pattern and what are they needed for?

Design patterns are programming language independent strategies for solving a common problem. That means a design pattern represents an idea, not a particular implementation. Design patterns are mainly used to solve not a specific issue, but to indicate the correct way to solve a general problem.

By using design patterns, you can make your code more flexible, reusable, and maintainable.

Programming patterns are divided into three main types:

  • Creational Design Patterns

Creational design patterns focus on handling object creation mechanisms where objects are created in a manner suitable for the situation we’re working in.

  • Structural Design Patterns

Structural design patterns explain how to assemble objects and classes into larger structures while keeping these structures flexible and efficient.

  • Behavioral Design Patterns

Behavioral patterns focus on improving or streamlining the communication between disparate objects in a system.

Creational Design Patterns

Creational design patterns provide various object creation mechanisms, increasing flexibility and reusing existing code.

Singleton

One of the most popular creational design patterns is Singleton which lets you ensure that a class has only one instance while providing a global access point to this instance. This design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

The singleton design pattern solves problems by allowing it to:

-Ensure that a class only has one instance.

-Easily access the sole instance of a class.

-Control its instantiation.

Compared to an object that is explicitly created at one point in the client code, a singleton is easier to access from multiple classes, packages, modules etc.

So Singleton is more flexible than static classes and can maintain state. If your code has access to the Singleton class, then it’s able to call the Singleton’s static method. So whenever that method is called, the same object is always returned.

  • Use the Singleton pattern when a class in your program should have just a single instance available to all clients; for example, a single database object shared by different parts of the program.
  • Use the Singleton pattern when you need stricter control over global variables.

Abstract Factory pattern

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

Abstract Factory patterns work around a super-factory which creates other factories. This design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

This pattern provides you with an interface for creating objects from each class of the product family. As long as your code creates objects via this interface, you don’t have to worry about creating the wrong variant of a product that doesn’t match the products already created by your app.

  • Use the Abstract Factory when your code needs to work with various families of related products, but you don’t want it to depend on the concrete classes of those products — they might be unknown beforehand or you simply want to allow for future extensibility.

The purpose of the Abstract Factory is to provide an interface for creating families of related objects, without specifying concrete classes.

Factory Method

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.

Factory method is a creational design pattern that solves the problem of creating product objects without specifying their concrete classes.

The main difference between a “factory method” and an “abstract factory” is that the factory method is a single method, and an abstract factory is an object. The factory method is just a method, it can be overridden in a subclass, whereas the abstract factory is an object that has multiple factory methods on it.

  • Use the Factory Method when you don’t know beforehand the exact types and dependencies of the objects your code should work with.
  • Use the Factory Method when you want to provide users of your library or framework with a way to extend its internal components.
  • Use the Factory Method when you want to save system resources by reusing existing objects instead of rebuilding them each time.

Prototype

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

Prototype design pattern is used when the Object creation is a costly affair and requires a lot of time and resources and you have a similar object already existing. Prototype pattern provides a mechanism to copy the original object to a new object and then modify it according to our needs.

It reduces the need of sub-classing. It hides the complexities of creating objects. The clients can get new objects without knowing which type of object they will be. It lets you add or remove objects at runtime.

  • Use the Prototype pattern when your code shouldn’t depend on the concrete classes of objects that you need to copy.
  • Use the pattern when you want to reduce the number of subclasses that only differ in the way they initialize their respective objects.

Structural design patterns

Structural design patterns explain how to assemble objects and classes into larger structures while keeping these structures flexible and efficient.

Adapter

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

The adapter pattern converts the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces. The client sees only the target interface and not the adapter. The adapter implements the target interface.

Adapter pattern works as a bridge between two incompatible interfaces. This type of design pattern comes under a structural pattern as this pattern combines the capability of two independent interfaces.

It allows two or more previously incompatible objects to interact. It allows the reusability of existing functionality.

  • Use the Adapter class when you want to use some existing class, but its interface isn’t compatible with the rest of your code.
  • Use the pattern when you want to reuse several existing subclasses that lack some common functionality that can’t be added to the superclass.

Decorator

Decorator is a structural design pattern that lets you attach new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors.

Decorator patterns allow a user to add new functionality to an existing object without altering its structure. So, there is no change to the original class. The decorator design pattern is a structural pattern, which provides a wrapper to the existing class.

The decorator design pattern is one of the twenty-three well-known GoF design patterns; these describe how to solve recurring design problems and design flexible and reusable object-oriented software — that is, objects which are easier to implement, change, test, and reuse.

  • Use the Decorator pattern when you need to be able to assign extra behaviors to objects at runtime without breaking the code that uses these objects.
  • Use the pattern when it’s awkward or not possible to extend an object’s behavior using inheritance.

Behavioral Design patterns

These patterns are concerned with algorithms and the assignment of responsibilities between objects.

Iterator

Iterator is a behavioral design pattern that lets you traverse elements of a collection without exposing its underlying representation (list, stack, tree, etc.).

This pattern is used to get a way to access the elements of a collection object in a sequential manner without any need to know its underlying representation. Iterator pattern falls under the behavioral pattern category.

  • Use the Iterator pattern when your collection has a complex data structure under the hood, but you want to hide its complexity from clients (either for convenience or security reasons).
  • Use the pattern to reduce duplication of the traversal code across your app.
  • Use the Iterator when you want your code to be able to traverse different data structures or when types of these structures are unknown beforehand.

State

State is a behavioral design pattern that lets an object alter its behavior when its internal state changes. It appears as if the object changed its class.

The state pattern is used in computer programming to encapsulate varying behavior for the same object, based on its internal state. This can be a cleaner way for an object to change its behavior at runtime without resorting to conditional statements and thus improve maintainability.

The State pattern minimizes conditional complexity, eliminating the need for if and switch statements in objects that have different behavior requirements unique to different state transitions.

  • Use the State pattern when you have an object that behaves differently depending on its current state, the number of states is enormous, and the state-specific code changes frequently
  • Use the pattern when you have a class polluted with massive conditionals that alter how the class behaves according to the current values of the class’s fields.
  • Use State when you have a lot of duplicate code across similar states and transitions of a condition-based state machine.

At DevCom, we’re all about developing — developing the highest quality software, strong ties with our team members, and robust partnerships with our clients. The number of years in the software development business is something to be incredibly proud of for DevCom. Our technology expertise means advanced knowledge, talent, and 20 years of experience working with all mainstream technology stacks and types of software products.

We, at DevCom, are flexible towards your business processes and requirements. Contact us.

--

--

DevCom — We do IT together
DevCom — We do IT together

Written by DevCom — We do IT together

We write about IT, software development, Cloud Computing, and tech trends. Subscribe to this blog or visit us at https://devcom.com

No responses yet