Builder Pattern. What’s Builder: TO find a solution to the telescoping constructor Problem: Too many parameters Solution: To get an abstract object, a.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

1 Classes and Objects in Java Parameter Passing, Delegation, Visibility Control, and Object Cleanup.
Programming Methodology (1). Implementing Methods main.
Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
Object Oriented Programming (OOP) with Java. Classes and Objects Inheritance Polymorphism.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Programming With Java ICS 201 University Of Ha’il1 Chapter 8 Abstract Class.
Remedial Tutorials Week 2 How to think about programming.
Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.
Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values.
Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values.
1-Sep-15 Scala Apologia. 2 Java What’s wrong with Java? Not designed for highly concurrent programs The original Thread model was just wrong (it’s been.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
CS 157B: Database Management Systems II January 30 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
Programming Progamz pls. Importance VERY IMPORTANT.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 8, page 1 Sun Certified Java 1.4 Programmer Chapter 8 Notes Gary Lance
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance
24-Oct-15 Java to Scala. Types Primitives char byte short int long float double boolean Objects String … and many more … Objects Char Byte Short Int Long.
Introduction to Java Classes and Objects. What is a class A class is description of a structure that contains both data and methods – Describes a set.
Design Patterns Introduction to Design Patterns Eriq Muhammad Adams J. Mail : | Blog :
Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Andreas Enbohm Capgemini Sverige AB Scala or ”The Free Lunch is Over”
Team 6 “The Unparseables” Design Patterns Chain of Responsibility Observer Flyweight 1.
Announcements Final Exam:TBD. public static void main(String [] args) { final int ASIZE = 5; int [] intArray= new int[ASIZE]; for(int i = 0; i < ASIZE;
Enterprise Java v041109Container Managed Relationships1 Container Managed Relationships (CMR) Source: “Enterprise JavaBeans, 3rd Edition”, Richard Monson-Haefel.
Arrays and Objects CIS 362. The familiar Employee class.
O O P Polymorphism Object Oriented Programming Prepared & Presented by: dr.Ismail Farahat Chapter 4.
Object Serialization.  When the data was output to disk, certain information was lost, such as the type of each value.  If the value "3" is read from.
Class Student class Student { private: string id; string firstName, lastName; float gpa; public: void Read() void Write() string getGPA() void setGPA(
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Architectural Patterns Support Lecture Patterns Pattern: A representation of a proven solution. Problem Applicable Forces Solution Consequences Benefits.
Association Association represents a general binary relationship that describes an activity between two classes. The relationship allows objects to call.
COT int[] x = {3,7,2,4,1}; int[] y = {5,8,6,9}; x = y; x[2] = 0; for (int k: y) { System.out.print(k + " "); }
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
6-Feb-16 Scala Apologia. 2 Java What’s wrong with Java? Not designed for highly concurrent programs The original Thread model was just wrong (it’s been.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Access Control to Class Members tMyn1 Access Control to Class Members In its support for encapsulation, the class provides two major benefits. First, it.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
Hibernate Thuy, Le Huu. Pentalog VN. Agenda Hibernate Annotations Improving performance – Lazy loading – Fetching Strategies – Dynamic insert, dynamic.
MANY-TO-MANY MAPPING. May 12, 2011  Implemented using a Set java collection  Mediator table is mandatory – Join table  Join table contains foreign.
Comp1004: Building Better Objects II Encapsulation and Constructors.
13-Jun-16 Scala Apologia. 2 Java What’s wrong with Java? Not designed for highly concurrent programs The original Thread model was just wrong (it’s been.
CLASSES IN JAVA Primitive Types Example of a Class Allocating Objects of a Class Protecting Class data Constructors Static data and Static Methods.
Staples are our staple Building upon our solution.
IB Computer Science Content developed by Dartford Grammar School Computer Science Department UML.
Classes (Part 1) Lecture 3
Lecture 6 Object Oriented Programming Using Java
Examples of Classes & Objects
Abstract Class, Interface, Package
Chapter 5 Hierarchies IS-A associations superclasses subclasses
What is Encapsulation, Benefits, Implementation in Java
Methods Additional Topics
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
The structure of Interactive Software
Classes Variables That Are Not of a Built-in Type Are Objects
An Introduction to Java – Part II
Java Programming Workshop
Simple Classes in C# CSCI 293 September 12, 2005.
Classes & Objects: Examples
Scala Apologia 1-Jan-19.
Dynamic Memory Allocation
Java Lesson 36 Mr. Kalmes.
Code Animation Examples
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
CS 240 – Advanced Programming Concepts
Presentation transcript:

Builder Pattern

What’s Builder: TO find a solution to the telescoping constructor Problem: Too many parameters Solution: To get an abstract object, a builder, that receives each initialization parameter step by step and then returns the resulting constructed object at once

Example public class User { private final String firstName; //required private final String lastName; //required private final int age; //optional private final String phone; //optional private final String address; //optional }

Option One public User(String firstName, String lastName) { this(firstName, lastName, 0); } public User(String firstName, String lastName, int age) { this(firstName, lastName, age, ''); } public User(String firstName, String lastName, int age, String phone) { this(firstName, lastName, age, phone, ''); } public User(String firstName, String lastName, int age, String phone, String address) { this.firstName = firstName; this.lastName = lastName; this.age = age; this.phone = phone; this.address = address; }

Option Two public class User { private String firstName; // required private String lastName; // required private int age; // optional private String phone; // optional private String address; //optional public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; }

public void setAge(int age) { this.age = age; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; }

Option Three public class User { private final String firstName; // required private final String lastName; // required private final int age; // optional private final String phone; // optional private final String address; // optional private User(UserBuilder builder) { this.firstName = builder.firstName; this.lastName = builder.lastName; this.age = builder.age; this.phone = builder.phone; this.address = builder.address; }

public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public int getAge() { return age; }

public String getPhone() { return phone; } public String getAddress() { return address; }

public static class UserBuilder { private final String firstName; private final String lastName; private int age; private String phone; private String address; public UserBuilder(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; }

public UserBuilder age(int age) { this.age = age; return this; } public UserBuilder phone(String phone) { this.phone = phone; return this; } public UserBuilder address(String address) { this.address = address; return this; } public User build() { return new User(this); }

public User getUser() { return new User.UserBuilder('Jhon', 'Doe').age(30).phone(' ').address('Fake address 1234').build(); }