Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Programming Behnam Hatami Fall 2017.

Similar presentations


Presentation on theme: "Advanced Programming Behnam Hatami Fall 2017."— Presentation transcript:

1 Advanced Programming Behnam Hatami Fall 2017

2 Agenda OOP Design Design Patterns

3 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

4 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

5 Mechanisms of Code Reuse
Generics Inheritance Composition interface

6 Composition Inheritance: is-a Composition: has-a Circle has a center
Other examples?

7 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

8 Example

9 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?

10 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

11 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

12 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

13 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.

14 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

15 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

16 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){...} ...

17 Singleton To make sure that
only one instance is present for a particular class (one instance per JVM) Creational pattern

18 Only One Instance? It is possible to create two instances of Logger. How? getInstance() method should be synchronized

19 Factory Design Pattern

20 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

21 Abstract Factory Pattern

22 Data Access Object Pattern (DAO)

23 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

24 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

25 Decorator

26 Prototype Creational pattern
the type of objects to create is determined by a prototypical instance which is cloned to produce new objects

27 Prototype (2)

28 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

29 References Refactoring: improving the design of existing code, Martin Fowler, Kent Beck, John Brant, William Opdyke, Don Roberts (1999) Java cup

30 Any Question


Download ppt "Advanced Programming Behnam Hatami Fall 2017."

Similar presentations


Ads by Google