S
s0suk3
Some time ago I came across this:
http://developer.apple.com/document...rence/CFAllocatorRef/Reference/reference.html
It's an object that's created by specifying a generic pointer to user-
defined data, as well as a set of pointers to functions that perform
various memory management tasks (allocate, deallocate, etc.).
The object can then be used by clients to allocate memory
independently of what kind of user-defined data and function pointers
are associated with a particular instance of the type.
I thought it was a good idea so I implemented my own version of it and
have been using it in a program. A couple of days ago I realized that
the type uses a design pattern that's really analogous to what other
languages offer in the form of an "interface": a type that's used only
as a base type for other types, and that can't be instanciated, but
variables of the type can hold instances of types that are derived
from the interface. In Java, for example, the Allocator type shown in
the above page would ideally be defined something like this (for
simplicity, I'll only deal with three of the functions shown in the
page):
public interface Allocator {
public void* allocate(int size);
public void* reallocate(void* ptr, int newSize);
public void deallocate(void* ptr);
}
(I know that Java doesn't have void*, but I couldn't think of an
alternative
In C++ there are no interfaces, but the equivalent is an "abstract
class" (a class with pure virtual functions):
class Allocator {
public:
virtual ~Allocator() {}
virtual void* allocate(size_t size) = 0;
virtual void* reallocate(void* ptr, size_t newSize) = 0;
virtual void deallocate(void* ptr) = 0;
};
So the question is what the Subject says: how would you define such an
interface in C? The method shown in the Apple page works but is IMO
inflexible, because the Allocator is a concrete type, and so it
prevents several design techniques that are inherent to subtyping,
like for example deriving a type that implements only part of the
functionality.
Sebastian
http://developer.apple.com/document...rence/CFAllocatorRef/Reference/reference.html
It's an object that's created by specifying a generic pointer to user-
defined data, as well as a set of pointers to functions that perform
various memory management tasks (allocate, deallocate, etc.).
The object can then be used by clients to allocate memory
independently of what kind of user-defined data and function pointers
are associated with a particular instance of the type.
I thought it was a good idea so I implemented my own version of it and
have been using it in a program. A couple of days ago I realized that
the type uses a design pattern that's really analogous to what other
languages offer in the form of an "interface": a type that's used only
as a base type for other types, and that can't be instanciated, but
variables of the type can hold instances of types that are derived
from the interface. In Java, for example, the Allocator type shown in
the above page would ideally be defined something like this (for
simplicity, I'll only deal with three of the functions shown in the
page):
public interface Allocator {
public void* allocate(int size);
public void* reallocate(void* ptr, int newSize);
public void deallocate(void* ptr);
}
(I know that Java doesn't have void*, but I couldn't think of an
alternative
In C++ there are no interfaces, but the equivalent is an "abstract
class" (a class with pure virtual functions):
class Allocator {
public:
virtual ~Allocator() {}
virtual void* allocate(size_t size) = 0;
virtual void* reallocate(void* ptr, size_t newSize) = 0;
virtual void deallocate(void* ptr) = 0;
};
So the question is what the Subject says: how would you define such an
interface in C? The method shown in the Apple page works but is IMO
inflexible, because the Allocator is a concrete type, and so it
prevents several design techniques that are inherent to subtyping,
like for example deriving a type that implements only part of the
functionality.
Sebastian