Please enable JavaScript.
Coggle requires JavaScript to display documents.
Chapter 02. DESIGN PATTERNS AND PRINCIPLES (Working with Design Patterns…
Chapter 02. DESIGN PATTERNS AND PRINCIPLES
Understanding Design Principles
Encapsulating Data
No actor other than the class itself should have direct access to its data
Creating JavaBeans
Rules
Properties
are
private
Getter
for
non-boolean
properties begins with
get
Getter
for
boolean
properties may begin with
is
or
get
.
Setter
methods begin with
set
The method name must have a prefix of set/get/is followed by the first letter of the property in uppercase and followed by the rest of the property name
Designing an interface
Includes
Constant
public static final
variables
default
methods
static
methods
public abstract
methods
Purpose
Provide a way for one individual to develop code that uses another individual's code, without having access to the other individual's underlying implementation
Introducing Functional Programming
Define
interface that contains a single abstract method
Implementing Functional Interfaces with Lambdas
Lambda Syntax
a -> a.canHop()
a single parameter with the name
a
-> separate the parameter from the body
body calls a single method and returns the result of that method
(Animal a) -> { return a.canHop(); }
-> separate the parameter from the body
one ore more lines of code, including braces
{}
, a semicolon
;
, and
return
statement
a single parameter with the name
a
and the state that the type is
Animal
, wrapping the input parameters in parentheses
()
Differences
() can be
OMITTED
if there is
exactly one input parameter
and the
type is not explicitly stated
in the expression
{} can be
OMITTED
if there is
single-line
lambda body
Add
{}
=> must
explicitly terminate
each statement in the body with a
semicolon
+ use
return
statement if method in lambda return a value
Predicate Interface
Provide a common interface to use lambdas
Implementing Polymophism
Object vs Reference
Type of object = properties exist within the object in memory
Type of reference = methods and variables are accessible to the Java program
Casting Object References
Cast
superclass -> subclass
:
require explicit
cast
The compiler will NOT allow casts to
unrelated types
Cast
subclass -> superclass
:
NOT require
an
explicit
cast
Even
compile without issue
, an exception may be thrown at
runtime
if the object being cast is
NOT actually
an instance of that class
Working with Design Patterns
Singleton
Problem
Create an object in memory only once and have it shared by multiple classes
Solution
Create
private static
variable within the class
Accessed via a single
public static
method
All constructors are marked
private
Apply Lazy Instantiation
Lazy Instantiation
Def: Create a reusable object the first time it is requested
Pros: Reduce memory usage and improve performance when an application starts up
Cons: Noticeable delay the first time a particular type of resource is needed
Create Unique Singletons
Thread safety using
synchronized
Double-Checked Locking
Immutable Object
Problem
Create read-only objects that can be shared and used by multiple classes
Solution
Constructor
to set all properties
Make all of
instance
variables
private
and
final
NOT define any
setter
NOT allow
referenced mutable objects
to be
modified
or
accessed directly
Prevent
methods from being
overriden
Modifying an Immutable Object
Create
new immutable objects
that contain all of the same information as the original object + whatever we wanted to change
Builder Pattern
Problem
Create an object that requires numerous values to be set at the time the object instantiated
Solution
Builder class is mutable but target class is an immutable object
Setter methods return an instance of the builder object
this
The
builder class
and
target class
are considered
tight coupled
Factory Pattern
Problem
Create objects in which the precise type of the object may not be known until runtime
Solution
Produce instances of objects based on a set of input parameters