Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can u please explain this code for me and why did he used this specific patterns I mean what's the aim of using every single

can u please explain this code for me and why did he used this specific patterns I mean what's the aim of using every single on of them please need a full explanation cuz I am confused

Below is program which implements Factory Design Pattern,Singleton,Proxy and Observer Design patterns. The program's objective is to make changes to a file by implementing the design patterns. StringManagerFactory interface is the Factory for StringManager class. StringManager class is of Singletonpattern as only one instance can be initiated from it. Tracker class is an Observer and any change done from Stringmanager class will get notified to Tracker objects via the notifytrackers() method triggered after changestring() method gets triggered. ProxyActivity is the Proxy implementation of ConcreteActivities class.

import java.io.BufferedWriter;

import java.io.FileWriter;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

public class MainActivity {

public static void main(String[] args) {

StringManagerFactory obj = StringManager.getObj();

Tracker t1 = new Tracker();

obj.register(t1);

obj.changeString();

}

}

//Factory Design pattern implementation

interface StringManagerFactory{

public void changeString();

public void register(Tracker t);

}

//Singleton Design pattern, Proxy Design pattern & Observer Design pattern implementation

class StringManager implements StringManagerFactory {

private static StringManager obj;

ProxyActivity ref;

List observerlist = new ArrayList();

public static StringManager getObj(){

if(obj==null){

obj = new StringManager();

}

return obj;

}

public void register(Tracker t){

observerlist.add(t);

}

public void unregister(Tracker t){

observerlist.remove(t);

}

public void notifytrackers(){

for(int i=0;i

observerlist.get(i).update();

}

}

public void changeString(){

ref = new ProxyActivity();

ref.doActivity();

notifytrackers();

}

}

class Tracker{

public void update(){

System.out.println("Value changed");

}

}

interface IReference{

public void doActivity();

}

class ProxyActivity implements IReference{

ConcreteActivities ca;

public void doActivity(){

if(ca ==null){

ca = new ConcreteActivities();

}

ca.doActivity();

}

}

class ConcreteActivities implements IReference{

public void doActivity(){

BufferedWriter bw = null;

FileWriter fw = null;

String FILENAME = "C:\\thefile.txt";

try {

String content = "This is the content to write into file ";

fw = new FileWriter(FILENAME);

bw = new BufferedWriter(fw);

bw.write(content);

System.out.println("Done");

} catch (IOException e) {

e.printStackTrace();

}

try {

bw.flush();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beyond Big Data Using Social MDM To Drive Deep Customer Insight

Authors: Martin Oberhofer, Eberhard Hechler

1st Edition

0133509796, 9780133509793

More Books

Students also viewed these Databases questions