Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a design pattern for this?

I have a component that needs to call a specific service depending on the input it receives. So my component has to look at the input and based on a configuration that says "for this input call this service with this data" needs to call the proper service. The services have a common signature method and a specific one (each).

I thought about an abstract class that includes the signatures for all three methods. The implementation for the two services will override all three methods (throwing NotImplementedException for the methods that are not supported by current service). A component that could be initialized with a map (that for each input type will have the type of the service to be called) will also be defined.

Do you have a better approach to cope this scenario ?

like image 665
ytrewq Avatar asked May 03 '10 17:05

ytrewq


People also ask

How do you know which design pattern to use?

To use design patterns effectively you need to know the context in which each one works best. This context is : Participants — Classes involved. Quality attributes — usability, modifiability, reliability, performance.

What design patterns are there?

As per the design pattern reference book Design Patterns - Elements of Reusable Object-Oriented Software , there are 23 design patterns which can be classified in three categories: Creational, Structural and Behavioral patterns. We'll also discuss another category of design pattern: J2EE design patterns.

Are there more than 23 design patterns?

There are 23 official patterns from the book Design Patterns - Elements of Reusable Object-Oriented Software, which is considered one of the most influential books on object-oriented theory and software development.


1 Answers

Factory pattern has this definition:

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses

Sounds like what you want, right?

like image 154
JB King Avatar answered Oct 24 '22 17:10

JB King