Advanced Programming Behnam Hatami Fall 2017
Agenda OOP Design Design Patterns
interfaces Defines a protocol an interface promises a certain functionality All the classes implementing the interface provide their own implementations for the promised functionality A class combines the state and the behavior of a real object An interface specifies (only) the behavior of an abstract entity
Famous Java Interfaces Comparable<T> int compareTo(T o) java.io.Serializable No method Sometimes, interfaces have no method The interface itself, is the protocol Collection, Itertable, Iterator
Mechanisms of Code Reuse Generics Inheritance Composition interface
Composition Inheritance: is-a Composition: has-a Circle has a center Other examples?
Composition vs. Inheritance You can make a base class And put the common functionality of many classes in it But always check: Whether the is-a relationship exists between the derived classes and the base class If the is-a relationship does not hold, Use composition instead of inheritance Favor Composition over Inheritance
Example
Problems with this Inheritance is-a DynamicDataSet is-a Sorting? No. What if the two types of data set classes have a genuine base class, DataSet?
Notes Favor composition over inheritance Use composition to get code that is easy to change and loosely coupled Make your classes dependent on interfaces, not on the actual implementation findBest(List<T>) class DynamicDataSet { Sorting sorting; Do not depend on MergeSorting
Terminology Association types: composition and aggregation Composition: the lifetime of the contained object and the container object is the same That is not the case with aggregation Body and heart? composition Library and book? aggregation
Composition over inheritance Classes achieve polymorphic behavior and code reuse By containing other classes that implement the desired functionality instead of through inheritance How? typically by creating various interfaces The interfaces represent the desired behaviors We can simulate multiple-inheritance using this technique
Design Patterns A general reusable solution to a commonly occurring problem within a given context in software design Repeatable solution to solve a generic design problem Professionals has formulated solutions to frequently recurring design problems Reusability in the level of software design design patterns are design solutions They are not ready-made solutions like code in a library Design Patterns: Elements of Reusable Object-Oriented Software (Gang of Four), Erich Gamma, et al.
Types of Design Patterns Creational patterns About object instantiation examples: singleton, Factory, Abstract Factory, and prototype Structural patterns how related classes and objects are composed together to form a larger structure. examples: Decorator, proxy, and Façade. Behavioral patterns objects communication and flow control e.g. Observer, Iterator, state
Observer Pattern Whenever any change in observable object takes place, you need to inform some classes (observers) as to the changed information interface java.util.Observer public class java.util.Observable Behavioral pattern You may write your own observable classes Perhaps to obey your class hierarchy
Observer public interface Observer { void update(Observable o, Object arg); } public class Observable { private boolean changed = false; private Vector obs; public synchronized void addObserver(Observer o){...} ...
Singleton To make sure that only one instance is present for a particular class (one instance per JVM) Creational pattern
Only One Instance? It is possible to create two instances of Logger. How? getInstance() method should be synchronized
Factory Design Pattern
Factory and Abstract Factory Pattern Creational Pattern Factory classes are usually singletone Abstract factory pattern: a factory of factories introduces one more indirection to create a specified object A client of the abstract factory design pattern first requests a proper factory from the abstract factory object and then it requests an appropriate object from the factory object
Abstract Factory Pattern
Data Access Object Pattern (DAO)
DAO Abstracts the details of the underlying persistence mechanism Hides the implementation details of the data source from its clients Loose coupling between core business logic and persistence mechanism Generic DAO pattern
Decorator Structural patterns also known as Wrapper Allows behavior to be added to an individual object, (either statically or dynamically) without affecting the behavior of other objects from the same class
Decorator
Prototype Creational pattern the type of objects to create is determined by a prototypical instance which is cloned to produce new objects
Prototype (2)
Façade (or Façade) A facade is an object that provides a simplified interface to a larger body of code, such as a class library. A Structural pattern A facade can: make a software library easier to use, understand and test make the library more readable reduce dependencies of outside code on the inner workings wrap a poorly designed collection of APIs with a single well-designed API
References Refactoring: improving the design of existing code, Martin Fowler, Kent Beck, John Brant, William Opdyke, Don Roberts (1999) Java cup
Any Question