design patterns hamza kindi software engineer [email protected] [email protected]...

44
www.streebo.com Design Patterns Hamza Kindi Software Engineer [email protected] [email protected] 0343-3024273

Upload: tomai

Post on 25-Feb-2016

45 views

Category:

Documents


2 download

DESCRIPTION

Design Patterns Hamza Kindi Software Engineer [email protected] [email protected] 0343-3024273. Outline. Some information regarding Streebo Software Labs. Design Patterns. Streebo – An Overview. Established in 2008 by seasoned and trusted former IBM employees - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Design PatternsHamza Kindi

Software Engineer [email protected]

[email protected]

Page 2: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Outline

• Some information regarding Streebo Software Labs.

• Design Patterns.

Page 3: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Streebo – An Overview

• Established in 2008 by seasoned and trusted former IBM employees

• Expertise in Portal Planning, Development and Governance

• Rapid growth - experiencing 200% revenue increase year to year

• 100% headcount growth in 2010 • A growing, reference ready client

base• IBM Advance Business Partner• IBM Authorized Independent Training

Provider (AITP)

Page 4: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Global footprint

Headquartered in Houston, TX with Global Delivery offices in Manchester – NH, Chengdu – China, Karachi –

Pakistan and Ahmedabad, India.

Page 5: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Areas of Expertise

– Over 50 portal implementations for world’s leading organizations– Packaged and rapid development methodology– Well-trained and experienced workforce provides: Planning,

Creative, Implementation, Integration, Governance and Education

– Niche focus provides deep skills in Lotus Portal and collaboration products

Portals and Collaboration

“Upon starting a very difficult WebSphere Portal project Streebo worked with us the whole way until the solution was in production. This was truly a first in the industry solution. Transmontaigne was

grateful to be working with a strong partner like Streebo."- Bill Paris, Program Manager, Transmontaigne Group

Page 6: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Areas of Expertise

– Experience in complex business scenarios– A list of Fortune 1000 satisfied customers

Information Management

Portal Training– Authorized IBM Training Provider

since 2009– Trained over 50 professionals on

Portal Development methodologies– Consistently ranked 5 star in

reviews

“I would suggest Streebo to anyone

who needs anything from basic to

advanced Portlet Factory training.” –

Corey Whitney, Honda

Page 7: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Product Expertise

– Experience in complex business scenarios– A list of Fortune 1000 satisfied customers

Category Product / Development Area

Level of Expertise

WebSphere Portal WebSphere Portlet Factory Expert

WebSphere Portal Web Content Management Good Experience

WebSphere Portal WebSphere Portal: Themes and Skins Good Experience

WebSphere Portal WebSphere Portal: Administration Good Experience

WebSphere Portal IBM Dashboard Accelerator Expert

WebSphere Portal IBM Healthcare Accelerator Expert

Mashups Lotus Mashups Center Expert

Mashups Widget Factory Expert

Page 8: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Management Team

Usman Memon is the co-founder and Managing Partner of Streebo Inc.

Usman advises his customers on IT Strategy and Enterprise Architecture. He is an oft-requested technical speaker at seminars and conferences, co-author of a book (Programming Portlets ― IBM Press) and has contributed to several articles and white papers related to Portal, Portlets and Service-Oriented Architecture.

Usman holds a Masters in Mathematics from Ohio University and a Bachelors in Engineering from NIT (REC) Trichy, India.

Mohammad Ovais is the co-founder and Managing Partner of Streebo, Inc.

He is a leading expert in designing Portal and Business Intelligence systems for large enterprises. He has consulted for over a decade with fortune 500 customers such as Procter & Gamble (P&G), British Petroleum (BP), Johnson & Johnson (J&J), Cisco Systems and TD Bank.

Mohammad holds a Bachelor's in Computer Science from Rice University.

Usman MemonManaging Partner

Mohammad OvaisManaging Partner

Page 9: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Management Team

Salman is the Director of Software and Consulting at Streebo Software Labs.

Salman has over ten years of experience in delivering practical and customer-friendly solutions to clients across the globe. Before coming to Streebo, Salman was VP Software Development for Avanza Solutions, Pakistan’s premier software company.

Salman has computer science degrees from SZABIST and the National University of Computer and Emerging Sciences.

Salman KasbatiDirector Software & Consulting

Page 10: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Highlights

100%

reference ready customers

50+

professionals trained in 2 years

55+

Portals developed and implemented

80%

Employees with relevant certifications

95%

Customer satisfaction rating for trainings

200 % of revenue growth year on year

Page 11: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Industries Served

Media

PharmaConsulting

Automobiles

RetailTelecom

Finance DistributionInsurance

Materials EnergyLife Sciences

Streebo has served industry leaders in the following sectors

Page 12: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Customers

Page 13: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Let’s look into Design Patterns …

Shall we?

Page 14: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Design Patterns14

• Design patterns are recurring solutions to software design problems you find again and again in real-world application development.

Page 15: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Benefits 15

• Promote design reuse in future systems• Help to identify common pitfalls• Help to design a system independent of

the target programming language• Shorten the design phase in software

development process

Page 16: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Criticism16

• The design patterns may just be a sign of some missing features of a given programming language (Java or C++ for instance). Peter Norvig demonstrates that 16 out of the 23 patterns in the Design Patterns book (which is primarily focused on C++) are simplified or eliminated (via direct language support) in Lisp or Dylan.

•  Inappropriate use of patterns may unnecessarily increase complexity.

Page 17: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Types of Design Patterns17

• Creational Pattern• Structural Pattern• Behavioral Pattern

Page 18: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Creational Pattern18

• Creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation

• Some examples of creational design patterns include:• Abstract factory pattern: centralize decision of what factory to

instantiate• Factory method pattern: centralize creation of an object of a

specific type choosing one of several implementations• Singleton pattern: restrict instantiation of a class to one object

Page 19: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Factory Pattern19

• Define an interface for creating an object, but let the subclasses decide which class to instantiate.

Page 20: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Uses of Factory20

• You need to localize knowledge of which class get created.

• A class can’t anticipate which class get created.

• Class uses sub classes to specify which objects get created.

Page 21: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Factory Code Example21

abstract class Shape{protected int r;

public abstract void getArea();

}

public class Circle extends Shape {

public Circle(int d){r=d;System.out.println("Circle of radius: " + r);}public void getArea(){System.out.println("Area: "+Math.PI *r*r);}}

public class Square extends Shape {public void getArea(){System.out.println("Area: "+ r*r);}public Square(int d){r=d;System.out.println("Square with length of sides: "+r);}}

Page 22: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Factory Contd..22

public class ShapeFactory {

public Shape getShape(String name, int r) { Shape myShape; if (name.equals("circle")) myShape=new Circle(r); else myShape=new Square(r); return myShape;}}

public class FactoryDemo {

public static void main(String[] args) {

ShapeFactory fac=new ShapeFactory();//Creating circleShape circle =fac.getShape("circle", 10);//Creating squareShape square = fac.getShape("square", 10);circle.getArea();square.getArea();

}

}

Page 23: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Singleton23

• restricts the instantiation of a class to one object.

Page 24: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Uses of Singleton24

• Singleton is used where we need to ensure that there is a single instance of a class eg, window manager.

Page 25: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Singleton Code Example25

public class Singleton { private static Singleton _instance; private Singleton() { System.out.println("this is singleton object constructor"); } public void helloWorld(){ System.out.println("Hello World this is Singleton"); }

// synchronized keyword ensure that this method is accessed // one thread at a time public static synchronized Singleton getInstance() { if (null == _instance) { _instance = new Singleton(); } return _instance; }}

Page 26: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Singleton Contd ..

public class SingletonDemo {public static void main(String[ ] args) {//Calling the singleton object getInsance MethodSingleton first = Singleton.getInstance();first.helloWorld();//Note that in the second call to getInstance method //Singleton object constructor is not called.//The second object is referring to the object //created in the first call.Singleton second = Singleton.getInstance();second.helloWorld();}

}

Page 27: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Structural Pattern27

• Structural design patterns are design patterns that ease the design by identifying a simple way to realize relationships between entities.

• Examples of Structural Patterns include:• Adapter pattern: 'adapts' one interface for a class into one that

a client expects• Facade pattern: create a simplified interface of an existing

interface to ease usage for common tasks• Proxy pattern: a class functioning as an interface to another

thing

Page 28: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Adapter28

• Adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern that translates one interface for a class into a compatible interface.

Page 29: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Uses of Adapter29

• An adapter allows classes to work together that normally could not because of incompatible interfaces. 

• The adapter translates calls to its interface into calls to the original interface.

• The adapter is also responsible for transforming data into appropriate forms. 

Page 30: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Adapter Code Example30

public class CylindricalSocket { public String supply(String cylinStem1, String cylinStem1) { System.out.println("Power power power..."); }} public class RectangularAdapter extendsCylindricalSocket { public String adapt(String rectaStem1, Sting rectaStem2) { //some conversion logic String cylinStem1 = rectaStem1; String cylinStem2 = rectaStem2; return supply(cylinStem1, cylinStem2); }}

public class RectangularPlug { private String rectaStem1; private String rectaStem2; public getPower() { RectangulrAdapter adapter = newRectangulrAdapter(); String power = adapter.adapt(rectaStem1, rectaStem2); System.out.println(power); }}

Page 31: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Proxy Pattern31

• A proxy, in its most general form, is a class functioning as an interface to something else. The proxy could interface to anything: a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate.

Page 32: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Uses of Proxy32

• In situations where multiple copies of a complex object must exist, the proxy pattern can be adapted order to reduce the application's memory footprint.

• Typically, one instance of the complex object and multiple proxy objects are created, all of which contain a reference to the single original complex object. Any operations performed on the proxies are forwarded to the original object. Once all instances of the proxy are out of scope, the complex object's memory may be deallocated.

Page 33: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Proxy Code Example33

interface Image { void displayImage();} // on System A class RealImage implements Image { private String filename; public RealImage(String filename) { this.filename = filename; loadImageFromDisk(); } private void loadImageFromDisk() { System.out.println("Loading " + filename); } public void displayImage() { System.out.println("Displaying " + filename); } }

/on System B class ProxyImage implements Image { private String filename; private RealImage image; public ProxyImage(String filename) { this.filename = filename; } public void displayImage() { if (image == null) { image = new RealImage(filename); } image.displayImage(); }}class ProxyExample { public static void main(String[] args) {

Image image1 = new ProxyImage("HiRes_10MB_Photo1"); Image image2 = new ProxyImage("HiRes_10MB_Photo2"); image1.displayImage(); // loading necessary image1.displayImage(); // loading unnecessary image2.displayImage(); // loading necessary image2.displayImage(); // loading unnecessary image1.displayImage(); // loading unnecessary } }

Page 34: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Behavioral Pattern34

• Behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.

• Examples of this type of design pattern include:• Chain of responsibility pattern: Command objects are handled or

passed on to other objects by logic-containing processing objects• Interpreter pattern: Implement a specialized computer language to

rapidly solve a specific set of problems• Iterator pattern: Iterators are used to access the elements of an

aggregate object sequentially without exposing its underlying representation

• Strategy pattern: Algorithms can be selected on the fly

Page 35: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Strategy Pattern

• Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

• A class defines many behaviors, and these appear as multiple conditional statements in its operations. Instead of many conditions, move related conditional branches into their own Strategy class.

• Define strategies for a performing a behavior and change them dynamically as per requirement.

Page 36: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

36

Page 37: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Who is What? 37

• Strategy - defines an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.

• ConcreteStrategy - each concrete strategy implements an algorithm.

• Context Contains a reference to a strategy object. Defines an interface that lets strategy accessing its data. The Context objects contains a reference to the

ConcreteStrategy that should be used. When an operation is required then the algorithm is run from the strategy object.

Context object receives requests from the client and delegates them to the strategy object. Usually the ConcreteStartegy is created by the client and passed to the context. From this point the clients interacts only with the context.

Page 38: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Example38

• Robots Application

Page 39: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Example Contd..39public interface IBehaviour {

public int moveCommand();}

public class AgressiveBehaviour implements IBehaviour{public int moveCommand(){

System.out.println("\tAgressive Behaviour: if find another robot attack it");

return 1;}

}

public class NormalBehaviour implements IBehaviour{public int moveCommand()

{System.out.println("\tNormal

Behaviour: if find another robot ignore it");return 0;

}}

public class DefensiveBehaviour implements IBehaviour{public int moveCommand(){

System.out.println("\tDefensive Behaviour: if find another robot run from it");

return -1;}

}

Page 40: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Example Contd..40public class Robot

{IBehaviour behaviour;String name;

public Robot(String name){

this.name = name;}

public void setBehaviour(IBehaviour behaviour){

this.behaviour = behaviour;}

public IBehaviour getBehaviour(){

return behaviour;}

public void move(){int command = behaviour.moveCommand();}

public String getName() { return name;}

public void setName(String name){ this.name = name;}}

Page 41: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Example Contd..41

public class Main {

public static void main(String[ ] args) {

Robot r1 = new Robot(“Robot1");Robot r2 = new Robot(“Robot2");Robot r3 = new Robot(“Robot3");

//Set behavior of ROBOTS

r1.setBehaviour(new AgressiveBehaviour());r2.setBehaviour(new DefensiveBehaviour());r3.setBehaviour(new NormalBehaviour());

r1.move();r2.move();r3.move();

//Change behavior of ROBOTS

//Robot1 will behave as Defensive Robot now.r1.setBehaviour(new DefensiveBehaviour());

//Robot2 will have aggressive behavior now.r2.setBehaviour(new AgressiveBehaviour());

r1.move();r2.move();r3.move();

}}

Page 42: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

At Streebo, we nurture talent!!!

Page 43: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Q&A

Page 44: Design Patterns Hamza Kindi  Software Engineer  Hamza.kindi@streebo.com humzahalkindi@hotmail.com 0343-3024273

www.streebo.com

Resources 44

• http://javapapers.com/category/design-patterns/

• The Design Patterns Java CompanionBy James W. Cooper

• Wikipedia Article “Software Design Patterns”