Question

Some coders suggest that the Singleton can be an “Anti-Pattern” – that is to say you...

Some coders suggest that the Singleton can be an “Anti-Pattern” – that is to say you can actually detract from software by using it. Evaluate the Singleton pattern and assess how it can detract from your software:

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

// Java program implementing Singleton class
// with getInstance() method
class Singleton
{
   // static variable single_instance of type Singleton
   private static Singleton single_instance = null;

   // variable of type String
   public String s;

   // private constructor restricted to this class itself
   private Singleton()
   {
       s = "Hello I am a string part of Singleton class";
   }

   // static method to create instance of Singleton class
   public static Singleton getInstance()
   {
       if (single_instance == null)
           single_instance = new Singleton();

       return single_instance;
   }
}

// Driver Class
class Main
{
   public static void main(String args[])
   {
       // instantiating Singleton class with variable x
       Singleton x = Singleton.getInstance();

       // instantiating Singleton class with variable y
       Singleton y = Singleton.getInstance();

       // instantiating Singleton class with variable z
       Singleton z = Singleton.getInstance();

       // changing variable of instance x
       x.s = (x.s).toUpperCase();

       System.out.println("String from x is " + x.s);
       System.out.println("String from y is " + y.s);
       System.out.println("String from z is " + z.s);
       System.out.println("\n");

       // changing variable of instance z
       z.s = (z.s).toLowerCase();

       System.out.println("String from x is " + x.s);
       System.out.println("String from y is " + y.s);
       System.out.println("String from z is " + z.s);
   }
}

Add a comment
Know the answer?
Add Answer to:
Some coders suggest that the Singleton can be an “Anti-Pattern” – that is to say you...
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
  • Software design [Singleton v.s. Static] You are producing a mobile game that allows both single player...

    Software design [Singleton v.s. Static] You are producing a mobile game that allows both single player gameplayer and networked multiplayer gameplay. Your team is debating how you should approach the NetworkController, which is a single point of control for the game's network connections and traffic. The language you are using is C#. Sandy, the system analyst, believes that a Singleton pattern will be needed here Terence, the product manager, believes a static class is sufficient Please analyze the advantages and...

  • Identify the most applicable design pattern for the given scenario. Assume you are writing a program...

    Identify the most applicable design pattern for the given scenario. Assume you are writing a program to respond to many different types of web requests. You determine that there are 5 steps for handling the request which need to be done in order: 1. Set up server memory 2. Process the URL 3. Handle web form parameters 4. Generate the content of the web page 5. Send the page back to the user. You quickly realize that Steps 1, 2,...

  • Can you suggest some important elements that must be present in a future model of care...

    Can you suggest some important elements that must be present in a future model of care delivery in the United States? 75-100 word.

  • Note that in some cases, you can choose to say that there is no change in...

    Note that in some cases, you can choose to say that there is no change in one (or even two) of these variables Number of pens BLI BLN 01 Number of books (i) The price of books has ii) The price of pens has (ii) The consumer's income has

  • (a) Has protection for proprietary software gone too far, as some critics suggest? (b) If not,...

    (a) Has protection for proprietary software gone too far, as some critics suggest? (b) If not, why? (c) If so, what are the implications for innovation and competition in the computer industry? (d) How can we achieve an appropriate balance between those who hold legal rights to proprietary information and ordinary users who wish to access, share, and communicate that information? Defend your answer. Please elaborate (beyond a yes or no answer) and provide your “theoretical” rationale in support of...

  • can you say for each reaction which is markovnikov or anti markovnikov. Hycheahan Reachion HO BH3...

    can you say for each reaction which is markovnikov or anti markovnikov. Hycheahan Reachion HO BH3 THE 2. thoz naon LOH Hydrobosancos-caudation Reachion 1- Os04 ? Na H509, Ho " KMnO4, HD) bo eis-hydroxy lahin Reachion ko 1. Hglosch, the 2. Nabha ton Beymereurahian demercurahiun Reachium 1NaBH or Li Alle 2. Hoes R on Reduction Reactice 1 Nabucor Coldu et Reduchicos Reachion- 2. 106 R-COR I LAIHY 2.100 - R^OH + R-OH Reduction Reachiun . ܝܙ. ܂ ܙܐ 0 A....

  • Strings have many convenient behaviors, one of which is that you can test whether a pattern...

    Strings have many convenient behaviors, one of which is that you can test whether a pattern occurs in another string, using the syntax pattern in original which produces a boolean result. We want you to implement this test from basic principles. Provide an implemenation of a function with calling signature isSubstring (pattern, original) that returns True or False, depending on the given parameters. Furthermore, you must not rely on any of the built-in behaviors of the string class, other than...

  • All the planets we know of rotate, some fast and some slow. Is there any limit...

    All the planets we know of rotate, some fast and some slow. Is there any limit on how fast they can rotate, or could it in principle be any speed? In this problem we will answer that question! Suppose (to keep it simple) that a particular planet is spherical and has a uniform mass density ρ. Consider a rock resting on the surface of that planet, at the equator. (a) Draw a free-body diagram of that rock, and write down...

  • 1.) Is the national crime rate really going down? Some sociologists say yes! They say that...

    1.) Is the national crime rate really going down? Some sociologists say yes! They say that the reason for the decline in crime rates in the 1980s and 1990s is demographics. It seems that the population is aging, and older people commit fewer crimes. According to the FBI and the Justice Department, 70% of all arrests are of males aged 15 to 34 years.† Suppose you are a sociologist in Rock Springs, Wyoming, and a random sample of police files...

  • Question 2 [Avec R] Say you can ask people to give a rating of a potential...

    Question 2 [Avec R] Say you can ask people to give a rating of a potential product. The population mean of this product's rating is μ, which is unknown. If you ask a single person. they will respond with a random variable that has a mean μ and a variance of 100. (a). Call n the number of people sampled and x =-Σ-1x, the sample mean of each individual's response. This will be your cstimate. What is the approximate distribution...

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