Presentation is loading. Please wait.

Presentation is loading. Please wait.

Www.javacup.ir Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.

Similar presentations


Presentation on theme: "Www.javacup.ir Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP."— Presentation transcript:

1 www.javacup.ir Sadegh Aliakbary

2 Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP is clearly noted as the source in the used case. JAVACUP shall not be liable for any errors in the content, or for any actions taken in reliance thereon. Please send your feedback to info@javacup.irinfo@javacup.ir 2JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

3 Agenda OOP Design Design Patterns 3JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

4 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 4JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

5 Famous Java Interfaces Comparable int compareTo(T o) java.io.Serializable No method Sometimes, interfaces have no method The interface itself, is the protocol Collection, Iterable, Iterator 5JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

6 6JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

7 7JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

8 Mechanisms of Code Reuse Generics Inheritance Composition interface 8  JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

9 Composition Inheritance: is-a Composition: has-a Circle has a center Other examples? 9JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

10 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 10JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

11 Example 11JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

12 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? 12JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

13 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 ) class DynamicDataSet { Sorting sorting; Do not depend on MergeSorting 13JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

14 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 14JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

15 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 15JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

16 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. 16JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

17 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 17JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

18 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 18JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

19 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){...}... 19JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

20 Singleton To make sure that only one instance is present for a particular class (one instance per JVM) Creational pattern 20JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

21 Only One Instance? public class Logger { private Logger() { } public static Logger myInstance; public static Logger getInstance() { if(myInstance == null) { myInstance = new Logger(); } return myInstance; } It is possible to create two instances of Logger. How? getInstance() method should be synchronized 21JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

22 Factory Design Pattern 22JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

23 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 23JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

24 Abstract Factory Pattern 24JAVACUP.ir

25 Data Access Object Pattern (DAO) 25JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

26 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 26JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

27 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 27JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

28 Decorator 28JAVACUP.ir

29 Prototype Creational pattern the type of objects to create is determined by a prototypical instance which is cloned to produce new objects 29JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

30 Prototype (2) 30JAVACUP.ir

31 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 31JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

32 JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source32


Download ppt "Www.javacup.ir Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP."

Similar presentations


Ads by Google