Design Patterns – Chocolate Factory (from Head First Design Patterns)

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Design Patterns.
Chapter 5: The Singleton Pattern
The Singleton Pattern II Recursive Linked Structures.
Jan Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
Oct Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
Nov R McFadyen1 Design Patterns (GoF) contains the creational patterns: Abstract factory Builder Factory method (section 23.3 has a Simple.
March R McFadyen1 Design Patterns (GoF) contains the creational patterns: Abstract factory Builder Factory method (in Larman) Prototype Singleton.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
March R McFadyen1 GoF (Gang of Four): Gamma, Johnson, Helm & Vlissides Book: Design Patterns: Elements of Reusable Object-Oriented Software.
Spring 2010ACS-3913 Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
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.
1 What is the “volatile” Keyword? You can skip locking (thread synchronization) in some cases by using the volatile variables. –No locking/unlocking =
Design Patterns Ref : Chapter 15 Bennett et al. useful groups of collaborating classes that provide a solution to commonly occuring problems. provide.
Winter 2007ACS-3913 Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
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.
11/18/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
Design Patterns Singleton & Factory Pattern Eriq Muhammad Adams J. Mail : | Blog :
Design Patterns Yonglei Tao. Design Patterns  A design pattern describes a recurring design problem, a solution, and the context in which that solution.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
Singleton Duchenchuk Volodymyr Oksana Protsyk. 2 /48.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Design Patterns Introduction
Findbugs Tin Bui-Huy September, Content What is bug? What is bug? What is Findbugs? What is Findbugs? How to use Findbugs? How to use Findbugs?
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Singleton Pattern Presented By:- Navaneet Kumar ise
The Singleton Pattern (Creational)
Arc: Communications between Addins Dr Andy Evans.
1 Design Patterns prepared for COMP314, Bernhard Pfahringer see links on the web page as well!
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
IGCSE ECONOMICS COSTS To explain the difference between the long run and the short run. To identify and calculate the various different costs To explain.
Sun Proprietary/Confidential: Internal Use Only 1 Multi-Threading Primer Byron Nevins December 10, 2007.
Patterns in programming
Module Road Map Refactoring Why Refactoring? Examples
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
Abstract Factory Pattern
Singleton Pattern Command Pattern
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
Design Patterns (GoF) contains the creational patterns:
Java Software Structures: John Lewis & Joseph Chase
Stack Data Structure, Reverse Polish Notation, Homework 7
Design Patterns in Operating Systems
Advanced Programming Behnam Hatami Fall 2017.
Software Engineering Lecture 7 - Design Patterns
Programming Design Patterns
SE-2811 Software Component Design
Chapter 1: Computer Systems
Pattern- a general idea for which there
Topic 1: Problem Solving
What is Singleton Category: Creational pattern
Singleton Pattern Pattern Name: Singleton Pattern Context
Singleton design pattern
Singleton …there can be only one….
Chapter 30 Condition Variables
Strategy and Template Method Patterns, Single User Protection
CS 350 – Software Design Singleton – Chapter 21
Focus of the Course Object-Oriented Software Development
SE-2811 Software Component Design
Design pattern Lecture 6.
Naming & Code Reviews.
5. Strategy, Singleton Patterns
Software Engineering and Architecture
More concurrency issues
GoF Patterns Ch. 26.
Threads and concurrency / Safety
Presentation transcript:

Design Patterns – Chocolate Factory (from Head First Design Patterns)

Scenario Automated chocolate bar maker Large chocolate boiler General process: Fill with ingredients Boil the chocolate Drain the tank Chocolate goes to the next step Two data members empty boiled

Scenario (cont.) Method fill Method drain Method boil If empty, empty = false, boiled = false Method drain If not empty and isboiled, empty = true Method boil If not empty and not boiled, boiled = true

Scenario (cont.) Possible issue: Software shouldn’t be able to think that there is / create two ChocolateBoiler instances Why not?

Scenario (cont.) Possible issue: Software shouldn’t be able to create two ChocolateBoiler instances Why not? There is only one physical ChocolateBoiler instance Steps could get done out of sync on two software instances (e.g. drain an empty boiler, two fills in a row)

Possible Solution Use Singleton pattern for ChocolateBoiler class Only one ChocolateBoiler can be created Always access that ChocolateBoiler

public class ChocolateBoiler { private boolean empty; private boolean boiled; private static ChocolateBoiler uniqueInstance; private ChocolateBoiler() { empty = true; boiled = false; } public static ChocolateBoiler getInstance () { if (uniqueInstance == null) { uniqueInstance = new ChocolateBoiler(); } return uniqueInstance; } }

Is this really a good solution? What possible problem can occur? Hint: think about threads Three columns: Thread one Thread two Value of uniqueInstance

How to fix problem? Idea Make getInstance method synchronized public static synchronized Singleton getInstance()

How to fix problem? Idea Make getInstance method synchronized public static synchronized Singleton getInstance()

Problem with synchronization Only used first time through Later synchronization overhead is totally unnecessary What can be done? Ignore problem (if not mission-critical) Use “eager creation” (static initialization of uniqueInstance) rather than “lazy creation” (creating at run-time, after code check for null instance) Double-checked locking – put synchronization only around body of ‘if’ statement

Singleton How to generalize Singleton pattern to support creation of up to N instances?

Singleton as an Anti-Pattern Many criticisms of singleton have been put forth Issues Function like a global variable Mix creation and structure Difficult to test Overall: may want to look for alternatives, only use Singleton as a last resort