Law of Demeter causing subclassing indecision and conflict with Single Responsibility

This is my situation:

I have a class called CueList, which is basically a wrapper around ArrayList. I have another class called TimeTicker that alerts all the Tickable (interface) in its list that a certain amount of time has passed. Since I need to tick all of the items in CueList, but I don’t want to violate DRY by keeping a copy of all of the CueList items in the TimeTickable class as well (which would make a nightmare when I want to delete a Cue), my immediate instinct was to make CueList a Tickable and have it pass on the “tick” message to all of its items.

This seems to violate the Single Responsibility principle, which says that CueList should not worry about both keeping track of all the cues AND Ticking them.

So what I did was create a CueManager class that is itself Tickable and links everything up (and ticks all of the CueList elements).

But now, because of the law of demeter, I have a bunch of duplicate methods in the CueManager that just mirror the TimeTicker and CueList class.

Doesn’t that mean that the CueManager class has multiple reasons to change? Doesn’t that mean it violates the Single Responsibility principle.

Also, since most of the methods in CueManager mirror TimeTicker and CueList, doesn’t that really mean that it IS a TimeTicker and CueList. Therefor shouldn’t it INHERIT from CueList and TimeTicker! (Obviously you can’t do multiple inheritance in Java, but I feel like it sort of makes sense to Inherit from CueList, but then that would violate Single Responsibility)

So in summary:

Law of Demeter makes me make a bunch of methods that merror two other classes, but that makes me think that it really should just inherit from both of those classes, but that would mean that one class handles multiple responsibilities (even if it is through its superclass).

2

CueList is an ArrayList (or at the very least can expose some similar interface such as Collection, List or Iterable). All items in that ArrayList are, ostensibly, Tickables. TimeTicker doesn’t, and shouldn’t, care that its items are in a CueList specifically, nor should it care that the items in the list it acts on are anything but Tickables. By the same token, CueList should have no knowledge of TimeTicker; it is of no bearing to CueList’s basic functionality AFAICT. This is the essence of the Least Knowledge Principle.

So, make TimeTicker act on an injected ArrayList<Tickable> (or a Collection<Tickable> or a List<Tickable> or an Iterable<Tickable>, whatever interface exposes the methods you use to spin through this list) and pass in CueList as this dependency, via some class that already has to know about both the list and the ticker.

Sounds like the Observer pattern where it is normal that the subject (TimeTicker) keeps a list of its observers (Tickables) so it can update them on an event. Your Tickable interface can define an UnRegister() method which you can call when it is removed from any list.

class TickableItem
{
    TimeTicker Subject;

    void UnRegister()
    {
        this.Subject.UnRegister(this);
    }

    void Register(TimeTicker subject) { ... }

    void OnTick() { ... }
}

The benefit of this approach is that your CueList doesn’t need to know who the TimeTicker is. It’s tickable items know who their TimeTicker’s are (they can be registered with different ones).

What you are describing is the Observer pattern and Java has already two nice things for that in store. There is the interface Observer and the abstract class Observable. If you use these two, then your TimeTicker does not handle the observer. TimeTicker extends from Observable and does only the stuff it needs to do. When a certain amount of time has passed, it calls the method setChanged and notifyObservers.

Your TimeTicker does know 0 Tickables (Observer). The Observable abstract class does only know them via the interface Observer. Every Tickable can now register itself as an observer to the TimeTicker without a problem.

That being said you don’t even need CueList anymore as the list of observers is handled by the abstract class Observable.

I would use the Domain Events pattern (google it) to afford TimeTicker and CueList to collaborate.

When the time is right, TimeTicker could ‘raise’ to a register of handlers an event called Tick. The register of handlers then passes Tick event onto all registered implementations of

Handle<Tick>  { void Handle(Tick t); }

Now you have options:

  • CueList could implement Handle and tick its members.

  • each Cue could implement Handle for itself

  • or a general TickHandler class can get the CueList and update the cues

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Law of Demeter causing subclassing indecision and conflict with Single Responsibility

This is my situation:

I have a class called CueList, which is basically a wrapper around ArrayList. I have another class called TimeTicker that alerts all the Tickable (interface) in its list that a certain amount of time has passed. Since I need to tick all of the items in CueList, but I don’t want to violate DRY by keeping a copy of all of the CueList items in the TimeTickable class as well (which would make a nightmare when I want to delete a Cue), my immediate instinct was to make CueList a Tickable and have it pass on the “tick” message to all of its items.

This seems to violate the Single Responsibility principle, which says that CueList should not worry about both keeping track of all the cues AND Ticking them.

So what I did was create a CueManager class that is itself Tickable and links everything up (and ticks all of the CueList elements).

But now, because of the law of demeter, I have a bunch of duplicate methods in the CueManager that just mirror the TimeTicker and CueList class.

Doesn’t that mean that the CueManager class has multiple reasons to change? Doesn’t that mean it violates the Single Responsibility principle.

Also, since most of the methods in CueManager mirror TimeTicker and CueList, doesn’t that really mean that it IS a TimeTicker and CueList. Therefor shouldn’t it INHERIT from CueList and TimeTicker! (Obviously you can’t do multiple inheritance in Java, but I feel like it sort of makes sense to Inherit from CueList, but then that would violate Single Responsibility)

So in summary:

Law of Demeter makes me make a bunch of methods that merror two other classes, but that makes me think that it really should just inherit from both of those classes, but that would mean that one class handles multiple responsibilities (even if it is through its superclass).

2

CueList is an ArrayList (or at the very least can expose some similar interface such as Collection, List or Iterable). All items in that ArrayList are, ostensibly, Tickables. TimeTicker doesn’t, and shouldn’t, care that its items are in a CueList specifically, nor should it care that the items in the list it acts on are anything but Tickables. By the same token, CueList should have no knowledge of TimeTicker; it is of no bearing to CueList’s basic functionality AFAICT. This is the essence of the Least Knowledge Principle.

So, make TimeTicker act on an injected ArrayList<Tickable> (or a Collection<Tickable> or a List<Tickable> or an Iterable<Tickable>, whatever interface exposes the methods you use to spin through this list) and pass in CueList as this dependency, via some class that already has to know about both the list and the ticker.

Sounds like the Observer pattern where it is normal that the subject (TimeTicker) keeps a list of its observers (Tickables) so it can update them on an event. Your Tickable interface can define an UnRegister() method which you can call when it is removed from any list.

class TickableItem
{
    TimeTicker Subject;

    void UnRegister()
    {
        this.Subject.UnRegister(this);
    }

    void Register(TimeTicker subject) { ... }

    void OnTick() { ... }
}

The benefit of this approach is that your CueList doesn’t need to know who the TimeTicker is. It’s tickable items know who their TimeTicker’s are (they can be registered with different ones).

What you are describing is the Observer pattern and Java has already two nice things for that in store. There is the interface Observer and the abstract class Observable. If you use these two, then your TimeTicker does not handle the observer. TimeTicker extends from Observable and does only the stuff it needs to do. When a certain amount of time has passed, it calls the method setChanged and notifyObservers.

Your TimeTicker does know 0 Tickables (Observer). The Observable abstract class does only know them via the interface Observer. Every Tickable can now register itself as an observer to the TimeTicker without a problem.

That being said you don’t even need CueList anymore as the list of observers is handled by the abstract class Observable.

I would use the Domain Events pattern (google it) to afford TimeTicker and CueList to collaborate.

When the time is right, TimeTicker could ‘raise’ to a register of handlers an event called Tick. The register of handlers then passes Tick event onto all registered implementations of

Handle<Tick>  { void Handle(Tick t); }

Now you have options:

  • CueList could implement Handle and tick its members.

  • each Cue could implement Handle for itself

  • or a general TickHandler class can get the CueList and update the cues

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Law of Demeter causing subclassing indecision and conflict with Single Responsibility

This is my situation:

I have a class called CueList, which is basically a wrapper around ArrayList. I have another class called TimeTicker that alerts all the Tickable (interface) in its list that a certain amount of time has passed. Since I need to tick all of the items in CueList, but I don’t want to violate DRY by keeping a copy of all of the CueList items in the TimeTickable class as well (which would make a nightmare when I want to delete a Cue), my immediate instinct was to make CueList a Tickable and have it pass on the “tick” message to all of its items.

This seems to violate the Single Responsibility principle, which says that CueList should not worry about both keeping track of all the cues AND Ticking them.

So what I did was create a CueManager class that is itself Tickable and links everything up (and ticks all of the CueList elements).

But now, because of the law of demeter, I have a bunch of duplicate methods in the CueManager that just mirror the TimeTicker and CueList class.

Doesn’t that mean that the CueManager class has multiple reasons to change? Doesn’t that mean it violates the Single Responsibility principle.

Also, since most of the methods in CueManager mirror TimeTicker and CueList, doesn’t that really mean that it IS a TimeTicker and CueList. Therefor shouldn’t it INHERIT from CueList and TimeTicker! (Obviously you can’t do multiple inheritance in Java, but I feel like it sort of makes sense to Inherit from CueList, but then that would violate Single Responsibility)

So in summary:

Law of Demeter makes me make a bunch of methods that merror two other classes, but that makes me think that it really should just inherit from both of those classes, but that would mean that one class handles multiple responsibilities (even if it is through its superclass).

2

CueList is an ArrayList (or at the very least can expose some similar interface such as Collection, List or Iterable). All items in that ArrayList are, ostensibly, Tickables. TimeTicker doesn’t, and shouldn’t, care that its items are in a CueList specifically, nor should it care that the items in the list it acts on are anything but Tickables. By the same token, CueList should have no knowledge of TimeTicker; it is of no bearing to CueList’s basic functionality AFAICT. This is the essence of the Least Knowledge Principle.

So, make TimeTicker act on an injected ArrayList<Tickable> (or a Collection<Tickable> or a List<Tickable> or an Iterable<Tickable>, whatever interface exposes the methods you use to spin through this list) and pass in CueList as this dependency, via some class that already has to know about both the list and the ticker.

Sounds like the Observer pattern where it is normal that the subject (TimeTicker) keeps a list of its observers (Tickables) so it can update them on an event. Your Tickable interface can define an UnRegister() method which you can call when it is removed from any list.

class TickableItem
{
    TimeTicker Subject;

    void UnRegister()
    {
        this.Subject.UnRegister(this);
    }

    void Register(TimeTicker subject) { ... }

    void OnTick() { ... }
}

The benefit of this approach is that your CueList doesn’t need to know who the TimeTicker is. It’s tickable items know who their TimeTicker’s are (they can be registered with different ones).

What you are describing is the Observer pattern and Java has already two nice things for that in store. There is the interface Observer and the abstract class Observable. If you use these two, then your TimeTicker does not handle the observer. TimeTicker extends from Observable and does only the stuff it needs to do. When a certain amount of time has passed, it calls the method setChanged and notifyObservers.

Your TimeTicker does know 0 Tickables (Observer). The Observable abstract class does only know them via the interface Observer. Every Tickable can now register itself as an observer to the TimeTicker without a problem.

That being said you don’t even need CueList anymore as the list of observers is handled by the abstract class Observable.

I would use the Domain Events pattern (google it) to afford TimeTicker and CueList to collaborate.

When the time is right, TimeTicker could ‘raise’ to a register of handlers an event called Tick. The register of handlers then passes Tick event onto all registered implementations of

Handle<Tick>  { void Handle(Tick t); }

Now you have options:

  • CueList could implement Handle and tick its members.

  • each Cue could implement Handle for itself

  • or a general TickHandler class can get the CueList and update the cues

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật