Coder Social home page Coder Social logo

javabeans-guide's Introduction

JavaBeans Creation Guide

Creation of a simple JavaBean component. The purpose of this repository is educational.

Steps to follow

Create EventBeanValue class that inherits from EventObject.

This class will store the old and new values of the JavaBean.

public class EventBeanValue extends EventObject {
	private int oldValue, newValue;
}

Create BeanValueListener interface that extends from EventListener.

This interface must have a method forcing implementing classes to fill it with their response to the event.

public interface BeanValueListener extends EventListener {
	/**
	 * Method to be run when the event occurs
	 * @param ev Object that describes the change that has been made.
	 */
	public void awareOfChangeInValue(EventBeanValue ev);
}

Create Bean class

This class must follow this points:

  • Implement Serializable. This allows instances of this class to be saved at any time.
  • Implement default (empty) constructor and have get, set and is methods.
  • Have a List of BeanValueListeners. This list must have methods declared with synchronized modifier to add and remove a certain listener.
  • A setValue() method that does the following when a new value is set:
    • Create an EventBeanValue describing the change
    • Create a copy of the listeners' list in a synchronized block
    • Notify each element of the copy using the method they inherit from BeanValueListener
// Notifies the change event to all subscribers
EventBeanValue ev = new EventBeanValue(this, oldValue, newValue);
notifyChange(ev);

/**
 * Firstly, creates a copy of the list in order to prevent concurrency problems.
 * After that, notifies the value change to all subscribers
 */
private void notifyChange(EventBeanValue ev) {
	List<BeanValueListener> copy;
	synchronized (this) {
		copy = new ArrayList<>(listeners);
	}

	for (BeanValueListener listener : copy) {
		listener.awareOfChangeInValue(ev);
	}
}

javabeans-guide's People

Contributors

mgonzalezg9 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.