What type is repository pattern in?

2020-02-26 07:41发布

问题:

In general, I know that there are 3 big types of design pattern

  1. Creational Pattern (Factory, Singleton, etc)
  2. Structural Pattern (Composite, Adapter, Proxy, etc)
  3. Behavioral Pattern (Specification, Command, etc)

But I dont know which type I can put the Repository pattern in.

Is Repository pattern in one of three above type? Or is it kind of in the middle of (2) and (3) pattern?

回答1:

Repository can be viewed as a special kind of Façade (structural) but also as a special kind of Factory (creational). Also, as the Repository often expose collection-like interface, then it might be a special application of Iterator (behavioral).

What I am trying to say is that neither those categories nor patterns themselves are any sort of definite doctrine. There are just some ideas and a language that tries to make them more explicitly visible. These categories are just helpers trying to express somehow what some patterns do. Also patterns are just various expressions of a generic loose coupling principles. Their borders are blurry.



回答2:

A repository is a specialisation of the Facade pattern which is structural.



回答3:

I assume you refer to the repository pattern by Martin Fowler.

He says:

Repository: Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.

He himself categorizes the pattern as "Object-Relational Metadata Mapping".

If you want to use the categories in the GOF book, I'd put it into the structural pattern category as the focus of this pattern is to present an interface/view to a client, similar to the Adapter/Facade/Proxy patterns.