MSO Object Creation Singleton & Object Pool

Similar documents
MSO Lecture 12. Wouter Swierstra (adapted by HP) October 26, 2017

CS342: Software Design. November 21, 2017

The Design Patterns Matrix From Analysis to Implementation

CPSC 310 Software Engineering. Lecture 11. Design Patterns

MSO Lecture 6. Wouter Swierstra. September 24, 2015

Goals of Lecture. Lecture 27: OO Design Patterns. Pattern Resources. Design Patterns. Cover OO Design Patterns. Pattern Languages of Programming

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Agile Architecture. The Why, the What and the How

A few important patterns and their connections

Plan. A few important patterns and their connections. Singleton. Singleton: class diagram. Singleton Factory method Facade

MSO Lecture 6. Wouter Swierstra (adapted by HP) September 28, 2017

Lecture 13: Design Patterns

From Design Patterns: Elements of Reusable Object Oriented Software. Read the sections corresponding to patterns covered in the following slides.

Pattern Resources. Lecture 25: Design Patterns. What are Patterns? Design Patterns. Pattern Languages of Programming. The Portland Pattern Repository

What is it? CMSC 433 Programming Language Technologies and Paradigms Spring Approach 1. Disadvantage of Approach 1

CS 351 Design of Large Programs Singleton Pattern

EINDHOVEN UNIVERSITY OF TECHNOLOGY

CHAPTER 6: CREATIONAL DESIGN PATTERNS

Applying Some Gang of Four Design Patterns CSSE 574: Session 5, Part 3

Applying Design Patterns to SCA Implementations

05. SINGLETON PATTERN. One of a Kind Objects

Thread Safety. Review. Today o Confinement o Threadsafe datatypes Required reading. Concurrency Wrapper Collections

Design Patterns: Structural and Behavioural

Software Design COSC 4353/6353 D R. R A J S I N G H

Index Shalloway rev.qrk 9/21/04 5:54 PM Page 419. Index

Concurrency: State Models & Design Patterns

Object-Oriented Design. NCCU Fall Lecture 4-2: Design Patterns-I Oct

1 Software Architecture

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

CS 349 / SE 382 Design Patterns. Professor Michael Terry January 21, 2009

Software Design Fundamentals. CSCE Lecture 11-09/27/2016

MSO Lecture Design by Contract"

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

The Strategy Pattern Design Principle: Design Principle: Design Principle:

An Introduction to Patterns

Application Architectures, Design Patterns

MSO Exam November 7, 2016, 13:30 15:30, Educ-Gamma

Design Patterns. Dr. Rania Khairy. Software Engineering and Development Tool

What is Design Patterns?

Review Software Engineering October, 7, Adrian Iftene

be used for more than one use case (for instance, for use cases Create User and Delete User, one can have one UserController, instead of two separate

CS 370 Design Heuristics D R. M I C H A E L J. R E A L E F A L L

Object Oriented Methods with UML. Introduction to Design Patterns- Lecture 8

OPC DataHub Offers Advanced Tunnelling of Process Data

Tuesday, October 4. Announcements

Design Pattern and Software Architecture: IV. Design Pattern

OODP Session 5a. Web Page: Visiting Hours: Tuesday 17:00 to 19:00

OPC DataHub Offers Advanced Tunnelling of Process Data

A4 Explain how the Visitor design pattern works (4 marks)

Client Code - the code that uses the classes under discussion. Coupling - code in one module depends on code in another module

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

Appendix A - Glossary(of OO software term s)

OPC DataHub Offers Advanced Tunnelling of Process Data

Facade and Adapter. Comp-303 : Programming Techniques Lecture 19. Alexandre Denault Computer Science McGill University Winter 2004

Team 4: Unicommerce. Team Members

GRASP Design Patterns A.A. 2018/2019

Factories, Builders and Singletons. Steven R. Bagley

Online and Mobile Banking Upgrade November 1 st, 2018

Essential Skills for the Agile Developer. Agile. copyright Net Objectives, Inc.

Design Patterns Thinking and Architecture at Scale

Lecturer: Sebastian Coope Ashton Building, Room G.18 COMP 201 web-page:

Design patterns. Jef De Smedt Beta VZW

Tutorial: Functions and Functional Abstraction. Nathaniel Osgood CMPT

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

CSCI Object Oriented Design: Frameworks and Design Patterns George Blankenship. Frameworks and Design George Blankenship 1

Design Patterns. SE3A04 Tutorial. Jason Jaskolka

Improve Your SystemVerilog OOP Skills

MSO Lecture 1. Wouter Swierstra (adapted by HP) September 11, 2017

Design to interfaces. Favor composition over inheritance Find what varies and encapsulate it

Software Architecture

Summary of the course lectures

What is a Pattern? Lecture 40: Design Patterns. Elements of Design Patterns. What are design patterns?

Software Design and Analysis for Engineers

CSC207H: Software Design Lecture 6

Recap: Class Diagrams

OODP Session 4. Web Page: Visiting Hours: Tuesday 17:00 to 19:00

Responsibilities. Using several specific design principles to guide OO design decisions.

Principles of Software Construction: Objects, Design, and Concurrency. Assigning Responsibilities to Objects. toad. Jonathan Aldrich Charlie Garrod

Object-oriented Software Design Patterns

CSCU9T4: Managing Information

Lethbridge/Laganière 2005 Chapter 9: Architecting and designing software 6

Aggregation. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

Elementary Concepts of Object Class

ADVANCED SOFTWARE DESIGN LECTURE 4 SOFTWARE ARCHITECTURE

Chapter 8: Class and Method Design

Expanding Our Horizons. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 9 09/25/2011

Design Patterns Design patterns advantages:

Lecture 7: Data Abstractions

Computer Science 4U Unit 1. Programming Concepts and Skills Modular Design

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

Object oriented programming Concepts

Design Patterns. Comp2110 Software Design. Department of Computer Science Australian National University. Second Semester

Keywords: Abstract Factory, Singleton, Factory Method, Prototype, Builder, Composite, Flyweight, Decorator.

CPSC 310: Sample Final Exam Study Questions 2014S1 (These are in addition to the Study Questions listed at the end of some lectures)

5 Distributed Objects: The Java Approach

L33.1 Introduction... 2

Object-Oriented Design

MSO Analysis & UML. Hans Philippi (based on the course slides of Wouter Swierstra) August 24, Analysis & UML 1 / 56

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Transcription:

MSO Object Creation Singleton & Object Pool Wouter Swierstra & Hans Philippi October 25, 2018 Object Creation: Singleton & Object Pool 1 / 37

This lecture How to create objects The Singleton Pattern The Object Pool Pattern Object Creation: Singleton & Object Pool 2 / 37

Categories of design patterns The Gang of Four distinguish three categories of design patterns: Behavioural patterns characterize the way in which classes or objects interact or distribute responsibility. Observer, Strategy Structural patterns deal with the composition of classes or objects. Adapter, Bridge, Decorator Creational patterns are concerned with the creation of new objects. Abstract Factory Object Creation: Singleton & Object Pool 3 / 37

Why worry about creation? A lot of the design we have focussed on so far in this course is concerned with figuring out how classes should work together, and how to encapsulate variation hiding implementation details But if we hide implementation details behind an abstract class, who decides which concrete instances should be made? Object Creation: Singleton & Object Pool 4 / 37

Factories Separating creation and usage leads to stronger cohesion Separate two distinct tasks: Defining classes and how they work together Writing factories that instantiate the correct objects for the right situation Object Creation: Singleton & Object Pool 5 / 37

Not everyone agrees... Remember Larman s Creator GRASP principle: Assign class A the responsibility to create instances of a classb if A has a B object This is a good guideline for simple situations, provided you are not programming to an interface If you are, you may need to consider applying creational design patterns Object Creation: Singleton & Object Pool 6 / 37

Creation vs Use Shalloway and Trott say that an object should either:... make and/or manage other objects... or it should use other objects It should never do both Following this rule leads to looser coupling Object Creation: Singleton & Object Pool 7 / 37

Using Objects Program to an interface, not to an implementation Find what varies, and encapsulate it Using Object AbstractClass Concrete ClassA Concrete ClassA Object Creation: Singleton & Object Pool 8 / 37

Example: strategy If the Context creates concrete strategies, it is breaking an abstraction barrier Instead, we want to assign the responsibility for creation to another object Object Creation: Singleton & Object Pool 9 / 37

Factory objects Compare figure 20-3 on page 352 of S&T Object Creation: Singleton & Object Pool 10 / 37

Using vs creating objects The Factory can create values of type AbstractClass on request; it stores all the creational logic and creates a concrete object Using objects means knowing only about the interface, not the implementation The Factory objects should only know how to create concrete instances; they should not rely on the details of the abstract class/interface We are free to add new concrete classes as necessary we know that the client objects should never inspect which concrete class they are using the Open-Closed principle! Object Creation: Singleton & Object Pool 11 / 37

Summary Patterns add indirection (hide information behind an abstract class, move functionality to new classes,... ) This makes code more maintainable, but adds complexity, in case the client code must know about these constructions Factories and other creational patterns are designed to hide the complexity that patterns sometimes introduce Object Creation: Singleton & Object Pool 12 / 37

The Singleton Pattern Factories can be used for more than just controlling which concrete instances to create For instance, factories may implement constraints The Singleton Pattern for example, can be used to guarantee that exactly one object exists of a particular type Object Creation: Singleton & Object Pool 13 / 37

The Singleton Pattern Singleton - instance : Singleton - singletondata - Singleton() + static getinstance() + operation... + getdata Object Creation: Singleton & Object Pool 14 / 37

The Singleton Pattern public static class Singleton { private static Singleton instance; private Singleton() {... } } public static Singleton getinstance() { if (instance == null) { instance = new Singleton(); } return instance; } Object Creation: Singleton & Object Pool 15 / 37

Singleton: example One example of a Singleton could be the CalcTax strategy for a specific country Another example of a Singleton could be a logging service: different objects want to be able to log a message... where the log should be shared by all Object Creation: Singleton & Object Pool 16 / 37

Caution! Singletons are not thread safe If you have multiple threads trying to create a singleton at the same time, you may create two (or more) Singleton objects Solution: The Double-Checked Locking Pattern addresses this, but we will not cover it in this course Object Creation: Singleton & Object Pool 17 / 37

More caution! Singletons are a controversial pattern The Singleton is regarded to be an over applied pattern - it is often not even needed To decide whether a class is truly a singleton, you must ask yourself some questions (Rainsberger 2001): Will every application use this class exactly the same way? Will every application ever need only one instance of this class? So much for singletons Object Creation: Singleton & Object Pool 18 / 37

Case study: electronic banking Today let s suppose we need to write software for online investment services, like alex.nl, that allow users: to browse their investment portfolio to give orders for purchasing or selling stocks... We will see that Factory tasks may be a bit more involving than just creating objects! Object Creation: Singleton & Object Pool 19 / 37

Architecture Java Servlet CORBA C++ Middleware TCP/IP Mainframe Object Creation: Singleton & Object Pool 20 / 37

Requirements The only way to communicate with the mainframe is through TCP/IP connections using a custom messaging protocol The C++ (or C#, or...) middleware is responsible for verifying user orders, before they are executed by the mainframe Object Creation: Singleton & Object Pool 21 / 37

Example interaction User logs in through their webbrowser Middleware gets user ID and password; it reformats message in a format the mainframe can understand Mainframe accepts the request and responds accordingly Object Creation: Singleton & Object Pool 22 / 37

Performance concerns The middleware needs to handle requests from many, many people at the same time We should aim at handling as many transactions as possible - maximizing throughput - and not worry about the time a single transaction needs Object Creation: Singleton & Object Pool 23 / 37

Design question How many TCP/IP connections should I use? 1 is too little all traffic goes over a single connection 100 is too much the bandwidth available could not handle more than 20 This is a high-risk issue; the performance of the entire system can rely on it Object Creation: Singleton & Object Pool 24 / 37

Issues involved Establish a single, robust TCP/IP connection Determine the number of TCP/IP connections that are necessary Establish a method to load balance the connections we don t want to route all traffic through a single connection, while the others are idle What to do first? Object Creation: Singleton & Object Pool 25 / 37

Design choice Start by establishing a single connection In other words, start with a single responsibility, but don t forget that you will have to address these other issues soon Open-closed: insulate yourself from change, but anticipate new requirements We need to design the system so that we can easily change the number of connections used... but that is another responsibility Object Creation: Singleton & Object Pool 26 / 37

TCP/IP management Distinguish separate responsibilities: A Port class is responsible for communicating with the mainframe A PortManager class is responsible for keeping track of the Ports Object Creation: Singleton & Object Pool 27 / 37

How many? There should be multiple Port objects The PortManager should be able to change the exact number easily There should only be a single PortManager object otherwise we don t have control over how many connections are established exactly So here we use the Singleton pattern! Object Creation: Singleton & Object Pool 28 / 37

Functionality The PortManager supports has the following methods and attributes: a private array of Port objects GetInstanceOfPort() - which looks through the array to find an inactive port; if no port is inactive, it sleeps and retries ReturnInstanceOfPort(Port release) - sets the status of the argument port to inactive Object Creation: Singleton & Object Pool 29 / 37

Using Ports Client code is now straightforward: 1 Ask the PortManager for a Port object 2 Complete the transaction 3 Release the Port to the PortManager The client code does not need to know anything about how many ports are active, setting up connections, etc Object Creation: Singleton & Object Pool 30 / 37

Strong cohesion, loose coupling This solution has strong cohesion and loose coupling the Port, Client, and PortManager classes all have a clear set of responsibilities What happens when I need to add error handling? Object Creation: Singleton & Object Pool 31 / 37

Adding Error Handling What happens when a connection goes down? 1 The Client code needs to request another Port 2 The Port that went down should be closed 3 The PortHandler may want to establish a new connection to replace the Port that went down It is clear who is responsible for handling this exceptional scenario Object Creation: Singleton & Object Pool 32 / 37

Are we done? If you re responsible for developing a system that handles millions of dollars worth of transactions, will you sleep easy at night? Object Creation: Singleton & Object Pool 33 / 37

Add checking Maybe we would sleep easier if the PortManager also checked the integrity of the connection Every 15 (or so) minutes it should: 1 Check how many unanswered requests there are for a Port 2 Query all the Ports to see if they are active or not 3 Check how many Ports have run into an error If any of these checks produces unexpected results, warning bells should go off Object Creation: Singleton & Object Pool 34 / 37

Even better... Don t stop there! It s not enough to know that a system is not failing Perhaps you cannot detect connection problems, but people still are not able to use the website for other reasons Log successful transactions Object Creation: Singleton & Object Pool 35 / 37

The Object Pool Pattern Client 0..* 1 Uses * Reusable 0..* ReusablePool -ReusablePool() +getinstance() +acquirereusable() : Reusable +releasereusable(r : Reusable) +setsize(maxsize : int) 1 1 Note that getinstance() refers to the creation of a singleton ReusabePool Object Creation: Singleton & Object Pool 36 / 37

The Object Pool Pattern: Lessons Factories are not just about instantiation! Object Creation: Singleton & Object Pool 37 / 37