Effective Java——Item 1,Consider static factory methods instead of constructors

public static Boolean valueOf(boolean b){
        return b ? Boolean.TRUE : Boolean.FALSE;
    }


Advantages: 

1、静态工厂方法不像构造方法,他可以有自己的名字。 静态方法和静态工厂方法没什么不同,只是静态工厂方法使用的场景是产生实例,类的对象。

2、每次调用的时候,不必都生产新的对象

3、不像构造函数,静态工厂方法可以返回任何子类。

// Service provider framework sketch
   // Service interface
   public interface Service {
       ... // Service-specific methods go here
}
   // Service provider interface
   public interface Provider {
       Service newService();
}
   // Noninstantiable class for service registration and access
   public class Services {
       private Services() { }  // Prevents instantiation (Item 4)
       // Maps service names to services
       private static final Map<String, Provider> providers =
           new ConcurrentHashMap<String, Provider>();
       public static final String DEFAULT_PROVIDER_NAME = "<def>";
// Provider registration API
       public static void registerDefaultProvider(Provider p) {
           registerProvider(DEFAULT_PROVIDER_NAME, p);
}
public static void registerProvider(String name, Provider p){
           providers.put(name, p);
       }
       // Service access API
       public static Service newInstance() {
           return newInstance(DEFAULT_PROVIDER_NAME);
       }
       public static Service newInstance(String name) {
           Provider p = providers.get(name);
           if (p == null)
               throw new IllegalArgumentException(
                   "No provider registered with name: " + name);
           return p.newService();
       }
}

4、创建有参数类型的实例时,减少冗余度。

public static <K, V> HashMap<K, V> newInstance(){
        return new HashMap<K, V>();
    }

Disadvantages:

              1、静态工厂方法不能是private \ protected

    2、静态工厂方法和静态放大其实没有什大的不同。注意命名规范,让client一看就知道方法的用途。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Certainly! I can help you write a comprehensive Java program that includes all the common features of Object-Oriented Programming (OOP) and displays the results of variables using methods. Here's an example program: ```java public class OOPExample { // Define a class public static class Person { // Define instance variables private String name; private int age; // Define constructor public Person(String name, int age) { this.name = name; this.age = age; } // Define instance methods public void sayHello() { System.out.println("Hello, my name is " + name); } public void displayAge() { System.out.println("I am " + age + " years old"); } } // Define another class that inherits from Person public static class Student extends Person { private String major; public Student(String name, int age, String major) { super(name, age); this.major = major; } public void displayMajor() { System.out.println("I am majoring in " + major); } } // Main method public static void main(String[] args) { // Create an instance of Person and call methods Person person = new Person("John", 25); person.sayHello(); person.displayAge(); // Create an instance of Student and call methods Student student = new Student("Alice", 20, "Computer Science"); student.sayHello(); student.displayAge(); student.displayMajor(); } } ``` In this program, we define a `Person` class with instance variables `name` and `age`, as well as instance methods `sayHello()` and `displayAge()`. We also define a `Student` class that inherits from `Person` and adds an additional instance variable `major` and a method `displayMajor()`. In the `main` method, we create an instance of `Person` and call its methods to display the name and age. Then, we create an instance of `Student` and call its methods to display the name, age, and major. When you run this program, it will output the following: ``` Hello, my name is John I am 25 years old Hello, my name is Alice I am 20 years old I am majoring in Computer Science ``` This example program demonstrates the common features of OOP in Java, including classes, objects, instance variables, constructors, and instance methods. It also shows how to use methods to display the results of variables.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值