Demo 3: Naked Objects

Taustaa

Naked Objects -demo

Hae demo (versio 1.2.2) osoitteesta http://www.nakedobjects.org/downloads.html, pura se ja kokeile sitä.

Naked Objects -ohjelmointi

Koodiesimerkit otettu Naked Objects -kirjasta, joka löytyy myös osoitteesta http://www.nakedobjects.org/book.html#book-ad.

Olion perusrakenne

import org.nakedobjects.object.AbstractNakedObject;
import org.nakedobjects.object.Title;
import org.nakedobjects.object.control.About;
import org.nakedobjects.object.value.TextString;

    public Title title() {...
}

public class Customer extends AbstractNakedObject {
    public static final long serialVersionUID = 1L;
    private final TextString lastName;
    private final TextString firstName;

    // "Rakenteellinen" rakennin
    public Customer() {
        firstName = new TextString();
        lastName = new TextString();
    }

    // "Toiminnallinen" rakennin
    public void created() {
        // ...
    }

    public About about() {
        // kenttien ja metodien näkyvyys ja
	// aktiivisuus
    }
 
    public TextString getLastName() {
        return lastName;
    }

    public TextString getFirstName() {
        return firstName;
    }

    // Yksinkertaisen otsikon rakentimet
    // (englannille tarpeettomat usein)
    public static String singularName() {
        return "Asiakas";
    }
    public static String pluralName() {
        return "Asiakkaat";
    }
    public Title title() {
        // erikoisemman otsikon rakennin
    }
}

Yksi yhteen -suhde

public class Booking extends AbstractNakedObject {
    private Location dropOff;
          
    public Location getDropOff() {
        resolve(dropOff);
        return dropOff;
    }   

    public void setDropOff(Location newDropOff) {
        dropOff = newDropOff;
        objectChanged();
    }
}

Yksi moneen -suhde

public class Customer extends AbstractNakedObject {
    private final InternalCollection locations;
    private final InternalCollection phoneNumbers;

    public Customer() {
        locations = new InternalCollection(Location.class, this);
        phoneNumbers = new InternalCollection(Telephone.class, this);
    }

    public final InternalCollection getLocations() {
        return locations;
    }

    public final InternalCollection getPhoneNumbers() {
        return phoneNumbers;
    }
}

Kaksisuuntainen suhde

public class Customer extends AbstractNakedObject {
    private final InternalCollection bookings;

    ...

    public Customer() {
        bookings = new InternalCollection(Booking.class, this);
    }  

    public final InternalCollection getBookings() {
        return bookings;
    }

    public void associateBookings(Booking booking) { // tai addBookings
        getBookings().add(booking);
        booking.setCustomer(this);
    }

    public void dissociateBookings(Booking booking) { // tai removeBookings
        getBookings().remove(booking);
        booking.setCustomer(null);
    }
}

public class Booking extends AbstractNakedObject {
    private Customer customer;

    ...

    public void associateCustomer(Customer customer) {
        customer.associateBookings(this);
    }

    public void dissociateCustomer(Customer customer) {
        customer.dissociateBookings(this);
    }

    public Customer getCustomer() {
        resolve(customer);
	return customer;
    }

    public void setCustomer(Customer newCustomer) {
        customer = newCustomer;
        objectChanged();
    }
}

Johdetut kentät

    public Date deriveDue() {
        Date due = new Date(ordered);
        due.add(14,0,0);
        return due;
    }

Kenttien järjestys

    public static String fieldOrder() {
        return "reference, status, customer, date, time, pick up, drop off, payment method";
    }

Olion metodit

    public Location actionNewLocation() {
        Location loc = (Location) createInstance(Location.class);
        loc.setCity(this);
        return loc;
    }

    // ...

    public void actionConfirm() {
        getStatus().setValue("Confirmed");

        getCustomer().associateLocations(getPickUp());
        getCustomer().associateLocations(getDropOff());
        if (getCustomer().getPreferredPaymentMethod() == null) {
            getCustomer().setPreferredPaymentMethod(getPaymentMethod());
        }
    }

Olioselain

package ecs.delivery;

import org.nakedobjects.Exploration;
import org.nakedobjects.object.NakedClassList;

public class EcsExploration extends Exploration {
    public void classSet(NakedClassList classes) {
        classes.addClass(Booking.class);
        classes.addClass(City.class);
        classes.addClass(Location.class);
        classes.addClass(CreditCard.class);
        classes.addClass(Customer.class);
        classes.addClass(Telephone.class);
    }

    public void initObjects() { 
        /* valmiiden olioiden alustus */
        Customer newCustomer;

        newCustomer = (Customer) createInstance(Customer.class);
        newCustomer.getFirstName().setValue("Richard");
        newCustomer.getLastName().setValue("Pawson");

        newCustomer = (Customer) createInstance(Customer.class);
        newCustomer.getFirstName().setValue("Robert");
        newCustomer.getLastName().setValue("Matthews");
    }

    public static void main(String[] args) {
        new EcsExploration();
    }
 }

Tehtävä

Toteuta Ohjelmointi 2 -kurssilta tuttu Kerho-ohjelma Naked Objects -teknologialla (kerhon jäsenkortisto, jossa on tiedot jäsenistä ja heidän harrastuksistaan). Helpotetaan sen verran, että jäsenen tiedoista tarvitaan vain nimi ja osoite, harrastuksen tiedoista vain sen nimi. Älä pyri tekemään samanlaista toteutusta kuin Ohjelmointi 2 -kurssilla, vaan tee Naked Objects -tyylinen toteutus.

Lataa NO-kirjasto osoitteesta http://www.nakedobjects.org/downloads.html, versio 1.2.2 käy. Halutessasi ota lähdekoodi mukaan. Liitä kirjasto projektiisi (nakedobjects.jar ja log4.jar).