Design Patterns and Principles Flashcards
What modifiers are automatically added to interface methods?
public, to all methods
abstract, to all methods that are non-static and non-default
Functional Interface
An interface that contains a single abstract method
Note: Can contain any number of static or default methods as well, as long as there is exactly one ABSTRACT method
@FunctionalInterface
Indicates that the following interface should be a functional interface.
If the interface has more than one abstract method or no abstract methods at all, the code will not compile.
Note: Functional interfaces can have any number of default and static methods, it’s just required that they have exactly one abstract method
java.util.function.Predicate interface?
An interface that you can use for the common need to use a lambda that returns a boolean
public interface Predicate {
public boolean test(T t);
}
is‐a relationship
the property of an object being an instance of a data type
has‐a relationship
the property of an object having a named data object or primitive as a member
Design Pattern
an established general solution to a commonly occurring software development problem
Creational Patterns
a type of software design pattern that manages the creation of objects within an application
Level of Indirection
a general term for solving a software design problem by conceptually separating the task into multiple levels.
Singleton Pattern
a creational pattern focused on creating only one instance of an object in memory within an application, sharable by all classes and threads within the application.
Singleton
The object created with the singleton pattern.
They should be private static, and will often be called “instance”
The constructor in the class should be private
public class HayStorage {
private HayStorage() {} private static final HayStorage instance = new HayStorage(); public static HayStorage getInstance() { return instance; } }
Lazy Instantiation
Creating a reusable object the first time it is requested rather than when the class is first used
Reduces memory usage and improves performance when an application starts up.
Immutable Object Pattern
a creational pattern based on the idea of creating objects whose state does not change after they are created and can be easily shared across multiple classes
1) Use a constructor to set all properties of the object.
2) Mark all of the instance variables private and final.
3) Don’t define any setter methods.
4) Don’t allow referenced mutable objects to be modified or accessed directly.
5) Prevent methods from being overridden.
The type of the object determines…
which properties exist within the object in memory
The type of the reference to the object determines…
which methods and variables are accessible to the Java program