Software Architecture: Architectural patterns
Published: 2021-04-29 16:16:59

Okay so as being a react developer that has always focused on coding while not giving much preference to the theory part, I decided to learn some stuff which led me here to **Architectural Patterns**, so I decided to write something on it.

## What is a Software Architecture?
Creating software doesn't always mean just code code and code and your software is ready (well that may work for small apps but doesn't applies to large-scale software). You have to think about various stages to create software which are: 

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/14vwfp34k3brfy8r3v86.png)
 

**Software architecture is how the components that define the software are organized and assembled, how they communicate with each other, and also the constraints or rules with which the whole system will run.
**

Let's have a closer look at the definition. 
- The organizing and assembling of the components is what architectural patterns do.
- The communication part is done through the API's.
- The rules or constraints are scalability, resilience, adaptability, resolve, etc.

And all these points together form the architecture of the software.
### What is architecture?
Architecture is a set of software structures, and a structure is simply a set of elements held together by a relation. There are three types of architectural structures, which play an important role in designing, documentation, and analysis of architectures.

- First plays a role in dividing the units to work on the database, business rules, and user interface.
- Second is dynamic and carries out the system functions and runtime elements.
- Third kind connects the software structure to organization, development, and installation.

Architecture specifically omits certain information about elements that are not useful for reasoning about the system. Architecture is an abstraction that focuses on certain information. Modern softwares are divided into two parts, public and private and architecture focuses on the public part as the private section deals with the internal implementations and hence not architectural.

## Architectural Patterns

In some cases, architectural elements are composed in such a way that it solves a particular problem and those compositions have been found useful in future and have been used over time to time in different situations, these compositions are known as architectural patterns.

Architectural patterns are just like templates (not exactly a template) or wireframes which makes it easy to build an application. There are different types of patterns present and I will be discussing a few of them here.

### Layered Pattern

Layered pattern also known as n-tier pattern, as the name suggests, consists of different layers. Generally, we take four layers in this pattern which are presentation, business, persistence, and database. The most common example of this pattern is the OSI model and the web architecture model.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eo7nvj6hfv6k7nmvqy4z.png) 

And this is how a layered pattern looks like with arrows showing the default behavior of this pattern:

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d4gl469mroauc6s6bjuw.png) 

A layer is an independent piece of the module in which several components are interconnected with each other. If there is any problem in one layer then we can fix it within the layer without affecting the other layers.

Each layer of the pattern has a specific responsibility in the application. The presentation layer does all the User Interface related stuff and defines how the application will look. The business layer takes the business decisions such as an email is sent to the user if this action is done. The persistence layer contains all the code definitions, main programs, and basic functions of the applications. The database layer contains the databases.

One of the great features of this pattern is the **separation of concern** within the layers i.e. each layer focuses on doing its task and nothing else like if a layer is managing the databases then it is none of its business what other layers are doing or what to do other than handling databases, it only focuses on managing the databases.

Now, as I told earlier that the default behavior of this pattern is moving down the layers step-wise. This is only followed when the layers are **closed**. A layer being in a state of **close ** means that as the request moves from layer to layer, the request must pass through the layer right next to the previous one and not skipping any layer.

Another state of layers is **open** (yes you have guessed right). It is not always necessary that this pattern should always go for 4 layer architecture, we can have multiple layers in this system (as the name n-tier suggests). So let's suppose we take a new layer in between the business layer and persistence layer and name it Service Layer and this layer is open. Then when a request will pass through the business layer **AND ** if it's of any use to the service layer **ONLY THEN** it will be sent into that layer otherwise the request will jump directly to the persistence layer.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qr14udfahc4odlniz253.png) 

Using a combination of both open and closed layers sometimes becomes very messy for the programmer so it should be used wisely. 

### Client-Server Pattern

Before going to this pattern first I'm going to discuss what is a client and a server.

#### Client

A client can be a machine or program. Machines can be a mobile, computer, laptop, etc and a client program is a program that allows users to make a request for example web browsers, editing software, etc.

#### Servers

As we all must have heard that a server is a high-performing machine but actually it is a program known as a server program that RUNS on a high-performing machine as it has to handle a lot of requests at once and also they provide the functionality to the clients.

And as I said a server can serve multiple clients at once and a single machine can have multiple servers.

#### Conclusion

Now as we have discussed client and servers we can now move on to the client-server pattern which can now be explained in just a few lines.

A client-server pattern is a centralized way to get the clients to communicate with servers on the web where the client makes the request and the server responds to it.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8jcozciw03ajqopidfa9.png) 

This is just one way to surf the web in a centralized manner and similarly, there is a decentralized manner too which is known as **peer-to-peer Model** where a machine can work both as a client and a server and no one has a specific role both can send requests and respond to it.

The most common example of this model is bit torrent and skype.

And now as this blog is getting really long and must be boring (haha I really appreciate it if you are still reading) I should end it here now. Thank you for giving me your valuable time.