Abstract Factory, Factory Method, Builder

后端 未结 6 1206
清歌不尽
清歌不尽 2021-01-30 09:21

It may seem as if this is question is a dupe, but please bear with me - I promise I\'ve read the related posts (and the GOF book).

After everything I\'ve read, I still

6条回答
  •  我在风中等你
    2021-01-30 09:54

    A builder helps you construct a complex object. An example is the StringBuilder class (Java, C#), which builds the final string piece by piece. A better example is the UriComponentsBuilder in Spring, which helps you build a URI.

    A factory method gives you a complete object in one shot (as opposed to the builder). A base class defines a single abstract method that returns an interface (or super class) reference, and defers the concrete creation of the object to subclasses.

    An abstract factory is an interface (or abstract class) to create many different related objects. A good example (in .NET) is the DbProviderFactory class, that serves to create related objects (connections, commands, ...) to a given database provider (oracle, sql server, ...), depending on its concrete implementation.

提交回复
热议问题