General points of attention

Features of OOP

Modular programs

List of advantages:

Encapsualtion

What is it?

Why is it useful?

ADT

What is it?

Why is it useful?

How to implement it?

Code:

Documentation:

Representation invariant (short: rep invariant) is condition to be satisfied by the instance variables, in order to make sense as a representation of an abstract value

Abstraction function maps each representation that satisfies the rep invariant to the represented abstract value. Can be implemented (in a way) in method String toString().


TDD

Steps

  1. Analyze requirements
  2. Slecet requirement to develop
  3. Specify class/methods informally: javadoc summary
  4. Specify formally: model with invariants, headers and contracts
  5. Implement rigorous tests, in unit test class
  6. Choose data rep and implement class methods
  7. Test implementation and fix defects
  8. Repeat

Why write test cases before implementing software?

Robustness

A method is called robust when its behaviour under violated precondition is well-specified in its contracts

How to test for robustness?

  1. Call the method under test in a try block so that precondition is violated
  2. If the next statement is reached (no exception thrown), the test fails
  3. Catch exception thrown in a catch block
  4. If expected exception was thrown, pass the test, fail otherwise

Exceptions

Why throw exceptions instead of returning a special value?

5 1/2 Major principles

SRP: Single Responsibility Principle

OCP: Open Closed Principle

LSP: Liskov Substitution Principle

ISP: Interface Segregation Principle

DIP: Dependency Inversion Principle

DRY: Don't Repeat Yourself

Mutable vs Immutable classes

Advantages:

Disadvantages:

Immutable vs mutable

Nested classes

These come in 4 kinds:

  1. static member class (top-level class)
  2. non-static member class
  3. names local classes
  4. anonymous class

The latter 3 are called inner classes. An inner class has access to the member of its outer classes

Inner classes

Advantages:

Disadvantages:

nested classes

Generic classes

SwingWorker

Purpose

The main steps

  1. Static aspect, declaration. By subclassing SwingWorker and overriding the doInBackground() and done() methods
  2. Dynamic aspect, execution. The resulting SwingWorker subclass must be instantiated for each run. The background thread is activated by calling execute().

Template Method:

Facade:

Miscellaneous

Design Patterns

Benefits:

Iterator

Intent

Solution

Iterator<E>

Iterable<E>

iterator uml

  1. The Iterator interface declares the operations required for traversing a collection: fetching the next element, retrieving the current position, restarting iteration, etc.
  2. Concrete Iterators implement specific algorithms for traversing a collection. The iterator object should track the traversal progress on its own. This allows several iterators to traverse the same collection independently of each other.
  3. The Collection interface declares one or multiple methods for getting iterators compatible with the collection. Note that the return type of the methods must be declared as the iterator interface so that the concrete collections can return various kinds of iterators.
  4. Concrete Collections return new instances of a particular concrete iterator class each time the client requests one.
  5. The Client works with both collections and iterators via their interfaces. This way the client isn’t coupled to concrete classes, allowing you to use various collections and iterators with the same client code. Typically, clients don’t create iterators on their own, but instead get them from collections. Yet, in certain cases, the client can create one directly; for example, when the client defines its own special iterator.

Discussion

Pros

Cons

Strategy

Introduction

Intent

Solution

strategy uml

  1. The Context maintains a reference to one of the concrete strategies and communicates with this object only via the strategy interface.
  2. The Strategy interface is common to all concrete strategies. It declares a method the context uses to execute a strategy.
  3. Concrete Strategies implement different variations of an algorithm the context uses.
  4. The context calls the execution method on the linked strategy object each time it needs to run the algorithm. The context doesn’t know what type of strategy it works with or how the algorithm is executed.
  5. The Client creates a specific strategy object and passes it to the context. The context exposes a setter which lets clients replace the strategy associated with the context at runtime.

Discussion

Anti-pattern

anti strategy uml

In the anti-pattern of this DP, client/context declares and uses a variable of each concrete algorithm. This way the client/context depends on (cannot be compiled without) the concrete strategy classes.

Pros

Cons

Observer

Intent

Observer is a behavioral design pattern that lets you define a subscription mechanism to notify multiple objects about any events that happen to the object they’re observing.

It defines a one-to-many relation between:

Solution

Simply, the mechanism consists of:

  1. an array field for storing a list of references to subscriber objects
  2. several public methods which allow adding subscribers to and removing them from that list.

observer uml

  1. The Publisher issues events of interest to other objects. These events occur when the publisher changes its state or executes some behaviors. Publishers contain a subscription infrastructure that lets new subscribers join and current subscribers leave the list.
  2. When a new event happens, the publisher goes over the subscription list and calls the notification method declared in the subscriber interface on each subscriber object.
  3. The Subscriber interface declares the notification interface. In most cases, it consists of a single update method. The method may have several parameters that let the publisher pass some event details along with the update.
  4. Concrete Subscribers perform some actions in response to notifications issued by the publisher. All of these classes must implement the same interface so the publisher isn’t coupled to concrete classes.
  5. Usually, subscribers need some contextual information to handle the update correctly. For this reason, publishers often pass some context data as arguments of the notification method. The publisher can pass itself as an argument, letting subscriber fetch any required data directly.
  6. The Client creates publisher and subscriber objects separately and then registers subscribers for publisher updates.

Discussion

The update method in the Observer interface could inclde a reference to the Subject as seen below:

interface Observer { void update(Subject subject, Data data); }

This is useful because an observer may be observing multiple subjects. The subject parameter identifies the object which has been updated, and can be used to pull more data from it.

Pros

Cons

Command

Introduction

Command is a behavioral design pattern that turns a request into a stand-alone object that contains all information about the request. This transformation lets you pass requests as a method arguments, delay or queue a request’s execution, and support undoable operations.

Intent

GUI Objects delegate the work to commands

Structure

command uml

Summary:

  1. Invoker: initiate requests encapsulated in commands
  2. Command: an abstract interface allows concrete commands to be loosely coupled to clients
  3. ConcreteCommand: calls operations on receiver objects in order to carry out the encapsulated request
  4. Receiver: represents existing functionality
  1. The Sender class (aka invoker) is responsible for initiating requests. This class must have a field for storing a reference to a command object. The sender triggers that command instead of sending the request directly to the receiver. Note that the sender isn’t responsible for creating the command object. Usually, it gets a pre-created command from the client via the constructor.
  2. The Command interface usually declares just a single method for executing the command.
  3. Concrete Commands implement various kinds of requests. A concrete command isn’t supposed to perform the work on its own, but rather to pass the call to one of the business logic objects. However, for the sake of simplifying the code, these classes can be merged. Parameters required to execute a method on a receiving object can be declared as fields in the concrete command. You can make command objects immutable by only allowing the initialization of these fields via the constructor.
  4. The Receiver class contains some business logic. Almost any object may act as a receiver. Most commands only handle the details of how a request is passed to the receiver, while the receiver itself does the actual work.
  5. The Client creates and configures concrete command objects. The client must pass all of the request parameters, including a receiver instance, into the command’s constructor. After that, the resulting command may be associated with one or multiple sender

Discussion

Applicability

Pros

Cons

Singleton

Singleton is a creational design pattern that lets you ensure that a class has only one instance, while providing a global access point to this instance.

Intent

Singleton solves two problems, vilating the Single Responsibility Principle:

  1. Ensures that a class has just a single instance
  2. Provide a global access point to that instance

Solution

Structure

singleton uml

  1. The Singleton class declares the static method getInstance that returns the same instance of its own class. The Singleton’s constructor should be hidden from the client code. Calling the getInstance method should be the only way of getting the Singleton object.

When it comes to multithreaded programs you should use double-checked locking sync.

singleton multithreading

Discussion

singleton problems

Applicability

Pros

Cons

Decorator

Decorator is a structural design pattern that lets you attach new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors.

It is about modifying behaviour while preserving interface

Problem

Extend the behaviour of an existence class with inheritance:

decorator combinations

Intent

Solution

Structure

decorator uml

  1. The Component declares the common interface for both wrappers and wrapped objects.
  2. Concrete Component is a class of objects being wrapped. It defines the basic behavior, which can be altered by decorators.
  3. The Base Decorator class has a field for referencing a wrapped object. The field’s type should be declared as the component interface so it can contain both concrete components and decorators. The base decorator delegates all operations to the wrapped object.
  4. Concrete Decorators define extra behaviors that can be added to components dynamically. Concrete decorators override methods of the base decorator and execute their behavior either before or after calling the parent method.
  5. The Client can wrap components in multiple layers of decorators, as long as it works with all objects via the component interface.

Discussion

Applicability

Pros

Cons