SOLID Principles

Ace your homework & exams now with Quizwiz!

What is the Open/Closed Principle?

"software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification"[Bertrand Meyer-1988]. That is, such an entity can allow its behavior to be modified without altering its source code.

What is the Liskov Substitution Principle?

...is the notion that "objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program" Liskov's notion of a behavioral subtype defines a notion of substitutability for mutable objects; that is, if S is a subtype of T, then objects of type T in a program may be replaced with objects of type S without altering any of the desirable properties of that program (e.g., correctness)

What constitutes Bad Software Design?

A piece of software that fulfills its requirements and yet exhibits any or all of the following three traits has a bad design: • Rigidity: It is hard to change because every change affects too many other parts of the system • Fragility: When you make a change, unexpected parts of the system break • Immobility: It is hard to reuse in another application because it cannot be disentangled from the current application

What is the Dependency Inversion Principle?

A specific form of decoupling where conventional dependency relationships established from high-level, policy-setting modules to low-level, dependency modules are reversed for the purpose of rendering high-level modules independent of the low-level module implementation details. • High-level modules should not depend on low-level modules; both should depend on abstractions • Abstractions should not depend on details; details should depend on abstractions

What is DRY?

Don't Repeat Yourself - AKA: Duplication is Evil (DIE) • "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system" [The Pragmatic Programmer] • Hunt and Thomas apply it quite broadly to include "database schemas, test plans, the build system, even documentation" • When applied successfully, a modification of any single element of a system does not require a change in other logically-unrelated elements. Additionally, elements that are logically related all change predictably and uniformly, and are thus kept in sync

What is the Single Responsibility Principle?

Every object should have a single responsibility, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility.

What are SOLID Principles?

Five basic principles of OO programming and design. The principles, when applied together, encourage a programmer to create a system that is extensible and easy to maintain over time.

Define Covariance & Contravariance

In it's simplest form, covariance, contravariance and invariance describe type "assignment compatibility": • Covariant: class A is a covariant of class B when class A is a subtype of class B - class of type A can be assigned to an variable of class type B. Use out keyword in c# to indicate covariance. Often used in return type assignment compatibility. • Contravariant: class A is a contravariant of class B when class A is a supertype of class B - class of type B can be assigned to a variable of class type A. Use in keyword in c# to indicate contravariance. Often used in method parameter assignment compatibility. • Invariant: class A is neither a subtype nor a supertype of Class B - no assignment can be made between the two types.

What is a critique of the Single Responsibility Principle?

May make code more complex and decomposed for an eventuality that never comes.

Describe Meyer's original implementation inheritance version of the Open/Closed Principle.

• A classes implementation should only be modified to correct errors; new or changed features would require a subclass be created • The subclass uses current functionality by inheritance • The subclass interface need not implement the original classes interface • Meyers has been replaced by Martins polymorphic interface version

What is the Interface Segregation Principle?

• Classes that implement interfaces should not be forced to implement methods they do not use. Another way of putting it is: use small interfaces, not fat ones • Focuses on the cohesiveness of interfaces with respect to the implementors that use them • Keep each implementation independent of interfaces that they do not use. If you need to change one interface, you shouldn't need to change the other

What are the standard signature requirements of the Liskov Substitution Principle?

• Contravariance of method arguments in the subtype • Covariance of return types in the subtype • No new exceptions should be thrown by methods of the subtype, except where those exceptions are themselves subtypes of exceptions thrown by the methods of the supertype

Describe the Open/Closed Principle.

• Meyer's original principle is redefined to refer to the use of interfaces, where multiple implementations could be created and polymorphically substituted for one another. • Interface specifications are reused through inheritance; implementation need not be reused. • The existing interface is closed to modification and new implementations must, at a minimum, implement that interface.

What are the behavioral conditions that a subtype must meet in the Liskov Substitution Principle?

• Preconditions cannot be strengthened in a subtype • Postconditions cannot be weakened in a subtype • Invariants of the supertype must be preserved in a subtype • History constraint (the "history rule"). Objects are regarded as being modifiable only through their methods (encapsulation) • Subtypes may introduce methods that are not present in the supertype. However, state changes in the subtype must be invariant with those that can occur in the supertype. • Fields added to the subtype may be safely modified because they are not observable through the supertype methods.

Describe the Single Responsibility Principle.

• Robert Martin defines a responsibility as a reason to change, and concludes that a class or module should have one, and only one, reason to change. It is a bad design to couple two things that change for different reasons at different times. • Example: class that compiles and prints a report. These are two different responsibilities each of which can change for different reasons. • When adding behavior, helps us clearly decide whether to extend the object or create another object (whether the behavior is inside or outside the object) • If we have to describe the object with and or or then we should break out coherent responsibilities into one or more new classes

What are the five principles?

• Single Responsibility Principle • Open/Closed Principle • Liskov Substitution Principle • Interface Segregation Principle • Dependency Inversion Principle


Related study sets

Palabras que terminan en -tad/-dad - Femeninas

View Set

Anxiolytic and Hypnotic Agents; Chapter 20

View Set

Civil Rights and Society: Minersville School Board v. Gobitis (1940)

View Set

Macroeconomics Exam 2 Study Guide

View Set

Chapter 1: Becoming a Public Speaker

View Set

تصميم خرساني مسلح2

View Set

Ch. 45 NCLEX Review- Cerebral Dysfuntion

View Set