Question

Let us consider the builder pattern where Product has four possible parts Part1, Part2, Part3, and...

Let us consider the builder pattern where Product has four possible parts Part1, Part2, Part3, and Part4 (all integer).

Assume we have three concrete builders: ConcreteBuilder1, ConcreteBuilder2, and ConcreteBuilder3.

ConcreteBuilder1 assigns 11, 12, 13, and 14 to Part1, Part2, Part 3, and Part4 respectively.

ConcreteBuilder2 assigns 21, 22, 23, and 24 to Part1, Part2, Part 3, and Part4 respectively.

ConcreteBuilder3 assigns 31, 32, 33, and 34 to Part1, Part2, Part3, and Part4 respectively.

1) Give the UML diagram.

2) Implement the UML class diagram given in 1 in Java.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1) UML diagram below


2) Java prpgram below

class Product
{
  
private String part1;
private String part2;
private String part3;
private String part4;
  
public void setPart1(String _part1)
{   this.part1 = _part1;
}
  
public void setPart2(String _part2)
{
this.part2 = _part2;
}
  
public void setPart3(String _part3)
{
this.part3 = _part3;
}
  
public void setPart4(String _part4)
{
this.part4 = _part4;
}
  
public String toString()
{
return " Part 1 : "+part1+
"\nPart 2 : "+part2+
"\nPart 3 : "+part3+
"\nPart 4 : "+part4;
}
}


interface Builder{
  
void buildPart1();
void buildPart2();
void buildPart3();
void buildPart4();
Product getProduct();
}

class ConcreteBuilder1 implements Builder{
Product product;
  
ConcreteBuilder1(){
this.product = new Product();
}
public void buildPart1(){
product.setPart1("11");
}
public void buildPart2(){
product.setPart2("12");
}
public void buildPart3(){
product.setPart3("13");
}
public void buildPart4(){
product.setPart4("14");
}
public Product getProduct(){
return product;
}
}

class ConcreteBuilder2 implements Builder{
Product product;
  
ConcreteBuilder2(){
this.product = new Product();
}
public void buildPart1(){
product.setPart1("21");
}
public void buildPart2(){
product.setPart2("22");
}
public void buildPart3(){
product.setPart3("23");
}
public void buildPart4(){
product.setPart4("24");
}
public Product getProduct(){
return product;
}
}

class ConcreteBuilder3 implements Builder{
Product product;
  
ConcreteBuilder3(){
this.product = new Product();
}
public void buildPart1(){
product.setPart1("31");
}
public void buildPart2(){
product.setPart2("32");
}
public void buildPart3(){
product.setPart3("33");
}
public void buildPart4(){
product.setPart4("34");
}
public Product getProduct(){
return product;
}
}

class ProductAssembler {

Builder builder ;
ProductAssembler (Builder builder){
this.builder = builder;
}

public void assemble(){
builder.buildPart1();
builder.buildPart2();
builder.buildPart3();
builder.buildPart4();
}
}

public class Main {
public static void main (String args[]){
  
Builder b1 = new ConcreteBuilder1();
ProductAssembler assembler = new ProductAssembler(b1);
  
assembler.assemble();
  
Product finalProduct = b1.getProduct();
System.out.println(" Final Product " + finalProduct);
  
}
}

Add a comment
Know the answer?
Add Answer to:
Let us consider the builder pattern where Product has four possible parts Part1, Part2, Part3, and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Parts This Skill Builder will require you to write several functions in which loops will...

    In Parts This Skill Builder will require you to write several functions in which loops will be the focus. In addition, some of these function will require you to design and implement finite state machines. So, let's get started! The template below has a class called SkillBuilder6 with a set of skeleton methods provided. The requirements for each method is provided below Left Triangle In the template below, SkillBuilder6 has a method with the following signature: public static String leftRightTriangle...

  • C++ Program 2 Consider a class Employee with data members: age (an integer), id (an integer)...

    C++ Program 2 Consider a class Employee with data members: age (an integer), id (an integer) and salary (a float), and their corresponding member functions as follows: class Employee { private: int age; int id; float salary: public: Employee; // default constructor: age=0, id=0, and salary=0 void setAge(int x); // let age = x void setId(int x); // let id = x void setSalary(float x); // salary = x int getAge(); // return age int getId; // return id float...

  • In Problem Set 7 you designed and implemented a Message class. This time, let's design and...

    In Problem Set 7 you designed and implemented a Message class. This time, let's design and implement a Mailbox class in a file named Mailbox java. Do the following with this class • You may use the Message class from PS 7. You will have to add new features to the Message class from PS 7 as you work through this problem. You are welcome to start with my sample solution if you wish • Suppose there are multiple mail...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT