
CSE316 : FUNDAMENTALS OF SOFTWARE DEVELOPMENT Lecture 14a : Intro to Design Patterns: Creational Design Patterns Topics ■ Intro to Design Patterns – Creational – Structural – Behavioral ■ Builder Pattern ■ Singleton ■ Prototype ■ Factory Method Pattern ■ Abstract Factory Pattern CSE316 : Fund of SW Development - Tony Mione, SUNY Korea - 2019 2 Intro to Design Patterns ■ Design patterns – are template solutions to common programming problems – Help us follow OO principles ■ There are several ‘categories’ of design patterns – Creational – These patterns help build objects in some way – Structural – Help assemble larger structures using various composition and aggregation techniques – Behavioral – These patterns are concerned with algorithms, assignment of responsibilities, and interactions between objects CSE316 : Fund of SW Development - Tony Mione, SUNY Korea - 2019 3 Creational Patterns ■ Creational Patterns include: – Builder – Singleton – Prototype – Factory Method Pattern – Abstract Factory Pattern CSE316 : Fund of SW Development - Tony Mione, SUNY Korea - 2019 4 Builder Pattern ■ Why Builder? – To build an object composed of other objects – The pattern hides the creation of component objects from the main object – The pattern also hides the details of creating the object from the ‘client’ code. CSE316 : Fund of SW Development - Tony Mione, SUNY Korea - 2019 5 Builder Pattern ■ Main parts of this pattern: – A Director – This oversees construction of the object – A Builder – An interface that provides methods that build the component objects – A Concrete Builder – This implements the Builder interface to build a specific type of object – A Product Interface – An interface used by product to allow creation of component objects CSE316 : Fund of SW Development - Tony Mione, SUNY Korea - 2019 6 Builder Pattern CSE316 : Fund of SW Development - Tony Mione, SUNY Korea - 2019 7 // This interface is for an abstract builder Builder Pattern - Example public interface PizzaBuilder { public void addSauce(); public void addCheese(); ■ Example code: Pizza Store with ‘Pizza Builders’ public void addTopings(); public Pizza getPizza(); } // This is a concrete builder for NY style pizza // This is the 'Director' from the Builder Pattern public class NYPizzaBuilder implements PizzaBuilder { public class PizzaStore { private Pizza pizza; private PizzaBuilder pizzaBuilder; public NYPizzaBuilder() { this.pizza = new Pizza(); public PizzaStore(PizzaBuilder pizzaBuilder) { } this.pizzaBuilder = pizzaBuilder; @Override } public void addSauce() { pizza.setSauce("Red"); public Pizza getPizza() { } return pizzaBuilder.getPizza(); @Override } public void addCheese() { pizza.setCheese("Mozzarella"); public void makePizza() { } this.pizzaBuilder.addSauce(); @Override this.pizzaBuilder.addCheese(); public void addTopings() { this.pizzaBuilder.addTopings(); pizza.setToppings("Pepperoni"); } } } @Override public Pizza getPizza() { return this.pizza; } } CSE316 : Fund of SW Development - Tony Mione, SUNY Korea - 2019 8 Builder Pattern - Example // Concrete Product: Pizza public class Pizza implements PizzaPlan { private String sauce; private String cheese; public interface PizzaPlan { private String toppings; // This is an interface for a product (Pizza) @Override public void setSauce(String sauce); public void setSauce(String sauce) { public void setCheese(String cheese); this.sauce = sauce; public void setToppings(String toppings); } } public String getSauce() { return sauce; } @Override public void setCheese(String cheese) { this.cheese = cheese; } public String getCheese() { return cheese; } @Override public void setToppings(String toppings) { this.toppings = toppings; } public String getToppings() { return toppings; } public String toString() { return "Pizza with: " + sauce + " sauce, " + cheese + " cheese, and " + toppings + " toppings."; } } CSE316 : Fund of SW Development - Tony Mione, SUNY Korea - 2019 9 Builder Pattern - Example // Finally – How to use all this //Another concrete builder… this one for Boston Style public class BostonPizzaBuilder implements PizzaBuilder { public class TestPizzaBuilder { Pizza pizza; public static void main(String[] args) { public BostonPizzaBuilder() { this.pizza = new Pizza(); PizzaBuilder nyPizza = new NYPizzaBuilder(); } PizzaStore pizzaStore = new PizzaStore(nyPizza); @Override pizzaStore.makePizza(); public void addSauce() { Pizza myPizza = pizzaStore.getPizza(); pizza.setSauce("White"); System.out.println("My Pizza: " + myPizza); } @Override PizzaBuilder bostonPizza = new BostonPizzaBuilder(); public void addCheese() { pizzaStore = new PizzaStore(bostonPizza); pizza.setCheese("Feta"); pizzaStore.makePizza(); } Pizza myOtherPizza = pizzaStore.getPizza(); @Override System.out.println("My other pizza: " + myOtherPizza); public void addTopings() { } pizza.setToppings("clam and mushroom"); } } @Override public Pizza getPizza() { return this.pizza; // Output from a test run } } My Pizza: Pizza with: Red sauce, Mozzarella cheese, and Pepperoni toppings. My other pizza: Pizza with: White sauce, Feta cheese, and clam and mushroom toppings. CSE316 : Fund of SW Development - Tony Mione, SUNY Korea - 2019 10 Singleton Pattern ■ This pattern: – Supports applications that need an object of which only 1 instance can be created – Useful anywhere there is a single resource to share ■ Example Uses: – Print Spooler – ThreadPool – Memory Manager – Deck of Cards CSE316 : Fund of SW Development - Tony Mione, SUNY Korea - 2019 11 Singleton Pattern ■ Components: – The Singleton Class – A single class that enforces creation of only 1 instance ■ Private Constructor ■ A private variable holding the instance of the Singleton ■ public getInstance() method ■ The getInstance() method: 1. Checks if the instance has been created 2. If it has, return that instance (reference) 3. If not, use the Constructor to build the instance and then return the instance CSE316 : Fund of SW Development - Tony Mione, SUNY Korea - 2019 12 Singleton Pattern CSE316 : Fund of SW Development - Tony Mione, SUNY Korea - 2019 13 Singleton Pattern ■ Example Singleton Code – Counter public class Singleton { public void increment() { count++; private int count; } static Singleton theInstance; public String toString() { return "Count: " + count; private Singleton() { } count = 0; } } public static Singleton getInstance() { if (theInstance == null) { theInstance = new Singleton(); } return theInstance; } CSE316 : Fund of SW Development - Tony Mione, SUNY Korea - 2019 14 Singleton Pattern ■ Example Singleton Code – Counter public class UseSingleton { public static void main(String[] args) { // Output from a run: Singleton s1 = Singleton.getInstance(); Singleton s2 = Singleton.getInstance(); S1: Count: 3 S2: Count: 3 s1.increment(); s2.increment(); s1.increment(); System.out.println("S1: " + s1); System.out.println("S2: " + s2); } } CSE316 : Fund of SW Development - Tony Mione, SUNY Korea - 2019 15 Prototype Pattern ■ This pattern: – Allows ‘cloning’ or ‘copying’ an object to create a new object – Allows adding any subclass of a known superclass at runtime – Good when there are numerous classes that you only want to use if needed at runtime – Reduces need for creating subclasses – Useful when creating an object from scratch is expensive CSE316 : Fund of SW Development - Tony Mione, SUNY Korea - 2019 16 Prototype Pattern CSE316 : Fund of SW Development - Tony Mione, SUNY Korea - 2019 17 Prototype Pattern - Example public class Shape implements Cloneable { // This is the prototype (the PrototypeInterface is ‘Cloneable’) private int id; private String name; private static int lastId = 0; public Shape() { id = ++lastId; name = "Unknown"; } public Shape(String name) { this.name = name; id = ++lastId; } public void setName (String name) { this.name = name; } public void setId(int id) { this.id = id; } public String getName() { return name; } public int getId() { return id; } } CSE316 : Fund of SW Development - Tony Mione, SUNY Korea - 2019 18 Prototype Pattern - Example // This is a prototype class which extends the Prototype // Tests the prototype Shape and clonging public class Circle extends Shape { public class UseShape { private int radius; public static void main(String[] args) { public Circle() { Circle firstShape = new Circle(1); super("Circle"); Circle second = (Circle) firstShape.makeCopy(); radius = 1; Circle third = second; } public Circle(int radius) { System.out.println("1st shape: " + firstShape); super("Circle"); System.out.println("second: " + second); this.radius = radius; System.out.println("Circle is made"); System.out.println("C1: " + System.identityHashCode(firstShape)); } System.out.println("C2: " + System.identityHashCode(second)); public Shape makeCopy() { System.out.println("C3: " + System.identityHashCode(third)); Circle circleObject = null; System.out.println("Circle is being cloned"); } try { } circleObject = (Circle) super.clone(); // Following is output from a run } catch (CloneNotSupportedException e) { e.printStackTrace(); Circle is made } Circle is being cloned circleObject.radius = this.radius; 1st shape: I am: Circle, with id=1 and a radius of 1 circleObject.setName(this.getName()); second: I am: Circle, with id=1 and a radius of 1 circleObject.setId(this.getId()); C1: 905544614 return circleObject; C2: 2137589296 } C3: 2137589296 public String toString() { return "I am: " + getName() + ", with id=" + getId() + " and a radius of " + radius; } } CSE316 : Fund of SW Development - Tony Mione, SUNY Korea - 2019 19 Factory Method Pattern ■
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages37 Page
-
File Size-