EffectiveJava study notes (b)

  Article: strengthening ability can not be instantiated by a private constructor

  java.lang.Math, java.util.Arrays no need to instantiate tools such as meaningless, but in the absence of an explicit constructor, the compiler will automatically construct a public constructor with no parameters, these tools class may be instantiated in the case of the unconscious, or inherited, and an attempt to make these kinds of restrictions to instantiate an abstract class is not feasible, because the subclass inherits the abstract class can still be instantiated, and it will misleading. The solution is to let this tool class contains a private constructor, so you can not guarantee the class is instantiated, but also can not be inherited, because the subclass can not call the constructor of the parent class.

  Article: give priority to reference resource dependency injection

  Many classes will depend on one or more underlying resources, the resource requirements of the underlying can be modified (multiple instances of the same class), meet this need is the simplest mode, when creating a new instance, the resource will be incoming class constructor (which is dependent injection), achieved as follows.

public class DependencyDemo {

    public static void main(String[] args) {

        new People(new Apple());
    }
}

class People {

    public People(Fruit fruit) {
        fruit.eat();
    }
}

interface Fruit {

    void eat();
}

class Apple implements Fruit {
    @Override
    public void eat() {
        System.out.println("eat apple");
    }
}

class Banana implements Fruit {
    @Override
    public void eat() {
        System.out.println("eat banana");
    }
}

  Do not use a tool or a singleton class implements a need to rely on the underlying resource class, because the behavior of resources will affect the behavior of the class, and do not directly create resources, dependency injection mode should be used. Resource dependency injection having immutable objects, multiple clients can share dependent objects, this dependency injection mode is also applicable to static plant, the builder, the builder. Dependency injection Another variant is passed to the constructor plant resource, the client can create any subtype.

  Article VI: avoid creating unnecessary objects

  The String s = new String ( "abc"), amended as String s = "abc"; the former is repeatedly executed each and every time a new object, which repeatedly executed each time with the same buffer character constants.

  Also provides for the construction and immutable class static factory method, static factory method should be preferred, to avoid creating unnecessary objects.

  Some object creation is expensive, it should be cached repeated use, such as the use String.matches method for matching a regular expression, which expression is positive instances created Pattern internally, and will be recovered after a single use, in order to improve performance, the regular expression compilation into a Pattern instance, are both initialized when creating class instances, cached, to achieve the purpose of reuse.

  Autoboxing will create unnecessary objects in the calculation only you do not need to empty sentenced operations should use Basic types rather than packaging.

  At the same time, not in any case have to avoid creating objects, create and recover some small objects is very inexpensive, maintenance-free objects in the pool, the pool maintenance target object should be very heavyweight, typically as database connection pool.

 

Guess you like

Origin www.cnblogs.com/youtang/p/11617919.html