私はそれは私がこのようなイベントインターフェイスを持っていると言うが爆発すると思う私のクラス階層に対する解決策を見つけようとしています
public interface Event {
String getId();
String getType();
String getName();
String getDescription();
Date getDate();
JsonElement getRaw();
}
public class BasicEvent implements Event {
protected String eventId;
protected String eventType;
protected String eventDescription;
protected String eventName;
protected Date time;
protected JsonElement raw;
public BasicEvent(String eventId, String eventType, String eventDescription, String eventName, Date time,
JsonElement raw) {
this.eventId = eventId;
this.eventType = eventType;
this.eventDescription = eventDescription;
this.eventName = eventName;
this.time = time;
this.raw = raw;
}
public BasicEvent(String eventId, String eventType, Date time, JsonElement raw) {
this(eventId, eventType, null, eventId, time, raw);
}
public BasicEvent(String eventId, String eventType, Date time, String eventName, String eventDescription) {
this(eventId, eventType, eventDescription, eventName, time, null);
}
@Override
public String getId() {
return this.eventId;
}
@Override
public String getType() {
return this.eventType;
}
@Override
public String getName() {
return this.eventName;
}
@Override
public String getDescription() {
return this.eventDescription;
}
@Override
public Date getDate() {
return this.time;
}
@Override
public JsonElement getRaw() {
return raw;
}
public Map<String, Object> getData() {
return new Gson().fromJson(raw, Map.class);
}
@Override
public String toString() {
return "BasicEvent{" + "eventId=" + eventId + ", eventType=" + eventType + ", time=" + time + ", raw=" + raw
+ '}';
}
}
私は3つのクラスを追加する継承を使用してイベントがシステムから、デバイスから又はゾーンからtriggredすることができます
public class ZoneEvent extends BasicEvent {
private Long zoneId;
public ZoneEvent(BasicEvent event, Long zoneId) {
super(event);
this.zoneId = zoneId;
}
public Long getZoneId() {
return zoneId;
}
public void setZoneId(Long zoneId) {
this.zoneId = zoneId;
}
}
public class DeviceEvent extends BasicEvent {
private Long deviceId;
private String driverId;
private String deviceType;
public DeviceEvent(BasicEvent event, Long id, String driverId, String deviceType) {
super(event);
this.deviceId = id;
this.deviceType = deviceType;
this.driverId = driverId;
}
public String getDriverId() {
return driverId;
}
public void setDriverId(String driverId) {
this.driverId = driverId;
}
public void setDeviceId(DeviceId id) {
this.deviceId = id;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
public DeviceId getDeviceId() {
return deviceId;
}
public String getDeviceType() {
return deviceType;
}
}
public class SystemEvent extends BasicEvent {
private Long systemId;
public SystemEvent(BasicEvent event, Long systemId) {
super(event);
this.systemId=systemId;
}
public Long getSystemId() {
return systemId;
}
public void setSystemId(Long systemId) {
this.systemId = systemId;
}
}
今イベントは、イベントがするzoneidおよびデバイスIDまたはデバイスIDとシステムIDに含めることができますし、私のクラス階層が爆発するこれら3つのクラスからの組合せとすることができます。この設計への解決策はありますか?そして、デコレータを使用してこの問題の王を解決することができますか?
オクラホマので、あなたの代わりに、それらの機能の属性に基づいてクラスを分離するという点で、より考えています。これは、OOPの周りに見ることは非常に一般的ですが、私はそれが間違っていると思います。
私はあなたがやるべきだと思うことはゲッターの完全かつ唯一の方法で1を作成する溝のインタフェースです。あなたがのイベントオブジェクトを必要としないものにと思いますか?多分それは、ワイヤ上で送信しますか?この方法は送信されます()。たぶん、別の男がそれを送信を担当しているのですか?あなたは方法は、印刷(OutputStreamのうち)ようなものになるだろうように、ストリームにイベントの内容を印刷する必要があるかもしれません。
あなたは、イベントがこれら3の任意の組み合わせであることができることを述べたので、私はあなたが3つの別々のクラスを必要と再考と言うでしょう。あなただけの地図か何かのように異なる特性を受け入れるものを必要とするように見えます。:それぞれが異なる機能を持っているので、あなたが本当にそれらを分離する必要がありますが、ある時点で、あなたがそれらすべての情報を結合する必要がある場合は、その後私は、プリンタのパターンは、ここで説明することをお勧め https://www.yegor256.com/2016/04を/05/printers-instead-of-getters.html 各イベントの実装では、定義したことをビルダークラスに、独自の内容を書き込む方法のプリントを持っているでしょう。あなたが必要として、あなたはそのビルダークラスを使用します。
あなたが言及したことをクラス階層の問題に触れるサンディメッツから関連する話もあります。https://youtu.be/OMPfEXIlTVE強くお勧めします。
それが役に立てば幸い!