Home > Java > javaTutorial > Java Notes: How to use Generator

Java Notes: How to use Generator

无忌哥哥
Release: 2018-07-20 11:01:57
Original
2368 people have browsed it

1. Preface

Different from the Iterator described in the previous blog post, this article describes a Generator. There is a big difference between the two. Iterators are used to traverse elements in a container. The Java standard library has the Iterator interface and its implementation; but generators are used to create objects. The Java standard library does not provide the Generator interface and its implementation.
[Note: Pay attention to distinguish the generator Generator here from the generator mode (that is, the builder mode) in the design mode. The two are different]

2. Introduction

  1. Generator is a class specifically used to create objects

  2. It is actually an application of the factory method pattern and a general A type applied to an interface

  3. When using a generator to create a new object, the object can be created without any parameters—>This is also the difference from the factory method pattern. Factory methods generally Requires parameters

  4. Generally, the generator only defines one method, which is specifically used to generate new objects

3. Generator interface

The Java standard library does not include the Generator interface, so the generator needs to be defined by itself.

[Generator]

/**
 - 生成器接口
 - @author johnnie
 - @param <T>
 */
public interface Generator<T> {

    /**
     - 用以产生新对象
     - @return
     */
    public T next();

}
Copy after login

4. Use of generator

[Example code]

/**
 - Generator 的实现类
 - @author johnnie
 *
 */
public class PersonGenerator implements Generator<Person> {

    private Class[] types = new Class[]{Person.class};

    public PersonGenerator() {  }

    @Override
    public Person next() {
        try {
            // 利用反射生成 Person 对象
            return (Person) types[0].newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static void main(String[] args) {
        PersonGenerator generator = new PersonGenerator();
        Person person = generator.next();
        person.setId(0);
        person.setName("johnnie");
        person.setSex("Man");
        System.out.println(person);
    }

}
Copy after login

[Output]

Person [id=0, name=johnnie, sex=Man]
Copy after login

The above is the detailed content of Java Notes: How to use Generator. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template