-
Notifications
You must be signed in to change notification settings - Fork 43
Add Abstract Factory Design Pattern project #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
2304808
a39b268
2e6fbe5
1313e0f
e5c5a5b
7b2f13e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "editor.suggest.showWords": false, | ||
| "editor.suggest.showKeywords": false | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.restaurant.burgerfactory; | ||
|
|
||
| public class BeefBurger implements Burger { | ||
| @Override | ||
| public void prepare() { | ||
| System.out.println("Preparing a Beef Burger"); | ||
| } | ||
|
|
||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like a simple factory not abstract factory |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.restaurant.burgerfactory; | ||
|
|
||
| public class BeefBurgerRestaurant { | ||
|
|
||
| public Burger createBurger(String request) { | ||
| if (request.equalsIgnoreCase("BEEF")) { | ||
| return new BeefBurger(); | ||
| } else { | ||
| return null; | ||
|
|
||
| } | ||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package com.restaurant.burgerfactory; | ||
|
|
||
| interface Burger { | ||
| void prepare(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.restaurant.burgerfactory; | ||
|
|
||
| public class Main { | ||
| public static void main() { | ||
| Restaurant restaurant = new VeggieBurgerRestaurant(); | ||
| Burger burger = restaurant.orderBurger("Veggie"); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.restaurant.burgerfactory; | ||
|
|
||
| abstract class Restaurant { | ||
| public Burger orderBurger(String request) { | ||
| Burger burger = createBurger(request); | ||
| burger.prepare(); | ||
| return burger; | ||
| } | ||
| abstract Burger createBurger(String request); | ||
| } | ||
|
|
||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.restaurant.burgerfactory; | ||
|
|
||
| public class VeggieBurger implements Burger { | ||
| @Override | ||
| public void prepare() { | ||
| System.out.println("Preparing a Veggie Burger"); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package com.restaurant.burgerfactory; | ||
|
|
||
|
|
||
| public class VeggieBurgerRestaurant extends Restaurant { | ||
| @Override | ||
| public Burger createBurger(String request) { | ||
| if (request.equalsIgnoreCase("veggie")) { | ||
| return new VeggieBurger(); | ||
| } else { | ||
| return null; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not commit any xml file |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| //TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or | ||
| // click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter. | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
|
|
||
| Person person = new PersonBuilder() | ||
| .name("Harshad") | ||
| .email("xyz@gmail.com") | ||
| .contact("9999999999") | ||
| .build(); | ||
|
|
||
| System.out.println(person.getName() + " " +person.getContact() + " " + person.getEmail()); | ||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| public class Person { | ||
| String name; | ||
| String email; | ||
| String contact; | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public String getEmail() { | ||
| return email; | ||
| } | ||
|
|
||
| public void setEmail(String email) { | ||
| this.email = email; | ||
| } | ||
|
|
||
| public String getContact() { | ||
| return contact; | ||
| } | ||
|
|
||
| public void setContact(String contact) { | ||
| this.contact = contact; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| public class PersonBuilder { | ||
| private final Person person; | ||
|
|
||
| public PersonBuilder(){ | ||
| this.person = new Person(); | ||
| } | ||
|
|
||
| public PersonBuilder name(String name){ | ||
| this.person.setName(name); | ||
| return this; | ||
| } | ||
|
|
||
| public PersonBuilder contact(String contact) { | ||
| this.person.setContact(contact); | ||
| return this; | ||
| } | ||
|
|
||
| public PersonBuilder email(String email){ | ||
| this.person.setEmail(email); | ||
| return this; | ||
| } | ||
|
|
||
| public Person build(){ | ||
| return this.person; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <module type="JAVA_MODULE" version="4"> | ||
| <component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
| <exclude-output /> | ||
| <content url="file://$MODULE_DIR$"> | ||
| <sourceFolder url="file://$MODULE_DIR$" isTestSource="false" /> | ||
| </content> | ||
| <orderEntry type="inheritedJdk" /> | ||
| <orderEntry type="sourceFolder" forTests="false" /> | ||
| </component> | ||
| </module> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import java.util.List; | ||
|
|
||
| public class LogPrinter { | ||
| public void printLog(List<String> logs) { | ||
| System.out.println("---- Printing Order ----"); | ||
| for (String log : logs) { | ||
| System.out.println(log); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import java.util.List; | ||
| import java.util.ArrayList; | ||
|
|
||
| public class Logger { | ||
|
|
||
| private List<String> L1; | ||
| private static Logger instance; | ||
|
|
||
|
|
||
| private Logger () { | ||
| L1 = new ArrayList<>(); | ||
| } | ||
|
|
||
| public static synchronized Logger getInstance() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the basic implementation, please add with double checking and the Bill Pugh method as well |
||
| if (instance == null) { | ||
| synchronized (Logger.class) { | ||
| if (instance == null) { | ||
| instance = new Logger(); | ||
| } | ||
| } | ||
| } | ||
| return instance; | ||
| } | ||
|
|
||
| public void log(String messages) { | ||
| System.out.println(messages); | ||
| L1.add(messages); | ||
| } | ||
|
|
||
| public List<String> getLogs() { | ||
| return L1; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| Logger log1 = Logger.getInstance(); | ||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| System.out.print("Enter to input the menu: "); | ||
| String message = scanner.nextLine(); | ||
|
|
||
| if (message.equalsIgnoreCase("Burger")){ | ||
| log1.log("Burger Created"); | ||
| log1.log("Fries Added"); | ||
| } | ||
|
|
||
|
|
||
| Logger log2 = Logger.getInstance(); | ||
|
|
||
|
|
||
| System.out.print("Enter to add drinks and Tacos : "); | ||
| String msg2 = scanner.nextLine(); | ||
| if (msg2.equalsIgnoreCase("tacos")) { | ||
| log2.log("Drink Added"); | ||
| } | ||
|
|
||
| System.out.print("Enter to see the order: "); | ||
| String message1 = scanner.nextLine(); | ||
|
|
||
| if (message1.equalsIgnoreCase("order")) { | ||
| LogPrinter pri1 = new LogPrinter(); | ||
| pri1.printLog(log1.getLogs());} | ||
|
|
||
| // LogPrinter pri2 = new LogPrinter(); | ||
|
|
||
| // pri2.printLog(log2.getLogs()); | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not commit this file