English 中文(简体)
背弃义
原标题:dependency inversion implementation in javascript

“建筑”一书说:

When using dynamically typed languages like Ruby and Python ... dependency inversion does not require either the declaration or the inheritance of interfaces.

但我不明白,这本书没有举任何例子。

我的理解是,如果A级取决于违反DIP的B级,我们就可以引入一个InterfaceC,那么我们就获得这样的东西。

ClassA ---> InterfaceC <--- ClassB

I ve also looked at some examples on the internet, like this one that attempts dependency injection between PurchaseHandler and PayPal , https://duncan-mcardle.medium.com/solid-principle-5-dependency-inversion-javascript-7b054685f7cb

class PurchaseHandler {
    processPayment(paymentDetails, amount) {
        const paymentSuccess = PaymentHandler.requestPayment(
            paymentDetails,
            amount
        );

        if (paymentSuccess) {
            // Do something
            return true;
        }

        // Do something
        return false;
    }
}

class PaymentHandler {
    requestPayment(paymentDetails, amount) {
        // Complicated, PayPal specific logic goes here
        return PayPal.requestPayment(paymentDetails, amount);
    }
}

然而,这似乎只是增加了一个层次,导致这种依赖性图表:

PurchaseHandler ---> PaymentHandler ---> PayPal

这里没有发生任何转变......

问题回答

你们必须牢记,在 Java文中没有支持接口。 清洁建筑中的接口点是,A类必须依赖B类的一种方法,但这种方法的实施并不重要。 这使你能够在B类进行改动,而不影响A类。 接口只能确保B类的方法不存在执行方式。

举例来说,班级购房者取决于班级付款Handler,但如果你使用薪给或另一种支付方法,则无所作为。 你们可以改变支付方法,不再重写“购买者”。 这样,就达到了依赖性转移原则。

如果你想以更纯洁的方式来做,那么你就应该利用遗产:

采购Handler-> 支付Handler Child <- PayHandler_Parent

班级薪级 父母必须有方法要求 付款必须退还错误或其他意外情况。 然后使用继承财产,你必须按班级付款。 以所谓的要求支付方式支付儿童费用。 这样,你就不得不采用这一方法,因为它是另一个方案语言的接口。 班级购得商必须指在付款Handler_Child实施的方法。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签