SlideShare a Scribd company logo
Singleton
Agenda ,[object Object],[object Object],[object Object],[object Object]
Singleton ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Singleton Classic ,[object Object],[object Object],public class CacheManager { private static final CacheManager CACHE_MGR = new CacheManager (); private CacheManager {} private Map<String,Object> cache; public static CacheManager  getInstance() { return CACHE_MGR; } public Object get(String key) { //code for getting the object from cache; }}
Singleton Classic – Java 5 ,[object Object],import static CacheManager.CACHE_MGR; public class CacheClient{ public static void main(String a[]){ CACHE_MGR.get(“testCache”); }} public  enum  CacheManager { CACHE_MGR; private Map<String,Object> cache; public Object get(String key) { //code for getting the object from cache; } public void put(String key,Object val) { //code for put the object in cache; } }
Case - Study
Notice Board detailed Domain
OnSubmit for send Message Static binding Singleton as Evil
Singleton public class MessageBroadCaster { private static final MessageBroadCaster INSTANCE = new MessageBroadCaster();  private MessageBroadCaster() {} public static MessageBroadCaster  getInstance () { return INSTANCE; } public void broadCast(final String[] messages) { final NoticeBoard board = NoticeBoard.getInstance(); int i = 0; final Message[] todayMessages = new Message[messages.length]; for(final String message : messages) { todayMessages[i++] = new Message(message); } board.show(todayMessages); } } public class NoticeBoard { private static final NoticeBoard noticeBoardInstance = new NoticeBoard(); private NoticeBoard   () {} public static NoticeBoard  getInstance () { return noticeBoardInstance;  } public void show(final Message[] messages){ for(Message msg : messages) msg.display();  } } Global Variables are many … Class operation
Change ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Patterns ,[object Object],[object Object],[object Object]
Abstraction
public class MessageBroadCaster{ private NoticeBoard noticeBoard; public MessageBroadCaster(NoticeBoard noticeBoard) { this.noticeBoard = noticeBoard; }  public void broadCast(final String[] messages) { int i = 0; final Message[] todayMessages = new Message[messages.length]; for(final String message : messages) { todayMessages[i++] = new Message(message); } noticeBoard.show(todayMessages); } } Abstraction class Simple implements NoticeBoard{ public void show(Message[] messages) { System.out.println(&quot;Simple Display ...&quot;); for(Message message: messages) { System.out.println(message.getContent()); } } } Instance operation, with good abstraction
Modules Sequence Simple Messenger Simple Message  Broadcaster Simple  Notice Board sends broadcast Simple LCD Messenger LCD Message  Broadcaster LCD  Notice Board sends broadcast LCD Electronic Messenger Electronic Message  Broadcaster Electronic  Notice Board sends broadcast Electronic This is not a
How to create Messenger for other modules like LCD,Electronic ..etc public class Messenger  { private MessageBroadCaster messageBroadCaster; public Messenger(MessageBroadCaster messageBroadCaster) { this.messageBroadCaster = messageBroadCaster; } public void send(String[] messages) { messageBroadCaster.broadCast(messages); } public static void main(String[] args) { NoticeBoard  simpleNoticeBoard  = new  Simple (); MessageBroadCaster simpleMessageBroadCaster = new MessageBroadCaster(simpleNoticeBoard) ; Messenger simpleMessenger = new Messenger(simpleMessageBroadCaster); simpleMessenger.send (new String[] { &quot;Welcome to Singleton&quot; , &quot;Town hall meet&quot; }); } } Simple Module Simple Notice Board flow or Module has been tested.
Solve the problem of creation ,[object Object],[object Object],[object Object]
Program Idiom public class NoticeBoardFactory { public NoticeBoard createNoticeBoard(String type)  { if(&quot;Simple&quot;.equals(type)) { return new Simple(); } else if(&quot;LCD&quot;.equals(type)){ return new LCD(); } else if(&quot;Electronic&quot;.equals(type)) { return new Electronic(); } else { return null; }}} public static void main(String[] args)  { NoticeBoardFactory boardFactory = new NoticeBoardFactory(); NoticeBoard simpleNoticeBoard =  boardFactory.createNoticeBoard(&quot;Simple&quot;);   MessageBroadCaster simpleMessageBroadCaster = new MessageBroadCaster(simpleNoticeBoard); Messenger simpleMessnger = new Messenger(simpleMessageBroadCaster); simpleMessnger.send(new String[] { &quot;Welcome to Singleton&quot; , &quot;Town hall meet&quot; }); } Note: We can avoid this if instead use Map, to look for specific type Creation of NoticeBoard becomes Abstract, using a program idiom
Module - IOC class MessengerModule{ private NoticeBoard noticeBoard; private MessageBroadCaster messageBroadCaster; private Messenger messenger; public MessengerModule(String type) { noticeBoard =  new NoticeBoardFactory().createNoticeBoard(type); messageBroadCaster =  new MessageBroadCaster(noticeBoard); messenger =  new Messenger(messageBroadCaster); } public void send(String[] messages) { messenger.send(messages); } } MessengerModule simpleModule = new MessengerModule(&quot;Simple&quot;); simpleModule.send(new String[] { &quot;Welcome to Singleton&quot; , &quot;Town hall meet&quot; }); MessengerModule lcdModule = new MessengerModule(&quot;LCD&quot;); lcdModule.send(new String[] { &quot;Welcome to Singleton&quot; , &quot;Town hall meet&quot; }); MessengerModule electronicModule = new MessengerModule(&quot;Electronic&quot;);   electronicModule.send(new String[] { &quot;Welcome to Singleton&quot; , &quot;Town hall meet&quot; });
Is flexible ? ,[object Object],[object Object],[object Object],[object Object],[object Object]
Flexible
Module – More Abstraction class MessengerModule{ private NoticeBoard noticeBoard; private MessageBroadCaster messageBroadCaster; private Messenger messenger; public MessengerModule(String type) { noticeBoard =  new NoticeBoardFactory().createNoticeBoard(type); messageBroadCaster =  new MessageBroadCasterFactory().createMessageBroadCaster(type,noticeBoard); messenger =  new MessengerFactory().createMessenger(type, messageBroadCaster); } public void send(String[] messages) { messenger.send(messages); }}
Module - Control ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
class MessengerModule{ private NoticeBoard noticeBoard; private MessageBroadCaster messageBroadCaster; private Messenger messenger; private static final Map<String, MessengerModule> MSG_MODULES = new HashMap<String, MessengerModule>(); public static MessengerModule getInstance(String type)  { MessengerModule messengerModule =  MSG_MODULES.get(type); if(messengerModule == null)  { messengerModule = new MessengerModule(type); MSG_MODULES.put(type,messengerModule); } return messengerModule; } private MessengerModule(String type) { noticeBoard = new NoticeBoardFactory().createNoticeBoard(type); messageBroadCaster = new MessageBroadCasterFactory().createMessageBroadCaster(type,noticeBoard); messenger = new MessengerFactory().createMessenger(type, messageBroadCaster);} public void send(String[] messages) { messenger.send(messages); } } Module – Singleton Classic
Module – Singleton – Java5 enum MessengerModule { Simple,LCD, Electronic; private NoticeBoard noticeBoard; private MessageBroadCaster messageBroadCaster; private Messenger messenger; public static MessengerModule getInstance(String type) { return valueOf(type); } private MessengerModule() { noticeBoard = new NoticeBoardFactory().createNoticeBoard(this.name()); messageBroadCaster = new MessageBroadCasterFactory().createMessageBroadCaster(this.name(),noticeBoard); messenger = new MessengerFactory().createMessenger(this.name(), messageBroadCaster); } public void send(String[] messages){ messenger.send(messages); }}
Test public class MessengerClient { public static void main(String[] args){ String[] messages = new String[] { &quot;Welcome to Singleton&quot; , &quot;Town hall meet&quot; }; MessengerModule.LCD.send(messages); MessengerModule.getInstance(&quot;Simple&quot;).send(messages); MessengerModule.getInstance(&quot;Electronic&quot;).send(messages); } } Output LCD Display ... Welcome to Singleton Town hall meet Simple Display ... Welcome to Singleton Town hall meet Electronic Display ... Welcome to Singleton Town hall meet
Summary ,[object Object],[object Object],[object Object],[object Object],[object Object]
Refferences ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Thanks ...

More Related Content

What's hot

Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency Gotchas
Alex Miller
 
Best Practices in Qt Quick/QML - Part IV
Best Practices in Qt Quick/QML - Part IVBest Practices in Qt Quick/QML - Part IV
Best Practices in Qt Quick/QML - Part IV
ICS
 
Windows Azure Storage
Windows Azure StorageWindows Azure Storage
Windows Azure Storage
goodfriday
 
The Ring programming language version 1.3 book - Part 7 of 88
The Ring programming language version 1.3 book - Part 7 of 88The Ring programming language version 1.3 book - Part 7 of 88
The Ring programming language version 1.3 book - Part 7 of 88
Mahmoud Samir Fayed
 
Actor Concurrency
Actor ConcurrencyActor Concurrency
Actor Concurrency
Alex Miller
 
Best Practices in Qt Quick/QML - Part II
Best Practices in Qt Quick/QML - Part IIBest Practices in Qt Quick/QML - Part II
Best Practices in Qt Quick/QML - Part II
ICS
 
Lecture 3: Storage and Variables
Lecture 3: Storage and VariablesLecture 3: Storage and Variables
Lecture 3: Storage and Variables
Eelco Visser
 
Voce Tem Orgulho Do Seu Codigo
Voce Tem Orgulho Do Seu CodigoVoce Tem Orgulho Do Seu Codigo
Voce Tem Orgulho Do Seu Codigo
Victor Hugo Germano
 
Id32
Id32Id32
Id32
sandhish
 
Compact and safely: static DSL on Kotlin
Compact and safely: static DSL on KotlinCompact and safely: static DSL on Kotlin
Compact and safely: static DSL on Kotlin
Dmitry Pranchuk
 
Integrazione QML / C++
Integrazione QML / C++Integrazione QML / C++
Integrazione QML / C++
Paolo Sereno
 
Di web tech mail (no subject)
Di web tech mail   (no subject)Di web tech mail   (no subject)
Di web tech mail (no subject)
shubhamvcs
 
Fighting null with memes
Fighting null with memesFighting null with memes
Fighting null with memes
Jarek Ratajski
 
Laporan multi client
Laporan multi clientLaporan multi client
Laporan multi client
ichsanbarokah
 
Design pattern - part 3
Design pattern - part 3Design pattern - part 3
Design pattern - part 3
Jieyi Wu
 
Design pattern part 2 - structural pattern
Design pattern part 2 - structural patternDesign pattern part 2 - structural pattern
Design pattern part 2 - structural pattern
Jieyi Wu
 
Android
AndroidAndroid
Android
ZÅhid IslÅm
 
MongoDB Live Hacking
MongoDB Live HackingMongoDB Live Hacking
MongoDB Live Hacking
Tobias Trelle
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency Gotchas
Alex Miller
 

What's hot (19)

Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency Gotchas
 
Best Practices in Qt Quick/QML - Part IV
Best Practices in Qt Quick/QML - Part IVBest Practices in Qt Quick/QML - Part IV
Best Practices in Qt Quick/QML - Part IV
 
Windows Azure Storage
Windows Azure StorageWindows Azure Storage
Windows Azure Storage
 
The Ring programming language version 1.3 book - Part 7 of 88
The Ring programming language version 1.3 book - Part 7 of 88The Ring programming language version 1.3 book - Part 7 of 88
The Ring programming language version 1.3 book - Part 7 of 88
 
Actor Concurrency
Actor ConcurrencyActor Concurrency
Actor Concurrency
 
Best Practices in Qt Quick/QML - Part II
Best Practices in Qt Quick/QML - Part IIBest Practices in Qt Quick/QML - Part II
Best Practices in Qt Quick/QML - Part II
 
Lecture 3: Storage and Variables
Lecture 3: Storage and VariablesLecture 3: Storage and Variables
Lecture 3: Storage and Variables
 
Voce Tem Orgulho Do Seu Codigo
Voce Tem Orgulho Do Seu CodigoVoce Tem Orgulho Do Seu Codigo
Voce Tem Orgulho Do Seu Codigo
 
Id32
Id32Id32
Id32
 
Compact and safely: static DSL on Kotlin
Compact and safely: static DSL on KotlinCompact and safely: static DSL on Kotlin
Compact and safely: static DSL on Kotlin
 
Integrazione QML / C++
Integrazione QML / C++Integrazione QML / C++
Integrazione QML / C++
 
Di web tech mail (no subject)
Di web tech mail   (no subject)Di web tech mail   (no subject)
Di web tech mail (no subject)
 
Fighting null with memes
Fighting null with memesFighting null with memes
Fighting null with memes
 
Laporan multi client
Laporan multi clientLaporan multi client
Laporan multi client
 
Design pattern - part 3
Design pattern - part 3Design pattern - part 3
Design pattern - part 3
 
Design pattern part 2 - structural pattern
Design pattern part 2 - structural patternDesign pattern part 2 - structural pattern
Design pattern part 2 - structural pattern
 
Android
AndroidAndroid
Android
 
MongoDB Live Hacking
MongoDB Live HackingMongoDB Live Hacking
MongoDB Live Hacking
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency Gotchas
 

Viewers also liked

Real World Lessons in jQuery Mobile
Real World Lessons in jQuery MobileReal World Lessons in jQuery Mobile
Real World Lessons in jQuery Mobile
Kai Koenig
 
Jakarta Airport transfer rent car
Jakarta Airport transfer rent carJakarta Airport transfer rent car
Jakarta Airport transfer rent car
Hans Ganteng
 
Svn Difference Investment Presentation
Svn Difference Investment PresentationSvn Difference Investment Presentation
Svn Difference Investment Presentation
gregsby
 
Apps vs. Sites vs. Content - a vendor-agnostic view on building stuff for the...
Apps vs. Sites vs. Content - a vendor-agnostic view on building stuff for the...Apps vs. Sites vs. Content - a vendor-agnostic view on building stuff for the...
Apps vs. Sites vs. Content - a vendor-agnostic view on building stuff for the...
Kai Koenig
 
ratio analysis
ratio analysisratio analysis
ratio analysis
goyal_p123
 
Technology Powerpoint
Technology PowerpointTechnology Powerpoint
Technology Powerpoint
BritaTheewis
 
Rugged Terminals Collection
Rugged Terminals CollectionRugged Terminals Collection
Rugged Terminals Collection
guest4c4c5a3
 
Introducció curs
Introducció cursIntroducció curs
Introducció curs
Natalia Hernández
 

Viewers also liked (9)

Real World Lessons in jQuery Mobile
Real World Lessons in jQuery MobileReal World Lessons in jQuery Mobile
Real World Lessons in jQuery Mobile
 
Jakarta Airport transfer rent car
Jakarta Airport transfer rent carJakarta Airport transfer rent car
Jakarta Airport transfer rent car
 
Presentasi DBS
Presentasi DBSPresentasi DBS
Presentasi DBS
 
Svn Difference Investment Presentation
Svn Difference Investment PresentationSvn Difference Investment Presentation
Svn Difference Investment Presentation
 
Apps vs. Sites vs. Content - a vendor-agnostic view on building stuff for the...
Apps vs. Sites vs. Content - a vendor-agnostic view on building stuff for the...Apps vs. Sites vs. Content - a vendor-agnostic view on building stuff for the...
Apps vs. Sites vs. Content - a vendor-agnostic view on building stuff for the...
 
ratio analysis
ratio analysisratio analysis
ratio analysis
 
Technology Powerpoint
Technology PowerpointTechnology Powerpoint
Technology Powerpoint
 
Rugged Terminals Collection
Rugged Terminals CollectionRugged Terminals Collection
Rugged Terminals Collection
 
Introducció curs
Introducció cursIntroducció curs
Introducció curs
 

Similar to Singleton

Contoh bahan latihan programan mobile java
Contoh bahan latihan programan mobile javaContoh bahan latihan programan mobile java
Contoh bahan latihan programan mobile java
Jurnal IT
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
MuhammadTalha436
 
Testing in android
Testing in androidTesting in android
Testing in android
jtrindade
 
Refactoring and code smells
Refactoring and code smellsRefactoring and code smells
Refactoring and code smells
Paul Nguyen
 
TechTalk - Dotnet
TechTalk - DotnetTechTalk - Dotnet
TechTalk - Dotnet
heinrich.wendel
 
Design patterns
Design patternsDesign patterns
Design patterns
Ba Tran
 
Java agents are watching your ByteCode
Java agents are watching your ByteCodeJava agents are watching your ByteCode
Java agents are watching your ByteCode
Roman Tsypuk
 
How to Start Test-Driven Development in Legacy Code
How to Start Test-Driven Development in Legacy CodeHow to Start Test-Driven Development in Legacy Code
How to Start Test-Driven Development in Legacy Code
Daniel Wellman
 
Kotlin / Android Update
Kotlin / Android UpdateKotlin / Android Update
Kotlin / Android Update
Garth Gilmour
 
softshake 2014 - Java EE
softshake 2014 - Java EEsoftshake 2014 - Java EE
softshake 2014 - Java EE
Alexis Hassler
 
Java doc Pr ITM2
Java doc Pr ITM2Java doc Pr ITM2
Java doc Pr ITM2
Aram Mohammed
 
Java adv
Java advJava adv
The State of JavaScript (2015)
The State of JavaScript (2015)The State of JavaScript (2015)
The State of JavaScript (2015)
Domenic Denicola
 
Google Web Toolkits
Google Web ToolkitsGoogle Web Toolkits
Google Web Toolkits
Yiguang Hu
 
Speed up your Web applications with HTML5 WebSockets
Speed up your Web applications with HTML5 WebSocketsSpeed up your Web applications with HTML5 WebSockets
Speed up your Web applications with HTML5 WebSockets
Yakov Fain
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
Intro C# Book
 
JEDI Slides-Intro2-Chapter20-GUI Event Handling.pdf
JEDI Slides-Intro2-Chapter20-GUI Event Handling.pdfJEDI Slides-Intro2-Chapter20-GUI Event Handling.pdf
JEDI Slides-Intro2-Chapter20-GUI Event Handling.pdf
MarlouFelixIIICunana
 
Lies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerLies Told By The Kotlin Compiler
Lies Told By The Kotlin Compiler
Garth Gilmour
 
The Ring programming language version 1.8 book - Part 16 of 202
The Ring programming language version 1.8 book - Part 16 of 202The Ring programming language version 1.8 book - Part 16 of 202
The Ring programming language version 1.8 book - Part 16 of 202
Mahmoud Samir Fayed
 
Gwt and Xtend
Gwt and XtendGwt and Xtend
Gwt and Xtend
Sven Efftinge
 

Similar to Singleton (20)

Contoh bahan latihan programan mobile java
Contoh bahan latihan programan mobile javaContoh bahan latihan programan mobile java
Contoh bahan latihan programan mobile java
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
Testing in android
Testing in androidTesting in android
Testing in android
 
Refactoring and code smells
Refactoring and code smellsRefactoring and code smells
Refactoring and code smells
 
TechTalk - Dotnet
TechTalk - DotnetTechTalk - Dotnet
TechTalk - Dotnet
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Java agents are watching your ByteCode
Java agents are watching your ByteCodeJava agents are watching your ByteCode
Java agents are watching your ByteCode
 
How to Start Test-Driven Development in Legacy Code
How to Start Test-Driven Development in Legacy CodeHow to Start Test-Driven Development in Legacy Code
How to Start Test-Driven Development in Legacy Code
 
Kotlin / Android Update
Kotlin / Android UpdateKotlin / Android Update
Kotlin / Android Update
 
softshake 2014 - Java EE
softshake 2014 - Java EEsoftshake 2014 - Java EE
softshake 2014 - Java EE
 
Java doc Pr ITM2
Java doc Pr ITM2Java doc Pr ITM2
Java doc Pr ITM2
 
Java adv
Java advJava adv
Java adv
 
The State of JavaScript (2015)
The State of JavaScript (2015)The State of JavaScript (2015)
The State of JavaScript (2015)
 
Google Web Toolkits
Google Web ToolkitsGoogle Web Toolkits
Google Web Toolkits
 
Speed up your Web applications with HTML5 WebSockets
Speed up your Web applications with HTML5 WebSocketsSpeed up your Web applications with HTML5 WebSockets
Speed up your Web applications with HTML5 WebSockets
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
 
JEDI Slides-Intro2-Chapter20-GUI Event Handling.pdf
JEDI Slides-Intro2-Chapter20-GUI Event Handling.pdfJEDI Slides-Intro2-Chapter20-GUI Event Handling.pdf
JEDI Slides-Intro2-Chapter20-GUI Event Handling.pdf
 
Lies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerLies Told By The Kotlin Compiler
Lies Told By The Kotlin Compiler
 
The Ring programming language version 1.8 book - Part 16 of 202
The Ring programming language version 1.8 book - Part 16 of 202The Ring programming language version 1.8 book - Part 16 of 202
The Ring programming language version 1.8 book - Part 16 of 202
 
Gwt and Xtend
Gwt and XtendGwt and Xtend
Gwt and Xtend
 

Singleton

  • 2.
  • 3.
  • 4.
  • 5.
  • 8. OnSubmit for send Message Static binding Singleton as Evil
  • 9. Singleton public class MessageBroadCaster { private static final MessageBroadCaster INSTANCE = new MessageBroadCaster(); private MessageBroadCaster() {} public static MessageBroadCaster getInstance () { return INSTANCE; } public void broadCast(final String[] messages) { final NoticeBoard board = NoticeBoard.getInstance(); int i = 0; final Message[] todayMessages = new Message[messages.length]; for(final String message : messages) { todayMessages[i++] = new Message(message); } board.show(todayMessages); } } public class NoticeBoard { private static final NoticeBoard noticeBoardInstance = new NoticeBoard(); private NoticeBoard () {} public static NoticeBoard getInstance () { return noticeBoardInstance; } public void show(final Message[] messages){ for(Message msg : messages) msg.display(); } } Global Variables are many … Class operation
  • 10.
  • 11.
  • 13. public class MessageBroadCaster{ private NoticeBoard noticeBoard; public MessageBroadCaster(NoticeBoard noticeBoard) { this.noticeBoard = noticeBoard; } public void broadCast(final String[] messages) { int i = 0; final Message[] todayMessages = new Message[messages.length]; for(final String message : messages) { todayMessages[i++] = new Message(message); } noticeBoard.show(todayMessages); } } Abstraction class Simple implements NoticeBoard{ public void show(Message[] messages) { System.out.println(&quot;Simple Display ...&quot;); for(Message message: messages) { System.out.println(message.getContent()); } } } Instance operation, with good abstraction
  • 14. Modules Sequence Simple Messenger Simple Message Broadcaster Simple Notice Board sends broadcast Simple LCD Messenger LCD Message Broadcaster LCD Notice Board sends broadcast LCD Electronic Messenger Electronic Message Broadcaster Electronic Notice Board sends broadcast Electronic This is not a
  • 15. How to create Messenger for other modules like LCD,Electronic ..etc public class Messenger { private MessageBroadCaster messageBroadCaster; public Messenger(MessageBroadCaster messageBroadCaster) { this.messageBroadCaster = messageBroadCaster; } public void send(String[] messages) { messageBroadCaster.broadCast(messages); } public static void main(String[] args) { NoticeBoard simpleNoticeBoard = new Simple (); MessageBroadCaster simpleMessageBroadCaster = new MessageBroadCaster(simpleNoticeBoard) ; Messenger simpleMessenger = new Messenger(simpleMessageBroadCaster); simpleMessenger.send (new String[] { &quot;Welcome to Singleton&quot; , &quot;Town hall meet&quot; }); } } Simple Module Simple Notice Board flow or Module has been tested.
  • 16.
  • 17. Program Idiom public class NoticeBoardFactory { public NoticeBoard createNoticeBoard(String type) { if(&quot;Simple&quot;.equals(type)) { return new Simple(); } else if(&quot;LCD&quot;.equals(type)){ return new LCD(); } else if(&quot;Electronic&quot;.equals(type)) { return new Electronic(); } else { return null; }}} public static void main(String[] args) { NoticeBoardFactory boardFactory = new NoticeBoardFactory(); NoticeBoard simpleNoticeBoard = boardFactory.createNoticeBoard(&quot;Simple&quot;); MessageBroadCaster simpleMessageBroadCaster = new MessageBroadCaster(simpleNoticeBoard); Messenger simpleMessnger = new Messenger(simpleMessageBroadCaster); simpleMessnger.send(new String[] { &quot;Welcome to Singleton&quot; , &quot;Town hall meet&quot; }); } Note: We can avoid this if instead use Map, to look for specific type Creation of NoticeBoard becomes Abstract, using a program idiom
  • 18. Module - IOC class MessengerModule{ private NoticeBoard noticeBoard; private MessageBroadCaster messageBroadCaster; private Messenger messenger; public MessengerModule(String type) { noticeBoard = new NoticeBoardFactory().createNoticeBoard(type); messageBroadCaster = new MessageBroadCaster(noticeBoard); messenger = new Messenger(messageBroadCaster); } public void send(String[] messages) { messenger.send(messages); } } MessengerModule simpleModule = new MessengerModule(&quot;Simple&quot;); simpleModule.send(new String[] { &quot;Welcome to Singleton&quot; , &quot;Town hall meet&quot; }); MessengerModule lcdModule = new MessengerModule(&quot;LCD&quot;); lcdModule.send(new String[] { &quot;Welcome to Singleton&quot; , &quot;Town hall meet&quot; }); MessengerModule electronicModule = new MessengerModule(&quot;Electronic&quot;); electronicModule.send(new String[] { &quot;Welcome to Singleton&quot; , &quot;Town hall meet&quot; });
  • 19.
  • 21. Module – More Abstraction class MessengerModule{ private NoticeBoard noticeBoard; private MessageBroadCaster messageBroadCaster; private Messenger messenger; public MessengerModule(String type) { noticeBoard = new NoticeBoardFactory().createNoticeBoard(type); messageBroadCaster = new MessageBroadCasterFactory().createMessageBroadCaster(type,noticeBoard); messenger = new MessengerFactory().createMessenger(type, messageBroadCaster); } public void send(String[] messages) { messenger.send(messages); }}
  • 22.
  • 23. class MessengerModule{ private NoticeBoard noticeBoard; private MessageBroadCaster messageBroadCaster; private Messenger messenger; private static final Map<String, MessengerModule> MSG_MODULES = new HashMap<String, MessengerModule>(); public static MessengerModule getInstance(String type) { MessengerModule messengerModule = MSG_MODULES.get(type); if(messengerModule == null) { messengerModule = new MessengerModule(type); MSG_MODULES.put(type,messengerModule); } return messengerModule; } private MessengerModule(String type) { noticeBoard = new NoticeBoardFactory().createNoticeBoard(type); messageBroadCaster = new MessageBroadCasterFactory().createMessageBroadCaster(type,noticeBoard); messenger = new MessengerFactory().createMessenger(type, messageBroadCaster);} public void send(String[] messages) { messenger.send(messages); } } Module – Singleton Classic
  • 24. Module – Singleton – Java5 enum MessengerModule { Simple,LCD, Electronic; private NoticeBoard noticeBoard; private MessageBroadCaster messageBroadCaster; private Messenger messenger; public static MessengerModule getInstance(String type) { return valueOf(type); } private MessengerModule() { noticeBoard = new NoticeBoardFactory().createNoticeBoard(this.name()); messageBroadCaster = new MessageBroadCasterFactory().createMessageBroadCaster(this.name(),noticeBoard); messenger = new MessengerFactory().createMessenger(this.name(), messageBroadCaster); } public void send(String[] messages){ messenger.send(messages); }}
  • 25. Test public class MessengerClient { public static void main(String[] args){ String[] messages = new String[] { &quot;Welcome to Singleton&quot; , &quot;Town hall meet&quot; }; MessengerModule.LCD.send(messages); MessengerModule.getInstance(&quot;Simple&quot;).send(messages); MessengerModule.getInstance(&quot;Electronic&quot;).send(messages); } } Output LCD Display ... Welcome to Singleton Town hall meet Simple Display ... Welcome to Singleton Town hall meet Electronic Display ... Welcome to Singleton Town hall meet
  • 26.
  • 27.