Search code examples
actionscript-3oopdesign-patternsfactory-pattern

Question on class implementation with interface


I have created the following classes for sharing images. They implement an interface, but I need a way of switching between them with user interaction. I've done it the following way:

enter image description here

As you can see, service 1 and service 2 implement iSharingServices, and inherit from PolimorphSharing.

PolimorphSharing is simply and an abstract class that implements the methods I want public from Service 1 and Service 2. Those methods will then be overridden on the Service 1 and Service 2.

Because I need a way to switch the service in runtime, I've created a gateway class that inherits from PolimorphSharing. I can then call it the following way:

private var sharingService:PolimorphSharing = new SharingServicesGW('svc1').createService();

This all works flawlessly, and I can now switch between services with no problem whatsoever. However, I feel there's something wrong about it, so I would like to ask you guys for some advice on how to better implement this.

Any opinions here would be appreciated. I feel like I'm kind of implementing the factory pattern here the hard way.

UPDATE: Just adding some more insight to this. Basically the idea here is for my client to be able to upload images with various different public sharing services such as imageshack, imgur etc. I want my client to be able to select the service in which the image is to be published to (hence the "switching between them with user interaction" bit of the question.

The method that does the uploading bit, is requestShareImage(), processResults() simply turns whatever gets returned to a unique format, so my client can read off it always the same way. getObject() is my accessor, and onIOError will handle exceptions with any of the public API's

Thanks all in advance,


Solution

  • SharingServicesGW IS a factory. However, there's no need for it to - and it shouldn't - inherit from PolimorphSharing. Also you're doing it a bit skewed. The client should be using objects of the interface type, not the abstract type. Your interface should be defining the public API, not your abstract base class. In fact in AS3 interfaces can only define public members, while pseudo abstract classes can enforce implementation of protected members.

    -- EDIT --

    here's a UML diagram of how I would do it enter image description here