You must share these responsibilities (for example, the ability to send a teacher). You can do this using a strategy template.
So, the teacher has an additional property Publishing, which serves as an interface. An implementation may have several implementations (for example, NoPublishing for a teacher who does not have publishing functions, or DefaultPublishing). Each teacher can have the Publishing property set to either NoPublishing or DefaultPublishing. If necessary, you can even change the runtime.
Example:
public class Teacher { public IPublishing Publishing { get; } } interface IPublishing { void Send(); } public NoPublishing : IPublishing { public void Send() {
Create a teacher:
var teacher = new Teacher();
Create a publisher strategy.
var defaultStrategy = new PublishDefault();
Plug them
teacher.Publishing = defaultStrategy;
Now you can send a message:
teacher.Publishing.Send();
Depending on which publishing strategy has been enabled, it will not send or send anything by default.
You only need to create an instance of each used publishing strategy once and reuse it for each Teacher (or even other classes that should be able to send).
If you need other publishing features, just add a new strategy (e.g. SmsPublishing, LetterPublishing, etc.).
You can even change the strategy on the fly if necessary (by reassigning the publication property).
Why not implement the interface not directly in Teacher?
- The principle of separation of ideas: IPublish contains a certain and different responsibility.
- IPublish may contain functionality that can be used later in different classes or even in other projects, so they can be reused.
- Testing is easier because IPublish does not need any knowledge of Teacher.
- The ability to change publisher behavior in real time.
(note: I don't have a compiler here, so the code is for explanation only).