我听说Liskov替换原则(LSP)是面向对象设计的基本原则.它是什么以及它的使用例子是什么?
oop liskov-substitution-principle definition design-principles solid-principles
使用Swift,来自Java背景,为什么要选择Struct而不是Class?看起来它们是相同的,使用Struct提供更少的功能.为什么选择呢?
我对Liskov替换原则的理解是,对于派生类,基类的某些属性是真的或某些实现的基类行为.
我想这意味着当一个方法在基类中定义时,它永远不应该在派生类中被覆盖 - 因为那么替换基类而不是派生类会产生不同的结果.我想这也意味着,拥有(非纯)虚拟方法是件坏事吗?
我想我可能对这个原则有错误的理解.如果我不这样做,我不明白为什么这个原则是好的做法.谁可以给我解释一下这个?谢谢
liskov-substitution-principle design-principles solid-principles
开放/封闭原则规定软件实体(类,模块等)应该是可以扩展的,但是对于修改是封闭的.这意味着什么,为什么它是良好的面向对象设计的重要原则?
oop definition design-principles open-closed-principle solid-principles
有人能给我一个单一责任原则的例子吗?我试图理解,在实践中,一个班级有一个单一的责任,因为我担心我可能每天都违反这条规则.
oop single-responsibility-principle definition design-principles solid-principles
在我工作的许多项目中,每当我必须返回一个只读集合时,我使用该IEnumerable<T>
接口并使其类型特定如下:
Public ReadOnly Property GetValues() As IEnumerable(Of Integer)
Get
'code to return the values'
End Get
End Property
Run Code Online (Sandbox Code Playgroud)
大多数时候,我返回一个List但是在一些函数和只读属性中我返回一个数组,这个数组也可以通过扩展方法的礼貌来达到目的.
我的问题是我会通过返回违反任何设计原则IEnumerable<T>
s,而不是特定类型(例如:List<T>
,HashSet<T>
,Stack<T>
或Array
S) ?
这是关于管道实施的设计问题.以下是我的天真实施.
管道中各个步骤/阶段的接口:
public interface Step<T, U> {
public U execute(T input);
}
Run Code Online (Sandbox Code Playgroud)
管道中步骤/阶段的具体实现:
public class StepOne implements Step<Integer, Integer> {
@Override
public Integer execute(Integer input) {
return input + 100;
}
}
public class StepTwo implements Step<Integer, Integer> {
@Override
public Integer execute(Integer input) {
return input + 500;
}
}
public class StepThree implements Step<Integer, String> {
@Override
public String execute(Integer input) {
return "The final amount is " + input;
}
}
Run Code Online (Sandbox Code Playgroud)
管道类将保存/注册管道中的步骤并一个接一个地执行它们:
public class Pipeline {
private …
Run Code Online (Sandbox Code Playgroud) 接口隔离原则(ISP)表示许多客户端特定接口优于一个通用接口.为什么这很重要?
java oop design-principles solid-principles interface-segregation-principle
当大型项目由于共享突变而崩溃时,多任务处理似乎是一场灾难.我会说共享资源是由多个线程访问的.调试和跟踪bug的来源以及导致它的原因变得非常困难.它让我问,是否有任何设计模式,可以在设计multithreaded
程序时使用?
我非常感谢您对此的看法和意见,如果有人能够提出可以遵循的良好设计实践,以使我们的程序线程安全,那将是一个很大的帮助.
multithreading design-patterns design-principles multitasking
在C++类中使用const引用字段作为只读getter是否合适?
我的意思是,这段代码是否符合良好做法?
class check{
private:
int _x;
public:
const int& x = _x;
void setX(int v){
_x = v;
}
};
Run Code Online (Sandbox Code Playgroud)
它非常像C#属性,恕我直言,并且在类使用代码中非常简单和干净:
check s;
int i;
std::cin >> i;
s.setX(i);
std::cout << s.x << '\n';
s.setX(7);
// s.x = i; // Error
std::cout<<s.x<<'\n';
Run Code Online (Sandbox Code Playgroud) oop ×6
definition ×3
java ×2
liskov-substitution-principle ×2
.net ×1
c# ×1
c++ ×1
class ×1
interface-segregation-principle ×1
multitasking ×1
properties ×1
reference ×1
single-responsibility-principle ×1
struct ×1
swift ×1
vb.net ×1