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

Slides:



Advertisements
Similar presentations
Welcome to. Who am I? A better way to code Design Patterns ???  What are design patterns?  How many are there?  How do I use them?  When do I use.
Advertisements

Design Patterns Section 7.1 (JIA’s) Section (till page 259) (JIA’s) Section 7.2.2(JIA’s) Section (JIA’s)
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
C15: Design Patterns Gamma,Helm,Johnson,Vlissides (GOF)
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Design Patterns CS is not simply about programming
Design Patterns. What are design patterns? A general reusable solution to a commonly occurring problem. A description or template for how to solve a problem.
Spring 2010CS 2251 Design Patterns. Spring 2010CS 2252 What is a Design Pattern? "a general reusable solution to a commonly occurring problem in software.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
Design Patterns Ric Holt & Sarah Nadi U Waterloo, March 2010.
Proxy Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Implementing Design Patterns Using Java St. Louis Java Special Interest Group Eric M. Burke Object Computing, Inc. Presented on July 9, 1998 (updated July.
CSSE 374: Introduction to Gang of Four Design Patterns
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Advanced Programming Rabie A. Ramadan 7.
Objects First With Java A Practical Introduction Using BlueJ Designing applications 1.0.
Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Chapter 8 Object Design Reuse and Patterns. Object Design Object design is the process of adding details to the requirements analysis and making implementation.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
Designing applications Main concepts to be covered Discovering classes CRC cards Designing interfaces Patterns Objects First with Java - A Practical.
July 28, 2015IAT 2651 Design Patterns. “Gang of Four” July 28, 2015IAT 2652.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
Design Patterns. 1 Paradigm4 Concepts 9 Principles23 Patterns.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
More Design Patterns From: Shalloway & Trott, Design Patterns Explained, 2 nd ed.
Design Patterns Introduction
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Object- oriented Design Principles
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Designing applications Main concepts to be covered Discovering classes CRC cards Designing interfaces Patterns © 2017 Pearson Education, Inc. Hoboken,
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Generator Design Patterns: Singleton and Prototype
Design Patterns: MORE Examples
The Object-Oriented Thought Process Chapter 15
Chapter 10 Design Patterns.
MPCS – Advanced java Programming
Introduction to Design Patterns
Design Patterns Based on slides provided by Abbie Jarrett
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
object oriented Principles of software design
Advanced Programming Behnam Hatami Fall 2017.
DESIGN PATTERNS : Introduction
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
Chapter 8, Design Patterns Introduction
Software Design Lecture : 39.
Presentation transcript:

Sadegh Aliakbary

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Abstract Factory Pattern 24JAVACUP.ir

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

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

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

Decorator 28JAVACUP.ir

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

Prototype (2) 30JAVACUP.ir

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

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