Object-Oriented Design and Programming

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which principle of object-oriented design aims to minimize dependencies between different components by limiting interactions?

  • Polymorphism
  • Information Hiding (correct)
  • Inheritance
  • Encapsulation

What is the primary goal of applying the principle of 'Separation of Concerns' in software design?

  • To increase code duplication for clarity
  • To maximize dependencies between components
  • To create tightly coupled modules
  • To divide a program into distinct sections, each addressing a separate concern (correct)

In object-oriented programming, what does the term 'composition' refer to?

  • The creation of small, simple, specialized programs
  • The bundling of data and methods within a class
  • The ability of a class to inherit properties from another class
  • The construction of complex objects by combining simpler ones (correct)

Which of the following is a key benefit of object-oriented programming that promotes the ability to easily modify and update code?

<p>Scalability (C)</p> Signup and view all the answers

What design principle is violated when a derived class introduces behavior that is unexpected or alters the intended behavior of its base class?

<p>Liskov Substitution Principle (C)</p> Signup and view all the answers

Which of the following best describes the purpose of the Open/Closed Principle?

<p>Software entities should be open for extension but closed for modification. (D)</p> Signup and view all the answers

What is the primary goal of the Single Responsibility Principle in object-oriented design?

<p>Ensuring a class has a focused purpose and only one reason to change (C)</p> Signup and view all the answers

What is the purpose of refactoring in software development?

<p>Improving code without creating new functionality (D)</p> Signup and view all the answers

Which of the following is an example of a 'code smell' that indicates a potential problem in the design of a system?

<p>Duplicate Code (C)</p> Signup and view all the answers

Which design principle should be applied to address the 'Long Methods & Large Classes' code smell?

<p>Single Responsibility Principle (SRP) (B)</p> Signup and view all the answers

What is the main characteristic of the 'Feature Envy' code smell?

<p>A method accesses data of another object more than its own data. (B)</p> Signup and view all the answers

How does the 'Flyweight' design pattern improve performance?

<p>By reducing the number of objects created (D)</p> Signup and view all the answers

What is the primary intent of the 'Adapter' design pattern?

<p>To convert the interface of a class into another interface clients expect (C)</p> Signup and view all the answers

In the context of design patterns, what is the role of the 'Observer' pattern?

<p>To define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified (C)</p> Signup and view all the answers

Which design pattern is used to decouple an abstraction from its implementation so that the two can vary independently?

<p>Bridge (C)</p> Signup and view all the answers

What is the intent of the 'Factory Method' design pattern?

<p>Provide an interface for creating objects in a superclass, but allow subclasses to alter the type of objects that will be created. (D)</p> Signup and view all the answers

Which problem does the 'Prototype' design pattern primarily aim to solve?

<p>Specifying the kinds of objects to create using a prototypical instance (C)</p> Signup and view all the answers

What does the term 'YAGNI' (You Aren't Gonna Need It) refer to in software development?

<p>A guideline to avoid adding functionality before it is deemed necessary (A)</p> Signup and view all the answers

Which term describes classes that are similar but not substitutable due to differing supertypes?

<p>Alternative Classes with Different Interfaces (C)</p> Signup and view all the answers

What is the main characteristic of an 'Anemic Domain Model'?

<p>It contains only data and no behavior. (B)</p> Signup and view all the answers

What is the primary goal of the 'Chain of Responsibility' pattern?

<p>To avoid coupling the sender of a request to its receiver by giving multiple objects a chance to handle the request (D)</p> Signup and view all the answers

Which design pattern aims to reduce chaotic dependencies between objects by enforcing indirect communication?

<p>Mediator (D)</p> Signup and view all the answers

What is the intent of the 'State' design pattern?

<p>To allow an object to alter its behavior when its internal state changes (A)</p> Signup and view all the answers

What is the purpose of the 'Memento' pattern?

<p>To save and restore an object's previous state without violating encapsulation (B)</p> Signup and view all the answers

Which of the following is the primary goal of Package Cohesion Principles?

<p>Organizing related classes and components into a logical grouping (D)</p> Signup and view all the answers

What is the advantage of preferring composition over inheritance?

<p>It increases flexibility by assembling objects from components (B)</p> Signup and view all the answers

What is the primary purpose of the 'Remote Facade' pattern?

<p>To reduce network chatter by grouping multiple fine-grained calls into a single call (A)</p> Signup and view all the answers

How does the 'Lazy Loading' optimization technique improve system performance?

<p>By delaying the initialization of an object or resource until it's actually needed (D)</p> Signup and view all the answers

In the context of event-driven systems, what does 'Event Sourcing' refer to?

<p>Capturing all changes to an application's state as a sequence of events (C)</p> Signup and view all the answers

What is the main purpose of the 'Repository' pattern?

<p>To abstract data access, providing a clean separation between the domain and data mapping layers (A)</p> Signup and view all the answers

Which characteristic defines structural design patterns?

<p>They simplify complex systems through flexible class &amp; object composition. (B)</p> Signup and view all the answers

What is the objective of applying behavioral patterns?

<p>Handling object interaction, communication, and responsibility delegation (B)</p> Signup and view all the answers

Consider that you are building an application with complex object creation logic that is also repetitive; which creational pattern would fit?

<p>Factory method (D)</p> Signup and view all the answers

If you can only have one of a certain object, and need a single access point, which creational pattern should you use?

<p>Singleton (C)</p> Signup and view all the answers

Which structural pattern makes different interfaces work together?

<p>Adapter (B)</p> Signup and view all the answers

When you need to apply a class of responsibilities at run time, which behavioral pattern is the best choice?

<p>Strategy (C)</p> Signup and view all the answers

If an object alters its behavior based on its state, it implies which behavioral design pattern?

<p>State (D)</p> Signup and view all the answers

If you want an object to be able to be restored in the same state as some point in the past, which pattern would be a good choice?

<p>Memento (B)</p> Signup and view all the answers

Flashcards

Object-Oriented Programming

Designing software by structuring it around objects that combine data and methods.

Encapsulation

Bundling data and methods into a single unit, restricting direct access and providing controlled interaction.

Information Hiding

Hiding internal details of a component to limit interactions and protect object integrity.

Inheritance

Acquiring properties and behavior from a parent class, promoting code reuse and creating hierarchies.

Signup and view all the flashcards

Polymorphism

Implementing methods differently based on the object type, supporting method overriding.

Signup and view all the flashcards

Composition

Building objects by combining simpler ones, establishing a 'has-a' relationship.

Signup and view all the flashcards

Delegation

Assigning tasks to helper objects, promoting modularity and flexibility.

Signup and view all the flashcards

Abstraction

Hiding complex implementation details and only exposing essential functionalities.

Signup and view all the flashcards

Information Expert

Assigning responsibility to the class that has the information needed to fulfill it.

Signup and view all the flashcards

Separation of Concerns

Dividing a program into sections, each addressing a separate concern to enhance modularity.

Signup and view all the flashcards

Package Cohesion Principles

Organizing related classes and components into a logical grouping for clarity.

Signup and view all the flashcards

Package Coupling Principles

Minimizing dependencies between packages, fostering independent maintenance.

Signup and view all the flashcards

Favor Composition Over Inheritance

Favor building objects from other objects (composition) over inheriting from parent classes (inheritance).

Signup and view all the flashcards

Single Responsibility Principle

A class should have one, and only one reason to change.

Signup and view all the flashcards

Open/Closed Principle

Software entities should be open for extension, but closed for modification.

Signup and view all the flashcards

Liskov Substitution Principle (LSP)

If S is a subtype of T, then objects of type T may be replaced with objects of type S.

Signup and view all the flashcards

Duplicate Behavior

Duplicated code indicates same behavior is needed somewhere else.

Signup and view all the flashcards

Don't Repeat Yourself (DRY)

Write code once in a method, then call when needed.

Signup and view all the flashcards

Long Parameter Lists

A method being in the wrong class always requires data from somewhere else.

Signup and view all the flashcards

Divergent Change

When the same class is changed for different reasons.

Signup and view all the flashcards

Shotgun Surgery

The class is changed a lot for different reasons

Signup and view all the flashcards

Feature Envy

A method keeps accessing data from another class.

Signup and view all the flashcards

Polymorphism

Use polymorphism to refactor logic and make it maintainable.

Signup and view all the flashcards

Anemic Domain Model

A class contains only data and not behavior e.g. BankAccount containing balance and accountNo, with getters and setters.

Signup and view all the flashcards

Refused Bequest

Class inherits members it doesn't need

Signup and view all the flashcards

Speculative Generality

Replace inheritance with composition

Signup and view all the flashcards

God Class/Method

Class or method contains too many lines of code .

Signup and view all the flashcards

Temporary field

Class or instance fields that are temporarily set for method operations.

Signup and view all the flashcards

Message Chains or Train Wrecks

Reaching into an object's attributes to call the attribute's methods.

Signup and view all the flashcards

"How" Comments

If you need a comment to understand the code, rewrite it.

Signup and view all the flashcards

Clean Code

Code that is easy to read, understand, and maintain.

Signup and view all the flashcards

Code Smells

Indicators of problems that can be addressed during refactoring.

Signup and view all the flashcards

Design Patterns

Design patterns are typical solutions to common problems in software design

Signup and view all the flashcards

History of Patterns

Patterns describe repeatable solutions to commonly occurring problems in software design

Signup and view all the flashcards

Creational Patterns

Abstracts the instantiation process, decoupling object creation from client code.

Signup and view all the flashcards

Structural Patterns

Patterns Focus on class and object composition.

Signup and view all the flashcards

Behavioral Patterns

Patterns Concerned with object interaction, communication, and responsibility delegation.

Signup and view all the flashcards

Scope of Patterns

Class Scope

Signup and view all the flashcards

Scope of patterns

Object scope

Signup and view all the flashcards

Study Notes

  • Object-Oriented Design focuses on designing software using independent components to avoid combinatorial explosion, where complexity exponentially grows with each added element.
  • Objects function as mini-programs, each possessing its own data and operations, exemplified in Unix by programs like grep, ls, and cat, which can be combined.
  • OOP involves creating small, simple, and specialized programs and combining them into larger programs through composition.

Object-Oriented Programming

  • Object-Oriented Programming is a programming paradigm that structures software around "objects," combining data (attributes) and behavior (methods).
  • OOP mirrors real-world entities, promoting modular, reusable, and maintainable code.
  • Key benefits include modularity, reusability, scalability, easier maintenance, and enhanced security.

Encapsulation

  • Encapsulation bundles data and methods into a single unit, restricting direct access to certain components and enabling controlled interaction through methods.
  • Information hiding limits interactions between components, reducing combinatorial explosion and protecting object integrity by concealing internal details.
  • Objects should only expose what is necessary at the appropriate level, following a "need-to-know" basis.

Inheritance

  • Inheritance enables a class (child) to inherit the properties and behavior of another class (parent), promoting code reuse and creating hierarchies.
  • Extending or overriding parent class functionality is possible through inheritance.

Polymorphism

  • Polymorphism facilitates the implementation of methods that vary depending on the object type, supporting method overriding in derived classes and providing a unified interface for diverse object types.

Composition and Delegation

  • Composition builds objects by combining simpler ones, establishing a "has-a" relationship.
  • Delegation assigns tasks to helper objects instead of performing them directly, promoting modularity, flexibility, and avoiding complexities of deep inheritance hierarchies.

Abstraction

  • Abstraction hides complex implementation details, exposing only essential functionalities.

Information Expert

  • Information Expert assigns responsibilities to the class that possesses the necessary information to fulfill them.

Separation of Concerns

  • Separation of Concerns divides a program into distinct sections, each addressing a separate concern to enhance modularity and maintainability. It reduces complexity by separating unrelated functionality.

Package Coupling Principles

  • Package Coupling Principles minimize dependencies between packages, to foster independent development and maintenance and reduce the risk of ripple effects from changes, as well as encourage modular design.

Package Cohesion Principles

  • Package Cohesion Principles organizing related classes and components into a logical grouping, to ensure clarity and consistency within packages, facilitating easier maintenance and scalability.

Favor Composition Over Inheritance

  • Favor Composition over Inheritance recommends using composition ("has-a" relationship) over inheritance ("is-a" relationship) when designing systems Increases flexibility by assembling objects from components Reduces dependency on rigid inheritance hierarchy

Single Responsibility Principle

  • Single Responsibility Principle dictates that a class should have only one reason to change, that ensures a focused purpose, simplifies debugging and maintenance, and improves modularity and reduces code duplication

Liskov Substitution Principle (LSP)

  • Liskov Substitution Principle (LSP) demands that derived classes must be substitutable for their base classes without altering program correctness Ensures derived classes maintain the behavior of their base classes and promotes robust, extensible designs

Open/Closed Principle

  • Open/Closed Principle states that software entities (classes, modules, functions) should be open for extension but closed for modification Promotes scalability by allowing behavior to be extended without altering existing code Reduces the risk of introducing bugs into tested code

Adherence to LSP:

  • Adherence to LSP promotes code reuse and scalability, as developers can confidently extend functionality without altering existing code. Violation of LSP results in brittle systems where adding new derived classes or modifying behavior can introduce unexpected side effects.

Code Smells

  • Duplicate Behavior
    • Duplicate Behavior, known as "duplicate code", occurs when the same behavior is needed in multiple places and is resolved through copy-pasting.
    • Making changes requires modifying all instances of the copied code, potentially leading to overlooked spots and bugs.
    • The Solution: Don’t Repeat Yourself (DRY)

-Coding should occur in a method just once, then call when needed - Use composition or inheritance to reduce duplication, and use parameters or polymorphism to handle variations. -Updating logic only needs updating, and test only one spot -Easier code reading since it is broken up into descriptive methods Patterns: - Patterns include utility/helper class, template method, strategy, state, and layer supertype

  • Long Methods & Large Classes: -Long Methods & Large Classes result in scrolling and classes with many fields (e.g., god classes), leading to difficulties in understanding, maintaining, and reusing code. -This issue goes against OOP principles, which emphasize small, specialized components. -Each method should do one, simple thing with a descriptive name and no side-effects, while a class should be a specialized, cohesive unit with just a few fields.
  • Solutions: Solutions include breaking up long methods into smaller ones, breaking up large classes into two or more classes, and applying SRP, Information Expert, and Law of Demeter.
  • Primitive Obsession, Data Clumps, & Long Parameter Lists sticking with built-in types instead of making classes Data Clump: data that is often found together (resulting in long parameter lists)
  • Long Parameter Lists: -Hard to maintain -Sign of a method being in the wrong class as it always requires data from somewhere else. Solution: Move -Move method to class where most/all of the data it uses is found.
    • Applying Information Expert or Law of Demeter. Solution: Cohesion -Encapsulate a data clump into a meaningful class -Design Patterns: rich domain model, state
  • Divergent Change -Divergent Change marks when the same class is changed for different reasons. -"I will have to change these three methods every time I get a new database and I have to change these four methods every time there is a new financial instrument" -This breaks the principle of separation of principle Solution: Break up the class so it only does one concern such as business logic, persistence, presentation, transactions, security Shotgun Surgery -Shotgun Surgery is when multiple classes need to be modified to do one change
  • Opposite of divergent change Cohesion -Cohesion is encapsulating the things that change together into one class - Also applies to packages Feature Envy Method keeps accessing data from another class Solution: move the method to the class where the data it uses resides apply apply Information Expert or Law of Demeter Conditional Complexity Large that is difficult to maintain Polymorphism can be used to solve this issue By separating common logic from the special case logic, and creating a supertype (abstract class or interface) as placeholder for special case logic and creating subtypes to hold logic for each special case Strategy, state Parallel Inheritance Hierarchy occurs when every time class is added to one package, a class gets added to another package Ex: Customer CustomerService, Customer —> Order———————>OrderService,

Lazy Class, Middle Man & Dead Code

Lazy Class: doesn’t do much to justify its existence Middle Man: delegates to another class but doesn’t add its own value Dead Code: code no longer used Solutions: eliminate code that doesn’t justify existing, and apply cohesion Lazy Class: inline its behavior into other classes Middle Man: combine it with the class to which it delegates Dead Code: delete Alternative Classes with Different Interfaces -classes are similar but not substitutable, since they do not share the same supertype; Ex: CustomerFileData and CustomerDAO both store and retrieve info to/from something, but from different sources -Solutions include;choices have to be hard-corded, or messy conditionals need to be used Polymorphism (have the classes share a common supertype) Data Class or Anemic Domain Model -class that only contains data and not behavior Ex: BankAccount containing balance and accountNo, with getters and setters Balance should not be negative ATM withdrawals should not exceed 20,000 All transactions must be logged with a transaction ID -as seen in the example, all behavior needs to be done outside the object if it is an anemic domain model, making it errorprone. Solution: Rich Domain Model, Information Expert, place methods in class with the data they operate on -Remove unneeded getters and setters to preserve encapsulation -Refused Bequest when a class inherits members it does not need; Ok if unneeded members are encapsulated; Dangerous if the unneeded members are part of the interface

Speculative Generality when code is written to be more flexible than the present requirements need it to be, usually in anticipation of future requirements. Methods that try to accommodate any data type, Overloaded methods or Parameters that aren’t used Supertypes that only have one subtype. Middle Man, Lazy Class, Dead Code hard to understand and maintain Remove parameters etc God Class/Method; much of the code is concentrated in one class or method, or many other classes or methods depend on that one class or method. Solution: SRP, Information Expert, Law of Demeter

Temporary Field -dangerous in multithreaded environment, as multiple threads may be modifying the field in the middle of local variable

The Message Chains or Train wrecks - causes tight coupling, as calling class becomes tightly coupled to the internet don’t get the fields, and instead create a Refactoring a process used to convert a mess info clean code code that is easy to read, understand, and maintain patterns provide various techniques for software to easily resolve code with minimal issues is defined as code a Refactoring is code easy is spoted code is too hard to follow.

Design Patterns What is a pattern - Most patterns are described very formally and tested with common design problems

Why Classify Design Patterns?

  • Facilitate better the software Architecture?
  • Helps developers identify with the rite pattern
  • support the faster learning pattern on common GO
  • simplify the pattern section
  • What the patterns achieve on the surface.
  • where you can the code(Class-level compile time)
  • Object level - where to the code is used Elements included when the object is reused with the class/code -Creational Patterns. -Structural Patterns: class. -Behavioral Patterns

Three Main Categories of Design Patterns Reusable Software by the Gang of Four (GoF) . Creational Patterns are with the object of mechanisms

Structural Patterns Object composition patterns where the code is used can also be Object is code Behavioral Patterns How Object interactions work

Creational where and How object is being used or made (Object level Runtime) and the way that the Object code works, the object is coded with in (ClassLevel),(Behavioural) the object works runtime (Primary concern) Code in general for what and How Object is being used with (Key Goal) the relations ships with the code in general

How the Objects code outcome for abstract and and how collaboration is being worked out (Typical Outcome.) structural object - and also Clear collaboration for the way the code works is being made (Typical Outcome.)

  • Factory Method This allows the code to be altered and new one be made is code How Abstract

Factory is easy produces Classes can constrict complex objects using Builder using patterns and code in mind Class can be copy using Existing objects while making the code dependent with Prototype design -Class with code that can be shared using singleton allows objects to work together to solve issues

Is used to combine objects together with smaller objects Is use to change objects around when the code works and add new ones to it Where Objects can fit that is more valuable for its ram with its self and has multiple properties and code for it.

allows access to other the code for the code.

  • is Chain of Responsibility uses a pass chain together code to be the used again how can we set all the parameter for that coding structure. -Can object be a way to the to have access for other and for ever has a family together. of coding

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Object-Oriented Programming
25 questions
Object-Oriented Programming (OOP)
5 questions
Object-Oriented Programming Basics
8 questions

Object-Oriented Programming Basics

AgreeableCelebration775 avatar
AgreeableCelebration775
Use Quizgecko on...
Browser
Browser