2

I am new to c# programming and I apologize for my beginner level question. I have tried to google about this pattern but have been unsuccessful. While learning about c# MVC/WCF on the net, I have come across various examples where it uses the following pattern or technique:

       public PaperResponse GetPaperResults(LearnerPaperRequest request)
    {
        return LearnerResponse.GetPaperResults(request);
    }

where

    public static class LearnerResponse
    {
        public static PaperResponse GetPaperResults(LearnerPaperRequest request)
        {
             //do work by calling Business logic layer or do work here itself like calling service or db
              return paperResponse;
        }
    }

I am trying to find the name of this pattern or technique so that I can read more about this pattern or technique.

KiranK
  • 33
  • 4
  • https://en.wikipedia.org/wiki/Separation_of_concerns would be one idea being used here though the static class and methods would be another. Is there some part to this that you want a name? There could be multiple patterns at work here. – JB King Oct 30 '15 at 15:17
  • @JBKing - Thank you for replying. LearnerResponse class is responsible for processing of all the request to said service/controller. For example if I were to add one more method to service called GetLearnerDetail(LearnerDetailRequest request), this will lead to addition of an another method in LearnerResponse class called GetLearnerDetail. I have not understood what advantage is obtained by transferring the responsibility to a separate class. Hence wanted to know what the name of the technique is so that I can understand it better. – KiranK Oct 30 '15 at 15:48
  • https://en.wikipedia.org/wiki/Dependency_injection would be another concept while for design patterns this would seem similar to factory or adapter patterns to my mind. – JB King Oct 30 '15 at 16:09
  • This looks more like a couple of anti-patterns (at least at first sight). Using a static class like `LearnerResponse` is like having a singleton, except that you cannot even have it implement an interface to pass it around. There is no way to test the first method, because you cannot mock `LearnerResponse`. The second might be that the first method does too little (I've seen people calling it Yet Another Java Layer, or something similar). – Groo Oct 31 '15 at 14:13

2 Answers2

0

Based on the minimal amount of code, I'd venture the following answer:

Layers pattern (and maybe Facade)

The static class LearnerResponse plays the role (maybe) of a Facade in the layers pattern. The comment clearly says that it handles business logic. Facade classes don't have to have static methods, but there are several examples in Java such as JOptionPane.

Assuming class X is in another layer (e.g., the presentation layer or GUI), it's not wise to handle business logic there, since often we make different presentation layers for different interactions with the user.

A great example is Siri on iOS, which does voice recognition. When you ask Siri to "Play artist Led Zeppelin" it's an equivalent command to clicking in the GUI to select music by the artist. The code to recognize, present, etc. is different, but the business logic (starting the play operation) is the same. If we put the business logic in the presentation layer, when we make a new version of the layer, then we'd repeat ourselves in repeating the logic.

Here's what the dynamic of your sample code looks like in UML:

UML sequence diagram of code

In this diagram, classes X and Y are variations of the layer that will call the LearnerResponse facade. They both need to perform the GetPaperResults operation, but since it's business logic, the design delegates that responsibility to another layer (beyond the facade).

This pattern is also a variation of using indirection, which also achieves protected variations (which is a form of information hiding and related to encapsulation and abstraction). It allows changing the implementation of LearnerResponse.GetPaperResults() without breaking the X and Y classes, provided the signature of the methods doesn't change.

You could even argue that this is a form of the open-closed principle, since LearnerResponse is open to extension with respect to its clients.

Community
  • 1
  • 1
Fuhrmanator
  • 7,907
  • 4
  • 51
  • 88
0

It's delegation, not really a pattern, more like a mini-pattern. Many patterns rely on delegation, but it's not really viewed as a pattern in itself.

ComDubh
  • 553
  • 2
  • 15