0% found this document useful (0 votes)
5 views5 pages

Summary CC31zks

The document provides an overview of object-oriented programming concepts in Java, including classes, inheritance, polymorphism, and design patterns such as Singleton, Strategy, and MVC. It emphasizes encapsulation, concurrency, and various design patterns to manage complexity and enhance functionality. Key concepts like the Observer pattern and the Active class design pattern are also discussed in the context of practical assignments.

Uploaded by

eceaslan5kg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Summary CC31zks

The document provides an overview of object-oriented programming concepts in Java, including classes, inheritance, polymorphism, and design patterns such as Singleton, Strategy, and MVC. It emphasizes encapsulation, concurrency, and various design patterns to manage complexity and enhance functionality. Key concepts like the Observer pattern and the Active class design pattern are also discussed in the context of practical assignments.

Uploaded by

eceaslan5kg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Object Orientation

Marie Simon s1023848


June 15, 2019

1 Basics
Classes Consist of attributes (instance variables) that are kept in memory.
In the constructor the attributes typically get initialized, just a special
method which is implicitly invoked via new to create an instance. The
methods may change the value of attributes.
Keywords • public can be seen by anyone
• protected by me and my children/subclasses
• private only by my class
• void returns nothing
• int or any other data type, function returns named object
• static shared by all instances of that class. Do not need an instance
to access it
Reference In Java everything is called by reference
Encapsulation Keep internal representation of an object hidden to the out-
side. Don’t use public attributes, use setters and getters

Inheritance A new class is created from existing class. Absorbs behaviour


and data from existing class and is enhanced with new capabilities. New
class is called derived class/subclass, original class is base class/superclass.
Demonstrates a ”is a” relationship.
Polymorphism The ability to associate many meanings to one method signa-
ture.
Overriding: A method is implemented in a new way in a child class
Overloading: Two methods have different parameters in the same class

1
Direct superclass One level up hierarchy
Indirect superclass Inherited two or more level up hierarchy
Single inheritance Inheritence from one superlcass
Multiple inheritance Inherits from multiple superclasses (Java does not sup-
port that)
Lambda-expressions • List of parameters
Can omit parantheses if only one
Can omit the types of parameters
• arrow token ->
• The body
A single expression (without statement braces {...} and does
not need the return statement)
Statement block (need braces {...} and can contain as many
statements as needed seperated by ; including the return statement
Concurrency • Apply for responsiveness, efficiency and (simulation of)
naturally concurrent processes
• Nondeterministic since same result can give different results (like
which thread terminates first)
• You ask a thread to perform a taks. A task is an object that provides
a run method
• Introduce an active class implementing Runnable
• Implement method run. Has no parameters, no return value, cannot
throw checked exceptions.
• Create and instance of class Thread with active class as argument.
Call start on Thread and run will be executed
Synchronization – synchronized on a method, the current ob-
ject is used for locking
– synchronized on a block the lock object is specified explic-
itly

2
Executor Manages a thread pool to execute Runnables
Conditions A factory method on lock for newCondition. These
conditions can call await, signalAll

2 Singleton Pattern
Lecture 8 - JavaFX
Context We want only one instance of a class at a time
Solution • Make the constructor private
• Add a static attribute to hold the object and a static method that
returns this object
Eager singleton The object is made when the class is loaded
Lazy Singleton Object is made when program gets first object from class

3 Strategy pattern
Lecture 3 - Different players with player interface
Context • A class can use different variants for an algorithm
• Clients replace standard algorithms with custom versions
• Dynamic change of algorithm
Solution • Define an interface that is an abstraction of the algorithm
• Actual strategy classes implement this interface type
• Whenever the algorithm needs to be executed the context class calls
the appropriate method of the strategy object

4 Decorator pattern
Context • Add or remove functionalities of a component class at run time
• Too many variations for a separate subclass
Solution • Define an interface type that is an abstraction of the component
• Actual component classes implement this interface
• Decorator object manages the component object

5 Optional pattern
Context null is the billion dollar mistake
Solution Replace null by an Optional type; the Optional object is always
there

3
6 Visitor pattern
Assignment 11 - Logical formulas
Context • Apply distinct unrelated operations to collection of objects
• We do not want to specify operations in every class
• Keep operations and operation structure apart
• We need an alternative way for a new method for each operation
Solution • Define a visitor that can perform the operation to objects (one
visitor class for each operation)
• Classes implement method to apply the visitor to themselves
They accept the visitor
Dynamic binding is necessary to select correct method of visitor,
double-call back is necessary to achieve dynamic binding

7 MVC pattern
Assignment 10 - Snake game
Context We have a model, a view and a controller
• Model is the state of program, has no idea of GUI/views
• View is the JavaFX displaying a view on the model
• Controller is the handler that changes the model
Solution Uses the observer pattern
• Model notifies views when something happens
• Button notifies action listeners when they are pressed
• Views attach themselves to model in order to be notified
• Action listeners attach themselves to button in order to be notified

8 Observer pattern
Assignment 9 - Pie Chart
Context • An object, the subject is the source of events
• one or more observer objects want to be notified when such an event
occurs
Solution • Define an observer interface type, all concrete observers imple-
ment it
• The subject maintains a collection of observers
• The subject supplies methods for attaching and detaching observers
• Whenever the state of the subject changes, the subject notifies all
observers

4
9 Active class design pattern
Assignment 14 - Taxi
Assignment 15 - Supermarket
Context Used for concurrency
Solution Design the method run and choose the classes that create the threads

You might also like