Creating URL with Builder pattern What do you think about this piece of code? How would you design the builder pattern in this situation?

#ifndef CPP_URL
#define CPP_URL
#include&lt;iostream&gt;
#include&lt;vector&gt;
#include&lt;map&gt;
using std::string;

enum protocol  {
  HTTP,
  HTTPS
};

static string map(protocol scheme) {
  switch(scheme) {
    case HTTP:
      return "http";
    default:
      return "https";
  }
}

static int defaultPort(protocol scheme) {
  switch (scheme) {
    case HTTP:
      return 80;
    default:
      return 440;
  }
}

class url final {
private:
  using list = std::vector&lt;std::string&gt;;
  using pairs = std::map&lt;std::string, std::string&gt;;
  const list paths;
  const pairs queries;
  const protocol scheme;
  const string domain;
  const string fragment;
  const string username;
  const string password;
  mutable int port;
public:
  class builder {
   friend url;
   private:
      protocol scheme;
      pairs queries;
      list paths;
      string domain;
      string fragment;
      string username;
      string password;
      int port = -1;
   public:
     builder&amp; setScheme(protocol);
     builder&amp; setFragment(const string&amp;);
     builder&amp; setDomain(const string&amp;);
     builder&amp; setUsername(const string&amp;);
     builder&amp; setPassword(const string&amp;);
     builder&amp; setPort(int port);
     builder&amp; addQuery(const string&amp;, const string&amp;);
     builder&amp; addPath(const string&amp;);
     url&amp; build() const;
  };
  explicit url(const url::builder&amp;);
  string build() const;
};

using builder = url::builder;

inline builder&amp; builder::setScheme(protocol scheme) {
  this-&gt;scheme = scheme;
  return *this;
}

inline builder&amp; builder::setUsername(const string&amp; username) {
  this-&gt;username = username;
  return *this;
}

inline builder&amp; builder::setPassword(const string&amp; password) {
  this-&gt;password = password;
  return *this;
}

inline builder&amp; builder::setFragment(const string&amp; fragment) {
  this-&gt;fragment = fragment;
  return *this;
}

inline builder&amp; builder::setDomain(const string&amp; domain) {
  this-&gt;domain = domain;
  return *this;
}

inline builder&amp; builder::setPort(int port) {
  this-&gt;port = port;
  return *this;
}

inline builder&amp; builder::addQuery(const string&amp; key, const string&amp; value) {
  queries.insert(std::pair&lt;string, string&gt;(key, value));
  return *this;
}

inline builder&amp; builder::addPath(const string&amp; path) {
  paths.insert(paths.begin(), path);
  return *this;
}

inline url&amp; builder::build() const {
  return *(new url(*this));
}

inline url::url(const builder&amp; object)
    : paths(object.paths)
    , queries(object.queries)
    , scheme(object.scheme)
    , domain(object.domain)
    , fragment(object.fragment)
    , username(object.username)
    , password(object.password)
    , port(object.port)
    {}

string url::build() const {
  string url = map(scheme).append("://");
  if (!username.empty()) {
    url.append(username);
    if (!password.empty()) {
      url.append(":").append(password);
    }
    url.append("@");
  }
  url.append(domain);
  if (port == -1)
    port = defaultPort(scheme);
  url.append(":").append(std::to_string(port));
  for (string path : paths) {
    url.append("/");
    url.append(path);
  }
  auto it = queries.begin();
  if (it != queries.end()) {
    url.append("?").append(it-&gt;first)
      .append("=").append(it-&gt;second);
    for(it++; it != queries.end(); it++) {
      url.append("&amp;").append(it-&gt;first)
        .append("=").append(it-&gt;second);

    }
  }
  if (!fragment.empty()) {
    url.append("#").append(fragment);
  }
  return url;
};

#endif


This is how we can use that:

   url url = url::builder()
    .setScheme(HTTPS)
    .setDomain(YOUTUBE_DOMAIN)
    .addPath(WATCH_PATH)
    .addQuery(CATEGORY, ITEM)
    .build();
  std::cout&lt;&lt;url.build()&lt;&lt;std::endl;
Include the required headers

You use std::string extensively, but you don't include &lt;string&gt;. This is allowed to work (any standard header is allowed to act as if it includes any or all others), but it's not required to work (and didn't, for me).

Use the proper defaults

Right now, your code defaults to https on port 440, but the normal default for https is port 443. I'd tend to include the port in the result if and only if it's explicitly specified. Otherwise, it's probably better to let the browser sort out the port, since most know quite a few more than your code does.

Fluent Interface

I'm not particularly excited about fluent interfaces in general, but if you're going to use one, I'd prefer to get rid of the set on the beginnings of most of the setters. If you have something like:

auto uri = builder().scheme(HTTPS).domain("www.google.com").build();


...you don't really need a set on the beginning of each to make it apparent what you intend.

Consider unique types for the parts of a URL/URI

I tend to wonder whether we wouldn't be better served by a set of tiny classes, one for each type of element in a URL, with each able to (for example) insert itself to a stream, with the url class basically just a set of those. At least for some of the obvious cases, I'd also at least consider using overloaded operators instead of named functions. For example, a fairly minimalist version might look something like this:

struct protocol {
    enum prot { http, https } p_;

    protocol(prot p) : p_(p) {}

    friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, protocol const &amp;p) {
        switch (p.p_) {
        case http: return os &lt;&lt; "http://";
        case https: return os &lt;&lt; "https://";
        }
    }
};

struct query {
    std::string k;
    std::string v;
};

class queries {
    std::vector&lt;query&gt; queries_;
public:
    void add(query const &amp;q) { queries_.push_back(q); }

    friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, queries const &amp;q) {
        std::string sep = "?";
        for (auto const &amp; f : q.queries_) {
            os &lt;&lt; sep &lt;&lt; f.k &lt;&lt; "=" &lt;&lt; f.v;
            sep = "&amp;";
        }
        return os;
    }
};

class path {
    std::vector&lt;std::string&gt; components;
public:
    void add(std::string const &amp;path) {
        components.push_back(path);
    }

    friend std::ostream &amp;operator &lt;&lt; (std::ostream &amp;os, path const &amp;p) {
        for (auto const &amp; s : p.components)
            os &lt;&lt; s &lt;&lt; "/";
        return os;
    }
};

class url {
    protocol prot_;
    path path_;
    queries q_;
public:
    url(protocol::prot p) : prot_(p) {}

    url &amp;operator/(std::string const &amp;p) { path_.add(p); return *this; }
    url &amp;operator&amp;(query const &amp;q) { q_.add(q); return *this; }

    friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, url const &amp;u) {
        return os &lt;&lt; u.prot_ &lt;&lt; u.path_ &lt;&lt; u.q_;
    }
};

int main() {
    url u = url(protocol::http) / "www.youtube.com" / "watch" &amp; query{"v", "tpnrd0xGRsw"};
    std::cout &lt;&lt; u;
}


Note how url's operator&lt;&lt; (roughly equivalent to your build) is now almost trivially simple--and if we add in types for a fragment, port, user name, and so on, it'll remain almost equally trivial, because most of the complexity will be delegated to each of those classes instead of being crammed together into the one giant "build" that knows everything about a URL and how all the pieces need to be delimited and such. Instead, it needs to know the order of the components in a URL, but that's about it. All the details of how to write each piece are delegated to the type for that piece (most of which are individually pretty trivial too).

More syntactic sugar

After some further thought, I've decided that a little more syntactic sugar probably won't rot our teeth too badly. It might be worth adding a couple of tiny functions like this:

url http() { return url(protocol::http); }
url https() { return url(protocol::https); }


This way we can generate a url something like this:

url u = https() / "www.youtube.com" / "watch" &amp; query{"v", "tpnrd0xGRsw"};


Seems a tad cleaner to me, anyway.