0% found this document useful (0 votes)
2 views4 pages

Design Patterns: Singleton & Factory

The document presents two design patterns: the Singleton Pattern and the Factory Method Pattern. The Singleton Pattern is demonstrated with a Logger class that ensures only one instance is created, while the Factory Method Pattern is illustrated through various document classes (Word, PDF, Excel) and their respective factories. Each pattern includes code examples and a main method to showcase their functionality.

Uploaded by

madhurimannem94
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Design Patterns: Singleton & Factory

The document presents two design patterns: the Singleton Pattern and the Factory Method Pattern. The Singleton Pattern is demonstrated with a Logger class that ensures only one instance is created, while the Factory Method Pattern is illustrated through various document classes (Word, PDF, Excel) and their respective factories. Each pattern includes code examples and a main method to showcase their functionality.

Uploaded by

madhurimannem94
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Module 1: Design Patterns and Principles

Exercise 1: Singleton Pattern


Code:

public class Logger {

private static Logger instance1;

private Logger()

[Link]("Created a logger instance");

public static Logger get()

if(instance1==null)

instance1=new Logger();

return instance1;

public void log(String message) {

[Link]("The message is: " + message);

public static void main(String[] args)

Logger a=[Link]();
Logger b=[Link]();

Output:

Exercise 2: Factory Method pattern


Code:

public class Main {

public interface Document {

void read();

public static class WordDocument implements Document {

public void read() {

[Link]("I am performing read operation in Word document.");

}
public static class PdfDocument implements Document {

public void read() {

[Link]("I am performing read operation in PDF document.");

public static class ExcelDocument implements Document {

public void read() {

[Link]("I am performing read operation in Excel document.");

public static abstract class DocumentFactory {

public abstract Document createDocument();

public static class WordDocFactory extends DocumentFactory {

public Document createDocument() {

return new WordDocument();

public static class PdfDocFactory extends DocumentFactory {

public Document createDocument() {

return new PdfDocument();

public static class ExcelDocFactory extends DocumentFactory {

public Document createDocument() {


return new ExcelDocument();

public static void main(String[] args) {

DocumentFactory wordFactory = new WordDocFactory();

Document wordDoc = [Link]();

[Link]();

DocumentFactory pdfFactory = new PdfDocFactory();

Document pdfDoc = [Link]();

[Link]();

DocumentFactory excelFactory = new ExcelDocFactory();

Document excelDoc = [Link]();

[Link]();

Output:

You might also like