using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace ConsoleApplication2{class Program{static void Main(string[] args){
var someClass = new SomeClass(Logger.GetLogger());}
}
public class SomeClass{public SomeClass(ILogger MyLogger){
}}
public class Logger : ILogger{private static Logger _logger;private Logger() { }
public static Logger GetLogger(){if (_logger==null){_logger = new Logger();}
return _logger;}
public void Log(){
}
}
public interface ILogger{void Log();}}
public class SupportedVersionSingelton {
private static ICalculator instance = null;
private SupportedVersionSingelton(){
}
public static ICalculator getInstance(){if(instance == null){instance = new SupportedVersionSingelton();}
return instance;}
@Overridepublic int getPrice() {// calculate price logic herereturn 0;}}
使用getPrice:
public class Advisor {
public boolean isGoodDeal(){
boolean isGoodDeal = false;ICalculator supportedVersion = SupportedVersionSingelton.getInstance();int price = supportedVersion.getPrice();
// logic to determine if price is a good deal.if(price < 5){isGoodDeal = true;}
return isGoodDeal;}}
In case you would like to test the method isGoodPrice , with mocking the getPrice() method you could do it by:Make your singleton implement an interface and inject it.
public interface ICalculator {int getPrice();}
最终Singleton实现:
public class SupportedVersionSingelton implements ICalculator {
private static ICalculator instance = null;
private SupportedVersionSingelton(){
}
public static ICalculator getInstance(){if(instance == null){instance = new SupportedVersionSingelton();}
return instance;}
@Overridepublic int getPrice() {return 0;}
// for testing purposepublic static void setInstance(ICalculator mockObject){if(instance != null ){instance = mockObject;}
测试类:
public class TestCalculation {
class SupportedVersionDouble implements ICalculator{@Overridepublic int getPrice() {return 1;}}@Beforepublic void setUp() throws Exception {ICalculator supportedVersionDouble = new SupportedVersionDouble();SupportedVersionSingelton.setInstance(supportedVersionDouble);
}
@Testpublic void test() {Advisor advidor = new Advisor();boolean isGoodDeal = advidor.isGoodDeal();Assert.assertEquals(isGoodDeal, true);
}
}
package com.demo.core;
public class NestedStaticClassTest{public static void main(String[] args){OuterClass.NestedStaticClass obj1 = new OuterClass.NestedStaticClass();OuterClass.NestedStaticClass obj2 = new OuterClass.NestedStaticClass();
if(obj1 == obj2){System.out.println("Both nested static objects are equal....");}else{System.out.println("NOT EQUAL......");}
System.out.println(OuterClass.NestedStaticClass.d);
obj1.setD(5);
System.out.println(OuterClass.NestedStaticClass.d);
System.out.println(obj1.sum());}}
class OuterClass{int a =1;static int b = 2;
static class NestedStaticClass extends OneClass implements Sample{int c = 3;static int d = 4;
public NestedStaticClass(){}
//Parameterized constructorpublic NestedStaticClass(int z){c = z;}
public int sum(){int sum = 0;sum = b + c + d + getE();return sum;}
public static int staticSum(){int sum = 0;sum = b + d;return sum;}
public int getC(){return c;}public void setC(int c){this.c = c;}public static int getD(){return d;}public static void setD(int d){NestedStaticClass.d = d;}}}
interface Sample{
}
class OneClass{int e = 10;static int f = 11;
public int getE(){return e;}public void setE(int e){this.e = e;}public static int getF(){return f;}public static void setF(int f){OneClass.f = f;}
}
public sealed class Connection{static readonly Connection _instance = new Connection();
private Connection(){}
public static Connection Instance{get{return _instance;}}}
两者都使用静态成员,它们是成员的单个副本,但Singleton将被引用的成员包装在一个真正实例化的“对象”周围,该对象的地址除了其静态成员之外还存在。该对象本身具有属性,其中in可以被传递和引用,从而增加价值。静态类只是一种类型,所以除了指向其静态成员之外,它不存在。这个概念在某种程度上巩固了Singleton vs Static Class的目的,超越了继承和其他问题。
public class Any {
private static Any instance = new Any();
private Singleton() {System.out.println("creating");}}
单例模式只存在一个实例:
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton() {System.out.println("creating");if (instance != null) {throw new RuntimeException("Imposible create a new instance ");}}}