I'm running into a wall when trying to use extension with interfaces.
The example I'm about to provide works using Java, so I believe the
OOP practices behind it are valid. But I can't figure out how to
accomplish the same thing in Flex, I keep running into compiler
issues. The example is a condensed version of something I'm working
on, for simplicity sake.
Let's say you have a bunch of events that all have some common
functionality, like the ability to have a nextEvent. Also, a subset
of those events also have additional functionality, they can be
filtered by a type of criteria. We'll call them search events.
Now, there are some parts of the application that can work with any
type of search event, and any type of criteria. But when the
individual searches are executed, that implementation expects a
specific type of search event and search criteria. That's my end goal
here.
Here are the interfaces I'm using:
public interface ICriteria
{
// just a marker
}
public interface ICustomCriteria extends ICriteria
{
function get name():String;
function set name(name:String):void;
}
public interface IEvent
{
function get nextEvent():IEvent;
function set nextEvent(event:IEvent):void;
}
public interface ISearchEvent
{
function get criteria():ICriteria;
function set criteria(criteria:ICriteria):void;
}
So far, so good. And now the implementing classes:
// would like to make this an abstract class, but not supported in AS
public class BaseEvent implements IEvent
{
private var _nextEvent:IEvent;
public function set nextEvent(event:IEvent):void
{
this._nextEvent = event;
}
public function get nextEvent():IEvent
{
return this._nextEvent;
}
}
// would like to make this an abstract class, but not supported in AS
public class BaseSearchEvent extends BaseEvent implements ISearchEvent
{
private var _criteria:ICriteria;
public function set criteria(criteria:ICriteria):void
{
this._criteria = criteria;
}
public function get criteria():ICriteria
{
return this._criteria;
}
}
Those base classes are fine and compile correctly. Now if you
remember from the requirements, there are some places that can work
with any type of ISearchEvent, and some places that can only work with
an individual subclass. Here is where the compiler errors come in.
Doing what I am about to do works in Java, and I'm wondering if there
is a way to accomplish it in AS3.
public class CustomSearchEvent extends BaseSearchEvent
{
private var _criteria:ICustomCriteria;
public override function get criteria():ICustomCriteria
{
return this._criteria;
}
public override function set criteria(criteria:ICustomCriteria):void
{
this._criteria = criteria;
}
}
The above class does not compile because the method signatures for
get/set criteria are different than those specified in the interface.
But from an OO perspective, I think this is a valid thing to want to
do. If you're working with an ISearchEvent instance, you should be
able to subclass the base class and provide custom behavior. The
ICustomCriteria is a subclass of ICriteria, so you should be able to
substitute them in this context. Is there another way to accomplish
this? Or is this just a language feature that's not there?