vendredi 24 janvier 2020

What are some software architecture/design techniques for calling a method on a subset of list items?

Imagine I have the following interfaces:

public interface IEntity {...}
public interface IFreezable {...}
public interface IBurnable {...}

Let's also imagine I have a few classes:

public class Tree : IEntity, IBurnable {...}
public class Monster : IEntity, IFreezable {...}

Finally, let's say somewhere in my code I have a list of entities:

List<IEntity> entities = ...;

Let's say I would like to go through that list, calling IFreezable.Freeze(...) on items that implement IFreezable. I'm trying to employ interface segregation to keep different types of IEntities from having to implement methods they don't need/use.

What are some ways I could approach this and what are the trade-offs? I could use casting, but I have generally read that type checks and casting could be a sign you're approaching the problem the wrong way, so I wanted to get different perspectives.

Aucun commentaire:

Enregistrer un commentaire