many dependencies in switch context in strategy design pattern

sina

i used strategy design pattern and i have a problem with many dependencies while instantiating in passed cases in switch . is there any a clean solution for not passing these dependencies?

this my credit service:

class CreditService
{
    public function addCredit($transaction)
    {
        $creditContext = new CreditContext();
        $creditStrategy = $creditContext->setStrategy($transaction->transaction_type);
        $creditStrategy->add($transaction->id);
    }
}

and this is my CreditContext with many dependencies in EmployerTransactionService and in TransactionService

class CreditContext
{
    /**
     * @param $strategy
     * @return InvoiceStrategy|UserStrategy
     * @throws \Exception
     */
    public function setStrategy($strategy)
    {
        switch ($strategy) {
            case User::class:
                return new UserStrategy(
                    new EmployerTransactionService(new TransactionService(dependencies..),dependencies..);
            case Proposal::class:
            case Milestone::class:
                return new InvoiceStrategy(
                    new TransactionService(
                        new PaymentService(new PaymentRepository(), new CreditRepository(), new TransactionRepository()),
                        new TransactionRepository(),
                        new CreditRepository(),
                        new IncomeReportService(new IncomeReportRepository()),
                        new CreditService())
                );
            default:
                throw new \Exception('not found strategy');
        }
    }
}
AH.Pooladvand

You have to use Dependency Injection Container which is the solution to all your problems

Your final code ultimately will be look like this

class CreditContext
{
    protected $strategies = [
        User::class,
        Proposal::class,
        Milestone::class
    ];

    public function make($type)
    {
        if (in_array($type, $this->strategies, true)) {
            $container = Container::getInstance();

            return $container->get($this->strategies[$type]);
        }

        throw new RuntimeException('Strategy not found');
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python design pattern for many conditions

Strategy Pattern and context class

what is context object design pattern?

Class with too many parameters: better design strategy?

Which design pattern to use strategy or factory design?

Using strategy design pattern with an abstract parameter

Duck example strategy pattern - Head first design pattern

Design pattern - Strategy and Bridge (Overlap in design)

Confused about strategy design pattern

What is the difference between Strategy and Factory design pattern?

JAVA - extends vs interface - Strategy design pattern

Design Pattern for Context-sensitive representation

Caching Strategy/Design Pattern for complex queries

How to correctly implement strategy design pattern

Is this following the strategy design pattern

How do I use Strategy Pattern in this context?

Strategy Design Pattern

Strategy design pattern - stopping the method

How is this design pattern called? (map instead of switch)

Strategy Design Pattern for handling requests

is Context in Android an implementation of a complex Strategy design pattern?

design pattern like electricity switch?

The Strategy design pattern vs simple Interface abstraction?

Component/Strategy Pattern in C++, Design and Implementation

Strategy pattern or no strategy pattern?

Test Automation - Page Object - Strategy Design Pattern

Avoid Service locator in strategy design pattern

Question on diamond operator for design pattern strategy

How to prevent a context class using the state design pattern from having too many responsibilities?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive