Abstract Factory Pattern

Slides:



Advertisements
Similar presentations
Creational Design Patterns. Creational DP: Abstracts the instantiation process Helps make a system independent of how objects are created, composed, represented.
Advertisements

Chapter 5: The Singleton Pattern
Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
The Singleton Pattern II Recursive Linked Structures.
Singleton vs utility class  at first glance, the singleton pattern does not seem to offer any advantages to using a utility class  i.e., a utility class.
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.
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.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
1 Scenario: Audio Clip Imagine that your application played an audio clip Based on user action, a different audio clip may begin playing You want only.
1 What is the “volatile” Keyword? You can skip locking (thread synchronization) in some cases by using the volatile variables. –No locking/unlocking =
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.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Nested References 2 inner reference data types Classes-Interfaces.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
ECE122 Feb. 22, Any question on Vehicle sample code?
Singleton and Basic UML CS340100, NTHU Yoshi. What is UML Unified Modeling Language A standardized general-purpose modeling language in the field of software.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
Design Patterns Yonglei Tao. Design Patterns  A design pattern describes a recurring design problem, a solution, and the context in which that solution.
QUEUES What are Queues? Creating a Queue Enqueuing and Dequeuing Testing for an Empty Queue.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
Singleton Duchenchuk Volodymyr Oksana Protsyk. 2 /48.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
Design Patterns Introduction
UNDERSTANDING RECURSIVE CLASSES CMSC 150: Lecture 22.
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Singleton Pattern Presented By:- Navaneet Kumar ise
The Singleton Pattern (Creational)
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Arc: Communications between Addins Dr Andy Evans.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
CSE 1020:Using Objects Mark Shtern 1-1. Summary Read API Method binding and overloading Development process Input validation Assert 1-2.
Section 2.3 Array-Based StringLog ADT Implementation.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Section 2.2 The StringLog ADT Specification. 2.2 The StringLog ADT Specification The primary responsibility of the StringLog ADT is to remember all the.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
Section 3.7 Linked-Based Implementations
Sections 3.4 Formal Specification
Topic: Classes and Objects
Design Patterns – Chocolate Factory (from Head First Design Patterns)
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
Design Patterns (GoF) contains the creational patterns:
Software Engineering Lecture 7 - Design Patterns
Programming Design Patterns
null, true, and false are also reserved.
SE-2811 Software Component Design
CSE 1030: Implementing Mixing static and non-static features
Initializing Objects.
CS 302 Week 9 Jim Williams.
What is Singleton Category: Creational pattern
Singleton Pattern Pattern Name: Singleton Pattern Context
Singleton design pattern
Example: LinkedSet<T>
CS 350 – Software Design Singleton – Chapter 21
Stacks CS-240 Dick Steflik.
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.
Chapter 8, Design Patterns Singleton
Objects with ArrayLists as Attributes
Chapter 5 Classes.
Presentation transcript:

Abstract Factory Pattern

Singleton Pattern One of a kind objects

Creating an object How would you create a single object? New MyObject(); What if another object wanted to create a MyObject? Could it call new on MyObject again? Yes So as long as we have a class, can we always instantiate it one or more times? Yes..only if it’s a public And if not? If its not a public class, only classes in the same package can instantiate it. But they can still instantiate it more than once

Do you know you could do this? Public MyClass { private MyClass() { } } It is a class that can’t be instantiated because its constructor is private The code in MyClass is the only code that can call it What about the instance that can call a class like this, otherwise it’s a chicken and egg problem

getInstance() Class method Used to refer a class in an static method Public MyClass { private MyClass() {} public static MyClass getInstance (); } Instance would be MyClass.getInstance();

First Singleton application public class Singleton { private static Singleton uniqueInstance; // other useful instance var here private Singleton() {} public static Singleton.getInstance() { if (uniqueInstance == null){ uniqueInstance = new Singleton(); } return uniqueInstance; } //other methods

Chocolate Factory 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) { System.out.println("Creating unique instance of Chocolate Boiler"); uniqueInstance = new ChocolateBoiler(); System.out.println("Returning instance of Chocolate Boiler"); return uniqueInstance;

public void fill() { if (isEmpty()) { empty = false; boiled = false; // fill the boiler with a milk/chocolate mixture } public void drain() { if (!isEmpty() && isBoiled()) { // drain the boiled milk and chocolate empty = true; public void boil() { if (!isEmpty() && !isBoiled()) { // bring the contents to a boil boiled = true; public boolean isEmpty() { return empty; public boolean isBoiled() { return boiled; } }

Singleton