Week 7 Flashcards
What is a Design Pattern?
Design Patterns are meant to provide a general reusable solution to one type of problems during software development - they provide an architecture
What are the 3 Principles of OO design?
Identify the aspects of your application that vary and separate them from what stays the same
Program to an interface, not an implementation
Favor ‘object composition’ over class inheritence
What are 3 types of Design Patterns?
Creational Patterns - object creation
Structural Patterns - composition of classes and objects
Behavioral Patterns - interaction between objects
What is the difference between the Factory Pattern and the Abstract Factory Pattern?
Factory Pattern - Uses inheritance (extends class and override a factory method) and gives you a complete object in one shot
Abstract Factory Pattern - Uses composition (defines abstract type for creating a family of products and lets subclasses decide how those products are produced) and returns a family of related classes
How does the Builder Pattern work?
The Builder specifies the interface for creating parts of the complex (Product) object
The ConcreteBuilder objects create and assemble parts that make up the Product through the Builder interface
The Director object takes responsibility for the construction process of the complex (Product) object, however it delegates the actual creation and assembly to the Builder interface
What is the main difference between Factory, Abstract and Builder Patterns?
Factory Pattern constructs a complete object in one shot
Abstract Factory Pattern returns a family of related classes
Builder Pattern Constructs complex object step by step
What are the benefits of Builder Pattern?
Encapsulates the way a complex object is constructed
Allows objects to be constructed in a multistep and varying process (as opposed to one-step factories)
Hides internal representation of the product from the client
What is the Singleton Pattern?
“Ensures that a class has only one instance, and provides and global point of access to it
Problem Want to have just a single instance of an object
Examples:
Window managers
File systems
Print spoolers
What are the 3 ways to create a Singleton Pattern?
Static Member - Create a private static variable of the Singleton class. This is the only instance of Singleton class
Private Constructor - Create a private constructor for making sure an outer class can NOT instantiate object from the Singleton class
Static public method - Create a global point of access to get a Singleton instance
What is Lazy Initialisation?
The Singleton class employs a technique known as Lazy Initialisation to create the singleton, to ensure that the singleton instance is not created until the getInstance() method is called for the first time (i.e created only when needed)
How do you create a Thread-Safe Singleton class?
Easiest way to create a Thread-Safe singleton class is to make the global access method synchronized, so that only one thread can execute this method at a time
How do you avoid extra overhead with Thread-Safe Singleton?
To avoid this extra overhad every time, Double Checked Locking Principle is used
Synchronized block is used inside the if condition with an additional check to ensure that only one instance of singleton class is created