Joe
Joe

Reputation: 3170

What design pattern is an API(like a web API) based on (if any) ?

I know how an API works, I've read up about it and understand how to make one too.

But what I don't understand is whether building an API follows a design pattern or is it just something that is done and design patterns are modelled around it?

From what I recall once reading, someone said that an API setup can be considered a sort of subset of the MVC design pattern. Will this be correct?

Thanks.

Upvotes: 2

Views: 1581

Answers (2)

T.S.
T.S.

Reputation: 19384

Let me clear this up for you: API and Design Patterns are NOT related. Their relation is as much as relation between database engine and REST web services - both are software running on hardware.

I can write API using completely procedural code, or better yet, using anti-pattern like Spaghetti Code.

Consider this:

public class MyAPI
{
    public int GetNumberOfLinesInThisSpaghettiCode()
    {
         // ---- Lots of spaghetti code  -----
         return 10000000000000;
    }

}

There we go - an API with no design pattern. You see, where it says "public" - this is API. Next subject.

Design Pattern is a common solution to a common problem. There are also anti-patterns - these are common practices of wrongly solve common problems.

Why it is important to know about design patterns? - besides obvious - that you create well structured, maintainable, etc. code; also because you can learn code much faster if it is developed using design patterns - it is more understandable, even if you look into it first time. Each pattern may have its specific implementation but generally it will resemble the "book version" and new programmer can quickly get going at new place, for example.

Read On at sourcemaking

Upvotes: 4

Patrick B.
Patrick B.

Reputation: 1317

But what I don't understand is whether building an API follows a design pattern or is it just something that is done and design patterns are modelled around it?

An API is a bunch of code snippets built using/following design patterns. You don't write an API then apply design patterns on it. You write your API using design patterns and grouping your code snippets in a coherent manner.
Let's consider Java Language. If you look closer in it's API, you'll recognize many design patterns used to build it's classes. For example, the Java I/0 API uses the decorator pattern a lot. Understanding the decorator pattern can help you master the Java I/O API with minor efforts. The visitor and strategy patterns are used to build the Java collection API. There are many other examples.
When you have finished building your API, the design patterns you used oblige developpers to use it in the scope the design patterns you used allow them to.

From what I recall once reading, someone said that an API setup can be considered a sort of subset of the MVC design pattern. Will this be correct?

What do you mean by API setup? I don't agree with that statement because it implies that any API follow the MVC design pattern. And I'm not sure it's true. MVC is a design pattern as other design patterns, even if it's more often used at an architectural level. For example, Struts (which, by the way, is not an API but a Framework) is built upon MVC design pattern. Furthermore, there are other architectural patterns different from MVC.

Upvotes: 1

Related Questions