Похожие чаты

Regarding original question. Personally, I haven't ever seen such usage

of marker interfaces. Neither in any codebase, nor in resources/documentation/books i.e. examples. I doubt that it's open source, but if it is, can you please share the link? Neither I've ever heard about such anti-pattern being described. Again, if you have a link to any resource about it can you post it here?

One more thing to clarify. this is not a marker interface:
interface Dog {
bark();
}

interface DomesticDog extends Dog {
// empty but not marker
}

interface FightingDog extends Dog {
fight(Dog withOther);
}

interface HuntingDog extends Dog {
hunt(Animal fowl);
}

In the code above interface DomesticDog is empty, but it's totally OK and is a good design. It indicates that type "DomesticDog" is a specialization of Dog but has nothing special. Yet. And this is the key factor - YET. Marker interfaces will always be empty whatever way the design would change. They are like tags.

Thus, I can't say for sure what exactly do interfaces in your codebase do.

If they are true marker interfaces. For classification in 100% I've ever faced it was done either this way in old java (you can often find such code in old projects):
interface HttpResponseType {
static final int OK = 200;
....
}

class HttpResponse {
int type;
}
or with enums (was it java 1.5+ ? can't remember when enums were added):
enum HttpResponseType {
OK,
...
}

class HttpResponse {
HttpResponseType type;
}

And in the client code you just check that type propertly of the object. Havent ever seen such classification was done with instanceof and true marker interfaces. Ever.

I can see several drawbacks of using marker interfaces for classification with instanceof operator:
- it's totally uncommon technique and has no any real advantages over using enums (which is common and thus readable); so other would guess about the real purpose of using those marker interfaces (what actually I'm doing here)
- the cost of instanceof operation is much higher than simple identity check with type == Type.ENUM_CONSTANT.
- polluting your project sources structure with totally different substance: all the xxx.java files actually represent type or utility class (non-instantiable/singleton with utility methods). But markers/tags are different, and they'll pollute sources and bring confusion for new developers.

But I can't see any huge "smell", no any real stopper to not use it. It's just so uncommon.


But having marker interfaces being used like tags makes sense. It really matters how exactly those marker interfaces are used. For example, the famous Serializable is used not for classification but for adding special ability/treatment from the runtime side. I could imagine the same with some framework, but all of them use annotations for this (or I just havent payed attention to this and cant remember now).

1 ответов

8 просмотров

thnx, was useful! The project which I am am working on, is closed-source and the last developer who was working on it, added a lot of interfaces without any method or constant variable. accourding to what you said, now I'm sure they should be deleted soon.👍

Похожие вопросы

Обсуждают сегодня

Hi everyone! I have a two-folded message to share today. First: updates and news, as we are being asked for those. We still want to go like we did the last 6 years: we w...
Marten | Unibright.io
31
комрады, че-та лыжы не едут var tmpFont: TFont; begin tmpFont:= TFont.Create; try case rgFontColor.ItemIndex of 0: tmpFont.Color:= clWindowText; 1: tmpFo...
Ed Doc
34
VIP-310 Set RedStone as the MAIN oracle for BTC and BNB on BNB Chain Summary If passed, this VIP will perform the following actions: - Configure RedStone as the MAIN oracle...
Venus Announcements
1
Интересно, нет ли какого-то способа получить из dll не адрес самой метки, а адрес со смещением?
The Bird of Hermes
54
monero is the only tool for privacy and currency . why would we destroy it?
😎
13
🔍 Launch of Sentinel dVPN's Native Explorer 🎉 The Sentinel Growth DAO is thrilled to announce the launch of Sentinel dVPN's new native blockchain explorer! Now anyone can div...
Joey | BadgerBite Joeyy
1
Можно вообще написать: Person fName' lName' age'. Тогда действительно имена полей потребуются лишь в строковом виде, чтобы эти fName' и т.д. достать :-) Но разве для этого нуж...
Михаил
8
Its been almost a week and the upgrade only went from 30% to 35%, why so slow?
Jackson
10
generic procedure function test<T>(param: T); type case T of longint: NewT = word; longword: NewT = byte; end; var v1: NewT; Как это можно сделать? Чтобы у меня...
notme
21
Whoa, did you guys see this FUNToken Seed Grant announcement? 💸
Noah Noure
37
Карта сайта