How to handle additional parameters for different implementations of an interface in Java?

I am working on a Spring Boot project where I have a Notifier interface with a send(String message) method. Different implementations of Notifier, such as FCMNotifier and SNSNotifier, require additional parameters to send notifications (e.g., tokens, topics, etc.).

Here is an example of my interface and two implementations:

`public interface Notifier {
void send(String message);
}

@Component
public class FCMNotifier implements Notifier {

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Override
public void send(String message) {
// Use token to send message via FCM
// need token
}
</code>
<code>@Override public void send(String message) { // Use token to send message via FCM // need token } </code>
@Override
public void send(String message) {
    // Use token to send message via FCM
   // need token

}

}

@Component
public class SNSNotifier implements Notifier {

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Override
public void send(String message) {
// Use topicArn to send message via SNS
// need topicArn
}
</code>
<code>@Override public void send(String message) { // Use topicArn to send message via SNS // need topicArn } </code>
@Override
public void send(String message) {

    // Use topicArn to send message via SNS
   // need topicArn
}

}
`

As you can see, each implementation requires additional parameters (token for FCMNotifier, and topicArn for SNSNotifier). I want to make this design more flexible and avoid tightly coupling the parameters to specific implementations.

My Question:
What is the best way to handle such additional parameters for different implementations in a scalable and maintainable way?
Should I use something like a Context object to pass parameters dynamically? Or are there better design patterns to solve this?
Should I add a builder to handle common requests instead?

I tried using both the Builder pattern and the Decorator pattern to improve flexibility and scalability. I expected the Builder pattern to help in constructing notifications with different parameters (like tokens or topics) dynamically, and the Decorator pattern to allow me to layer additional behaviors (e.g., logging or retry mechanisms) around the notification process. However, I am not sure if this is the right approach or if there are better patterns or practices I should consider for this use case.

New contributor

KzeroJun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Sounds like they don’t have a common interface if additional parameters are needed.

My first thought is: Don’t overcomplicate it.

If you make the common method send(), without parameters, you can pass the different parameter types into a constructor as member variables. Each type of Notifier can handle them as they wish.

The downside to this approach is that you need to create an instance every time you want to send a notification.

Java developers love patterns and object hierarchies. Sometimes we get carried away. Do something simple and only add complexity when necessary.

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

How to handle additional parameters for different implementations of an interface in Java?

I am working on a Spring Boot project where I have a Notifier interface with a send(String message) method. Different implementations of Notifier, such as FCMNotifier and SNSNotifier, require additional parameters to send notifications (e.g., tokens, topics, etc.).

Here is an example of my interface and two implementations:

`public interface Notifier {
void send(String message);
}

@Component
public class FCMNotifier implements Notifier {

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Override
public void send(String message) {
// Use token to send message via FCM
// need token
}
</code>
<code>@Override public void send(String message) { // Use token to send message via FCM // need token } </code>
@Override
public void send(String message) {
    // Use token to send message via FCM
   // need token

}

}

@Component
public class SNSNotifier implements Notifier {

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Override
public void send(String message) {
// Use topicArn to send message via SNS
// need topicArn
}
</code>
<code>@Override public void send(String message) { // Use topicArn to send message via SNS // need topicArn } </code>
@Override
public void send(String message) {

    // Use topicArn to send message via SNS
   // need topicArn
}

}
`

As you can see, each implementation requires additional parameters (token for FCMNotifier, and topicArn for SNSNotifier). I want to make this design more flexible and avoid tightly coupling the parameters to specific implementations.

My Question:
What is the best way to handle such additional parameters for different implementations in a scalable and maintainable way?
Should I use something like a Context object to pass parameters dynamically? Or are there better design patterns to solve this?
Should I add a builder to handle common requests instead?

I tried using both the Builder pattern and the Decorator pattern to improve flexibility and scalability. I expected the Builder pattern to help in constructing notifications with different parameters (like tokens or topics) dynamically, and the Decorator pattern to allow me to layer additional behaviors (e.g., logging or retry mechanisms) around the notification process. However, I am not sure if this is the right approach or if there are better patterns or practices I should consider for this use case.

New contributor

KzeroJun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Sounds like they don’t have a common interface if additional parameters are needed.

My first thought is: Don’t overcomplicate it.

If you make the common method send(), without parameters, you can pass the different parameter types into a constructor as member variables. Each type of Notifier can handle them as they wish.

The downside to this approach is that you need to create an instance every time you want to send a notification.

Java developers love patterns and object hierarchies. Sometimes we get carried away. Do something simple and only add complexity when necessary.

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