在问这个问题之前我想说的是问题与我的问题非常相似,但概念仍然不清楚,非常令人困惑。
\n\n我试图理解依赖倒置原理,但我无法完全理解它?
\n\n下面是DIP说的两点
\n\n\n\n\nA. 高层模块不应该依赖于低层模块。两者都应该依赖于抽象。B. 抽象不应依赖于细节。细节应该取决于抽象。
\n
我能够理解第一点,但无法理解第二点,看起来两者是相同的。在 stackoverflow 和其他网站进行大量搜索后,我可以理解两者都试图说不同的事情,但我无法理解。
\n\n让\xe2\x80\x99s 考虑一个例子:
\n\n让\xe2\x80\x99s考虑SalaryCalculator类[高级模块],它用于计算员工的工资。其中使用BonusCalculator [高级模块]来计算工资,如下所示。由于 SalaryCalculator 使用 BonusCalculator,它\xe2\x80\x99s 违反了\xe2\x80\x9c 的第一点,高级模块不应依赖于低级模块。两者都应该依赖于抽象\xe2\x80\x9d。
\n\n\n\n因此我们在两者之间引入了抽象,如下所示:
\n\n\n\n这里的细节[低级和高级模块]依赖于抽象,而抽象不依赖于细节。那么在 DIP 中,第二点试图说明什么?\n 如果两者相同,为什么将其分为两点?
\n\n如果有人给我一个代码示例,那将非常有用。
\noop inversion-of-control design-principles solid-principles dependency-inversion
我已经编写了UserService关于用户的类(在逻辑层,而不是持久层),它包含这些方法。
具有这些方法的此类是否违反了SRP?
python
class UserService:
repository: Repository
def create(...):
self.repository.save(...)
def patch(...):
self.repository.patch(...)
def delete(...):
self.repository.delete(...)
def get_one(...):
return self.repository.get(...)[0]
def get_list(...):
return self.repository.save(...)
Run Code Online (Sandbox Code Playgroud)
如果这有很多职责我该如何分配课程?
我一直在练习如何使用 SOLID 编写干净的代码。我还在代码中使用 DI 来消除耦合,但只能通过构造函数注入。在我使用过 DI 的很多情况下,我只是用它来调用方法。我还不明白的是,当您有一个依赖类,其构造函数在另一个类中接受参数时,如何解耦。如果var obj = new A(month)在 B 类内部创建依赖关系和紧密耦合,我如何解耦/抽象它?这就是属性接口的用武之地吗?如果是这样,我该如何在这里使用它?
public class A
{
private string _month;
public A(string month)
{
_month = month;
}
}
public class B
{
public List<A> ListOfMonths;
public B()
{
ListOfMonths = new List<A>();
}
public List<A> SomeMethod()
{
string[] months = new []
{
"Jan",
"Feb",
"Mar"
};
foreach(var month in months)
{
var obj = new A(month); // If this is coupling, how do I remove it? …Run Code Online (Sandbox Code Playgroud) 只是对 Stack Overflow 和 Microsoft 开发社区的一个想法和一个关于称为 SOLID 的 OO 软件设计原则的问题。请问Liskov替换原则和依赖倒置原则有什么区别?我已经考虑了一段时间,但我不确定其中的区别。请问你能告诉我吗?非常欢迎任何想法/反馈。
liskov-substitution-principle solid-principles dependency-inversion
开放/封闭原则规定类对于修改是封闭的,但对于扩展是开放的。假设我们想要设计一个支付系统,其中支付可以由多个处理器处理,如下所示:
class Payment {
void pay(paymentMethod) {
switch (paymentMethod) {
case 'PayPal':
break;
case 'Swift':
break;
default:
break;
}
}
}
class PayPal {
void pay() {
//implement payment1
}
}
class Swift {
void pay() {
//implement payment2
}
}
Run Code Online (Sandbox Code Playgroud)
假设我们是第一次实施这两种支付系统。现在如果由于某种原因任何支付系统的实现过程发生了变化,我们是不是应该修改相关的类?例如,如果我们实现PayPal,2-3年后PayPal的工作流程发生了变化,修改PayPal类是否会破坏开放/封闭原则?如果解决的话有什么解决办法?
如果您考虑将inner_multiply其作为 的初始化程序multiply,那么您是否应该使它们松散耦合并使用 DI 初始化程序(或任何其他方式),特别是如果您需要多个初始化程序?或者我误解了 FP 中柯里化函数的基本概念?
def inner_multiply(x):
def multiply(y):
return x * y
return multiply
def curried_multiply(x):
return inner_multiply(x)
multiply_by_3 = curried_multiply(3)
result = multiply_by_3(5)
print(result) # Output: 15 (3 * 5)
Run Code Online (Sandbox Code Playgroud) python functional-programming dependency-injection currying solid-principles
在我写的应用程序中,我有一个Policy类.有4种不同类型的政策.每个策略都针对其他策略进行加权,例如PolicyA> PolicyB> PolicyC> PolicyD.
谁负责实施逻辑来确定一个政策是否比另一个更好?我最初的想法是重载>和<运算符并在Policy类型本身中实现逻辑.
这会违反SRP吗?
如果我有类似的东西
class square : figure {}
class triangle : figure {}
Run Code Online (Sandbox Code Playgroud)
这是否意味着我永远不应该使用方形和三角形类,而只是参考图?
就像从来没有这样:
var x = new square();
Run Code Online (Sandbox Code Playgroud) 在以下示例中,AccountService和ProductService位于ASP.NET MVC应用程序中.该AccountWebAPI和ProductWebAPI是外部托管API的微服务.
1)我可以消除ProductService并协调检索CustomerAccountController本身的订单吗?这是因为我将Controller视为DDD(域驱动设计)中提到的应用层/服务.
2)我是否违反了n层架构,因为ProductService调用的AccountService是同一层?
3)由于AccountWebAPI和ProductWebAPI是微服务,它们是否必须在客户端应用程序(MVC App)中作为AccountService和ProductService分开,以保持责任分离?因此,ProductService需要重命名为ProductAppService,ProductService应该与ProductWebAPI交互,就像AccountService与AccountWebAPI的对话一样.
public class CustomerAccountController : Controller
{
IProductService _productService;
public CustomerAccountController(IProductService productService)
{
_productService = productService;
}
public IActionResult Index()
{
return View();
}
public IActionResult Account(int customerId)
{
var orders …Run Code Online (Sandbox Code Playgroud) 考虑这个ruby示例
class Animal
def walk
# In our universe all animals walk, even whales
puts "walking"
end
def run
# Implementing to conform to LSP, even though only some animals run
raise NotImplementedError
end
end
class Cat < Animal
def run
# Dogs run differently, and Whales, can't run at all
puts "running like a cat"
end
def sneer_majesticly
# Only cats can do this.
puts "meh"
end
end
Run Code Online (Sandbox Code Playgroud)
方法是否sneer_majesticly违反LSP,仅在Cat上定义,因为Animal上没有实现这个接口也不需要它?
solid-principles ×10
liskov-substitution-principle ×3
oop ×3
c# ×2
python ×2
single-responsibility-principle ×2
currying ×1
n-layer ×1
ruby ×1