Design pattern Lecture 6.

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

Creational Design Patterns. Creational DP: Abstracts the instantiation process Helps make a system independent of how objects are created, composed, represented.
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Design Patterns Section 7.1 (JIA’s) Section (till page 259) (JIA’s) Section 7.2.2(JIA’s) Section (JIA’s)
SWE 4743 Strategy Patterns Richard Gesick. CSE Strategy Pattern the strategy pattern (also known as the policy pattern) is a software design.
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology.
Fall 2009ACS-3913 R McFadyen1 Singleton Problem: Exactly one instance of a certain object is required (this object is called a singleton). We must ensure.
7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
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.
Singleton and Basic UML CS340100, NTHU Yoshi. What is UML Unified Modeling Language A standardized general-purpose modeling language in the field of software.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Li Tak Sing COMPS311F. RMI callbacks In previous example, only the client can initiate a communication with the server. The server can only response to.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Design Patterns Introduction
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
Object-Oriented Programming © 2013 Goodrich, Tamassia, Goldwasser1Object-Oriented Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
JAVA INTRODUCTION. What is Java? 1. Java is a Pure Object – Oriented language 2. Java is developing by existing languages like C and C++. How Java Differs.
Advanced Object-oriented Design Patterns Creational Design Patterns.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Singleton Pattern Presented By:- Navaneet Kumar ise
The Singleton Pattern (Creational)
Design Pattern: Builder Timothy Holper. The Builder design pattern: What is it? The Builder pattern is a way to: ‘Separate the construction of a complex.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Design Patterns: Brief Examples
INF230 Basics in C# Programming
Working in the Forms Developer Environment
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Object-Oriented Analysis and Design
MPCS – Advanced java Programming
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
The Object-Oriented Thought Process Chapter 1
Chapter 11 Object-Oriented Design
Singleton Pattern Command Pattern
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Design Patterns (GoF) contains the creational patterns:
Chap 7. Building Java Graphical User Interfaces
Presented by Igor Ivković
Graphical User Interfaces -- Introduction
More Design Patterns 1.
VISUAL BASIC.
Component-Level Design
SE-2811 Software Component Design
Singleton design pattern
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
An Introduction to Software Architecture
CS 350 – Software Design Singleton – Chapter 21
Constructors, GUI’s(Using Swing) and ActionListner
Real World Scenario Sometimes it's appropriate to have exactly one instance of a class: window manager print spooler Filesystems press Ctrl-F to display.
Creational Patterns.
Presented by Igor Ivković
Software Design Lecture : 38.
Steps to Build Frame Window Recipe Application
Presentation transcript:

Design pattern Lecture 6

builder

Introduction The car is a more complex object. Its composition includes Chassis, Frame, Wheels, Engine, Gas Tank, Accelerator, Brake, Steering wheel. ,Battery, Audio, Sunroof, Cruise Control, GPS, etc. In order to decouple the process of building a complex object from its components, the software design uses a class to separately encapsulate the process of constructing a class. This class is called the Director class. Designing another class is responsible for producing different part class objects that meet certain conditions. This class is called the CarBuilder class; in addition, it needs a class package product called Car. The Builder pattern is to create a complex object step by step, which allows the user to construct them only by specifying the type and content of the complex object. The user does not know the internal concrete construction details. The concrete interaction for constructing a Car object can be represented by a sequence diagram, as shown in Figure 1.

Introduction Figure 1 Car assembly system sequence diagram

Builder—Concepts and Mechanisms Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representation. The class diagram design for the Builder pattern is shown in Figure 2. Figure 2 Class diagram of Builder pattern

Builder—Concepts and Mechanisms Figure 2 Class diagram of Builder pattern Director director = new Director(); director.setBuilder(cb); director.construct(); if ( cb == 1 ) Product1 = director.getObject(); else Product 2 = director.getObject();

Builder Applicability Structure Use the Builder pattern when The algorithm for creating a complex object should be independent of the parts that make up the object and how they are assembled. The construction process must allow different representations for the object that is constructed Structure Figure 3 Builder pattern structure

Builder—Examples A company needs to design a home purchase system. The houses in the system are divided into two types: Normal House and Luxury House. The difference between different types of housing is reflected in the size of the Area and the number of Bedrooms, Bathrooms, Garages, Gardens, and Swimming pools. According to the user‘s choice, the software uses three graphical interfaces for the user to select various indicators of the house. This program uses Builder pattern.

Builder—Examples The main interface of the program is shown in Figure 4. At the top left of the graphical interface, there is a “Choose House Type” pick list with Normal House and Luxury House options. Figure 4 House purchase system main graphical interface

Builder—Examples New sections in the Normal House interface include Area, Bedroom number, Garage type, and Garden type. Each selection contains two options. Figure 5 House Purchase System Normal House Graphical Interface

Builder—Examples The house properties section of the Luxury House interface contains Area, Bedroom number, Garage type, Garden type, and Swimming pool type. Each selection contains two options. Figure 6 House Purchase System Luxury House Graphical Interface

Builder—Examples The house purchase system includes a main user graphical interface. When the program is running, according to the user‘s selected input: Normal House or Luxury House, the two user graphical interfaces will be embedded in the main user graphical interface for the user to select the houses Item indicators. Finally, the user’s selection information will be displayed synchronously in the text box of the main graphical interface. The sequence diagram of the house purchase system is shown in Figure 7 Figure 7 The sequence diagram of the House Purchase System

Builder—Examples This program adopts Builder Pattern. The design diagram is shown in Figure 8. Figure 8 House Purchase System Designed Using Builder pattern

Builder—Examples Figure 8 House Purchase System Designed Using Builder pattern Director director = new Director(); director.setHouseBuilder( hb ); director.constructWholeHouseObj(); House hsObj = director.getHouse();

Singleton

Singleton Intent Motivation Solution Ensure a class only has one instance, and provide a global point of access to it. Motivation It is important for some classes to have exactly one instance. Although there can be many printers in a system, there should be only one printer spooler. There should be only one file system and one window manager. Solution Make the class itself responsible for keeping track of its sole instance. The class can ensure that no other instance can be created (by intercepting requests to create new objects), and it can provide a way to access the instance.

Singleton The basic idea Declare a constructor as a private type, and other types cannot use this constructor to create an object. Provide a getInstance method that can get an instance. This method is a static method that ensures that the client gets the same object at all times. Public class Singleton { private static Singleton instance; private String name; private Singleton (String name) { this.name = name; } public static Singleton getInstance (String name) { if (instance == null && ! name.equals(null)) instance = new Singleton (name); return instance;

Singleton Figure 9 Singleton pattern structure Instance is a static variable, of type Singleton, used to store instances that have already been created. GetInstanceData() creates a method for the instance. It creates the instance in a very special way. If an instance was previously created (and therefore stored in the variable instance), the method returns an instance; if the previous Singleton instance was not created, then The method newly creates and returns an instance of the Singleton class, thus ensuring the uniqueness of the instance

Singleton Applicability Use the Singleton pattern when There must be exactly one instance of a class, and it must be accessilbe to clients from a well-known access point. When the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.

Singleton—Examples Consider a company's internet connection. The company's external Internet uses a unified IP address, and all internal users use a unified internal server. When a user wants to connect to the Internet, the user should first connect to the internal server. Obviously, if a user has already started a connection, the user can no longer have a second connection. Figure 10 ClientGUI generated user interface Figure 11 The graphical interface generated by the SingleLogonGUI class Figure 12 ClientGUI interface – the situation when the user clicks the "Create Connection" button again

Singleton—Emamples Figure 13 Internet connection system class diagram designed using singleton pattern

Singleton—Examples Singleton Pattern must be used carefully in multithreaded applications. If two threads simultaneously call the create method when the only instance has not yet been created, it will cause each of the two threads to create an instance of the same, thereby violating the original intention of the uniqueness of the singleton pattern. In order to improve the above design, the getInstance() method in the design can be declared as synchronized. Figure 14 The president class diagram designed using the linear security singleton pattern President ins = President.getInstance();

Singleton—Examples Solution 1—Use the synchronized keyword public static Singleton getInstance() { if (singleton == null) { synchronized (Singleton.class) { singleton = new Singleton(); } return singleton; Solution 2 — Obtained by an internal static class public class Singleton { private static Singleton singleton; return SingletonHolder.mSingleton; private static class SingletonHolder { private static final Singleton mSingleton = new Singleton();