1/33
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
abstraction
hiding implementation details while exposing only essential features
Encapsulation
protects object integrity by controlling access to data
Tight coupling
class depends on specific methods or fields of another class
Low coupling
minimizes dependencies between classes, making changes in one class less liley to impact others
abstraction and encapulsation how they related and different
They are related in a way that encapsulation helps achieve abstraction.
They are different in the sense that abstraction focuses on hiding complexity of the system and encapsulation focusses on specifically restricting access to data or methods in a clas
Private keyword
demonstrates encapsulation. Encapsulation restricts direct access to the internal state of an object and only allows controlled access through public methods (deposit, withdraw, getBalance).
high cohesion
High cohesion because the class only deals with balance (focused on one responsibility).
This
keyword used to indicate the current object's members
protected
accessible to subclasses or classes in the same
Package
super
a reference variable that points to the parent class object. It is used in a subclass to access member
Liskov Substitution Principle
Violated when a subclass modifies behavior in a way that contradicts the expectations sets by its parent class
Composition over inheritance improve software design
it allows code to be more flexible by favoring delegation over hierarchy
SRP violation
Class has more than one reason to change such as
generating invoices, printing, and sending them
Model in MVC controller
stores the application's data
Inheritance vs compositiobn
composition main class has subclass. so car has engine
class Car { public Engine engine;
Car(Engine engine){ this.engine = engine; }
void drive(){ System.out.println("Car is driving..."); }}
DIP
high level modules should not depend on low level modules.
for example,
The Computer class directly depends on the concrete Keyboard class.
class Computer {
private Keyboard keyboard;
Computer(Keyboard keyboard){this.keyboard = keyboard;}
void use() { keyboard.type(); }
}
Interface Segregation Principle
a class should not be forced to implement interfaces it doesn't need to
Open/Closed Principle
a class should be open for extension butclosed for modification
Dependency Inversion Principle
objects should depend on abstractions instead of concrete implementations
Abstract data types
Definition: A collection of well-defined behaviors, irrespective of implementation details• Examples: Lists, maps, sets, stacks, queues
Model-View-Controller
a pattern which supports separation
of concerns for applications with user interfaces.
View
Allows users to input data and displays new data from the
model
Controller
Connects the view and model by receiving user
inputs, processing data, updating the data in the model, then
triggering a refresh of the view.
Singleton
creational pattern controls object instantiation at the cost of introducing global state
Builder
creational pattern is best suited for constructing an object where multiple configuration steps are required
primary benefit of using creational design patterns
They abstract the process of object creation, making code more flexible and reusable.
primary difference between the Factory Method and Abstract Factory patterns?
Factory Method creates a single product, while Abstract Factory creates a family of
related products.
Open/Closed Principle (OCP) relate to the Factory Method pattern
Factory method aligns with OCP by allowing new product types be introduced without modifying existing code.
Instead of changing the factory or client code, new implementations can be added by extending the factory.
to implement a Singleton, but it contains two issues. Identify and explain the problems
public instance and public constructor should be private. synchronized should be used to handle concurrency in the getInstance function.
what is wrong with book code.
Book is not TableOfContents, book contains TableOfContents. So composition should be used instead of inheritance. Fix is to make TableOfContents a member variable inside Book. It is good practice to initialize the member variable using dependency injection. Below is corrected code.
public class Book {
private String title;
private String author;
private TableOfContents tableOfContents;
public Book(String title, String author, TableOfContents tableOfContents) {
this.tableOfContents = tableOfContents;
this.title = title;
this.author = author;
}
distinguishes structural design patterns from other GoF pattern categories?
They emphasize how objects and classes are composed to form larger structures.
What is the primary motivation behind using an Adapter pattern?
To enable two incompatible interfaces to work together without modifying their source code.
best describes the Flyweight pattern?
It reduces memory usage by sharing common parts of object state
How does the Bridge pattern help manage the complexity when a class must vary along two or more independent dimensions (e.g., 3 subclasses for one aspect and 7 for another)
It separates one dimension into a distinct hierarchy using composition, thereby avoiding a combinatorial explosion of subclasses