How does a method with the same name return more than one object type?

Asked

Viewed 123 times

4

I am implementing the Google Drive API and I came across the following situation:

var fileMetadata = new GoogleDriveAPIV3.Data.File()
        {
            Name = "folderTest/testeUpload.jpg"
        };
FilesResource.CreateMediaUpload request1 = new DriveConnection().ReturnDriveService().Files.Create(
                fileMetadata, stream, "image/jpeg");


var fileMetadata2 = new GoogleDriveAPIV3.Data.File()
        {
            Name = "FolderTest",
            MimeType = "application/vnd.google-apps.folder"
        };
FilesResource.CreateRequest request2 = new DriveConnection().ReturnDriveService().Files.Create(fileMetadata);

The method Create is returning two types of Object (CreateMediaUpload or CreateRequest) and this differentiation is occurring when I pass a different parameter.

How can it happen one method return more than one object different ?

2 answers

6


There is a design pattern (Design Pattern) so-called factory (Factory).

Factory is a Design Pattern for creation of objects.

To Factory knows which object to create according to the parameters it receives.

Therefore, when we implement this pattern and try to create the object, internally Factory is in charge of returning the object, which may be different depending on the parameters reported.

In this example you reported, when creating the object using new DriveConnection().ReturnDriveService().Files.Create(...), internally to Factory when receiving the parameters can return an object or another.

Example (this example is only for basic understanding of how a method could return more than one object type, but if you want to know more details about the design pattern Factory take a look here):

Imagine that CreateMediaUpload and CreateRequest implement the same interface called ICreate. Let’s assume there’s a class that will be ours Factory with a method like this:

public ICreate Create(....)
{
  // De acordo com os parâmetros, pode ser retornado um tipo CreateMediaUpload ou CreateRequest
}

In this example, the Factory can provide a method for creating different objects that implement the interface ICreate.

Editing:

Another example where a method can return more than one different object:

Imagine that the class Files below provide two methods Create with different parameters, depending on the parameters an object type is returned:

public class Files 
{
    public CreateMediaUpload Create(string fileMetadata, string stream, string imageType)
    {            
        return new CreateMediaUpload(fileMetadata, stream, imageType);
    }

    public CreateRequest Create(CreateMediaUpload createMediaUpload)
    {
        return new CreateRequest(createMediaUpload);
    }
 }

When using Files.Create(...) although the method name is Create I can get different objects.

  • 1

    Very good, great answer, took my doubt.

  • I couldn’t verify that the classes referred to implement the same interface or have some kind of inheritance in common. Even if CreateMediaUpload and CreateRequest implement the same interface, with Create(....) returns to interface, is not allowed to do CreateMediaUpload nomeVar = factory.Create(....);, unless a cast explicit: CreateMediaUpload nomeVar = (CreateMediaUpload)factory.Create(....), what is not done in the question code. Either the question code does not compile or there must be another explanation, something that does not occur to me or I do not know.

  • @ramaral I am not familiar with the objects of the Google Drive API and do not have the original code of the question to know more details, the answer is more focused on the conceptual question How can a method return more than one different object ? as it did not raise any problem of compilation error in the question. I did not even worry about other issues other than that question. If you can help post an answer there for us sharing your knowledge as well :-)

  • I agree with your reply, according to your comment. I have nothing to add but the need to have the cast explicit, which does not appear in the question code. Also note that the cast can "pass" at build time but fail at runtime.

  • There is no need to use cast when implementing the Factory project standard, in case of doubts I provided a reference link to a complete implementation of Factory in the reply :-)

  • Confirm or not that the following is true: If the method returns an interface you will have to do cast for the specific object if you want to use it as such. CreateMediaUpload nomeVar = factory.Create(....); does not compile, or uses cast or uses ICreate nomeVar = factory.Create(....);

  • I added another example, with a slightly different scenario, to try to improve the answer and make it clearer.

  • Now yes, the example given explains the example of the question. Now I can give my +1 :)

Show 3 more comments

3

Take a look at the signature of these objects:

FilesResource.CreateMediaUpload

Filesresource.Createrequest

So much CreateMediaUpload and CreateRequest are functions that returns objects(Object Factory) then its method Create returns a reference to a function. This function, which in turn returns a specific object.

  • Augustus as so are functions? n are objects already declared before the name of the same ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.