Java中构建模式 : 构建模式 « 设计模式 « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » 设计模式 » 构建模式屏幕截图 
Java中构建模式

//[C] 2002 Sun Microsystems, Inc.---
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
public class RunBuilderPattern {
    private static Calendar dateCreator = Calendar.getInstance();
    
    public static void main(String [] arguments){
        Appointment appt = null;
        
        System.out.println("Example for the Builder pattern");
        System.out.println();
        System.out.println("This example demonstrates the use of the Builder");
        System.out.println("pattern to create Appointment objects for the PIM.");
        System.out.println();
        
        System.out.println("Creating a Scheduler for the example.");
        Scheduler pimScheduler = new Scheduler();
        
        System.out.println("Creating an AppointmentBuilder for the example.");
        System.out.println();
        AppointmentBuilder apptBuilder = new AppointmentBuilder();
        try{
            System.out.println("Creating a new Appointment with an AppointmentBuilder");
            appt = pimScheduler.createAppointment(
                apptBuilder, createDate(20669221230),
                null, "Trek convention"new LocationImpl("Fargo, ND"),
                createAttendees(4));
            System.out.println("Successfully created an Appointment.");
            System.out.println("Appointment information:");
            System.out.println(appt);
            System.out.println();
        }
        catch (InformationRequiredException exc){
            printExceptions(exc);
        }
        
        System.out.println("Creating a MeetingBuilder for the example.");
        MeetingBuilder mtgBuilder = new MeetingBuilder();
        try{
            System.out.println("Creating a new Appointment with a MeetingBuilder");
            System.out.println("(notice that the same create arguments will produce");
            System.out.println(" an exception, since the MeetingBuilder enforces a");
            System.out.println(" mandatory end date)");
            appt = pimScheduler.createAppointment(
                mtgBuilder, createDate(20669221230),
                null, "Trek convention"new LocationImpl("Fargo, ND"),
                createAttendees(4));
            System.out.println("Successfully created an Appointment.");
            System.out.println("Appointment information:");
            System.out.println(appt);
            System.out.println();
        }
        catch (InformationRequiredException exc){
            printExceptions(exc);
        }
        
        System.out.println("Creating a new Appointment with a MeetingBuilder");
        System.out.println("(This time, the MeetingBuilder will provide an end date)");
        try{
            appt = pimScheduler.createAppointment(
                mtgBuilder,
                createDate(2002411000),
                createDate(2002411130),
                "OOO Meeting",
                new LocationImpl("Butte, MT"),
                createAttendees(2));
            System.out.println("Successfully created an Appointment.");
            System.out.println("Appointment information:");
            System.out.println(appt);
            System.out.println();
        }
        catch (InformationRequiredException exc){
            printExceptions(exc);
        }
    }
    
    public static Date createDate(int year, int month, int day, int hour, int minute){
        dateCreator.set(year, month, day, hour, minute);
        return dateCreator.getTime();
    }
    
    public static ArrayList createAttendees(int numberToCreate){
        ArrayList group = new ArrayList();
        for (int i = 0; i < numberToCreate; i++){
            group.add(new ContactImpl("John", getLastName(i)"Employee (non-exempt)""Yoyodyne Corporation"));
        }
        return group;
    }
    
    public static String getLastName(int index){
        String name = "";
        switch (index % 6){
            case 0: name = "Worfin";
                break;
            case 1: name = "Smallberries";
                break;
            case 2: name = "Bigbootee";
                break;
            case 3: name = "Haugland";
                break;
            case 4: name = "Maassen";
                break;
            case 5: name = "Sterling";
                break;
        }
        return name;
    }
    
    public static void printExceptions(InformationRequiredException exc){
        int statusCode = exc.getInformationRequired();
        
        System.out.println("Unable to create Appointment: additional information is required");
        if ((statusCode & InformationRequiredException.START_DATE_REQUIRED0){
            System.out.println("  A start date is required for this appointment to be complete.");
        }
        if ((statusCode & InformationRequiredException.END_DATE_REQUIRED0){
            System.out.println("  An end date is required for this appointment to be complete.");
        }
        if ((statusCode & InformationRequiredException.DESCRIPTION_REQUIRED0){
            System.out.println("  A description is required for this appointment to be complete.");
        }
        if ((statusCode & InformationRequiredException.ATTENDEE_REQUIRED0){
            System.out.println("  At least one attendee is required for this appointment to be complete.");
        }
        if ((statusCode & InformationRequiredException.LOCATION_REQUIRED0){
            System.out.println("  A location is required for this appointment to be complete.");
        }
        System.out.println();
    }
}

interface Contact extends Serializable{
    public static final String SPACE = " ";
    public String getFirstName();
    public String getLastName();
    public String getTitle();
    public String getOrganization();
    
    public void setFirstName(String newFirstName);
    public void setLastName(String newLastName);
    public void setTitle(String newTitle);
    public void setOrganization(String newOrganization);
}

class ContactImpl implements Contact{
    private String firstName;
    private String lastName;
    private String title;
    private String organization;
    
    public ContactImpl(String newFirstName, String newLastName,
        String newTitle, String newOrganization){
            firstName = newFirstName;
            lastName = newLastName;
            title = newTitle;
            organization = newOrganization;
    }
    
    public String getFirstName(){ return firstName; }
    public String getLastName(){ return lastName; }
    public String getTitle(){ return title; }
    public String getOrganization(){ return organization; }
    
    public void setFirstName(String newFirstName){ firstName = newFirstName; }
    public void setLastName(String newLastName){ lastName = newLastName; }
    public void setTitle(String newTitle){ title = newTitle; }
    public void setOrganization(String newOrganization){ organization = newOrganization; }
    
    public String toString(){
        return firstName + SPACE + lastName;
    }
}
interface Location extends Serializable{
    public String getLocation();
    public void setLocation(String newLocation);
}
class LocationImpl implements Location{
    private String location;
    
    public LocationImpl(){ }
    public LocationImpl(String newLocation){
        location = newLocation;
    }
    
    public String getLocation(){ return location; }
    
    public void setLocation(String newLocation){ location = newLocation; }
    
    public String toString(){ return location; }
}

class Scheduler{
    public Appointment createAppointment(AppointmentBuilder builder,
        Date startDate, Date endDate, String description,
        Location location, ArrayList attendeesthrows InformationRequiredException{
            if (builder == null){
                builder = new AppointmentBuilder();
            }
        builder.buildAppointment();
        builder.buildDates(startDate, endDate);
        builder.buildDescription(description);
        builder.buildAttendees(attendees);
        builder.buildLocation(location);
        return builder.getAppointment();
    }
}

class Appointment{
    private Date startDate;
    private Date endDate;
    private String description;
    private ArrayList attendees = new ArrayList();
    private Location location;
    public static final String EOL_STRING =
        System.getProperty("line.separator");
    
    public Date getStartDate(){ return startDate; }
    public Date getEndDate(){ return endDate; }
    public String getDescription(){ return description; }
    public ArrayList getAttendees(){ return attendees; }
    public Location getLocation(){ return location; }

    public void setDescription(String newDescription){ description = newDescription; }
    public void setLocation(Location newLocation){ location = newLocation; }
    public void setStartDate(Date newStartDate){ startDate = newStartDate; }
    public void setEndDate(Date newEndDate){ endDate = newEndDate; }
    public void setAttendees(ArrayList newAttendees){
        if (newAttendees != null){
            attendees = newAttendees;
        }
    }
    
    public void addAttendee(Contact attendee){
        if (!attendees.contains(attendee)){
            attendees.add(attendee);
        }
    }
    
    public void removeAttendee(Contact attendee){
        attendees.remove(attendee);
    }
    
    public String toString(){
        return "  Description: " + description + EOL_STRING +
            "  Start Date: " + startDate + EOL_STRING +
            "  End Date: " + endDate + EOL_STRING +
            "  Location: " + location + EOL_STRING +
            "  Attendees: " + attendees;
    }
}

class AppointmentBuilder{
    
    public static final int START_DATE_REQUIRED = 1;
    public static final int END_DATE_REQUIRED = 2;
    public static final int DESCRIPTION_REQUIRED = 4;
    public static final int ATTENDEE_REQUIRED = 8;
    public static final int LOCATION_REQUIRED = 16;
    
    protected Appointment appointment;
    
    protected int requiredElements;
    
    public void buildAppointment(){
        appointment = new Appointment();
    }
    
    public void buildDates(Date startDate, Date endDate){
        Date currentDate = new Date();
        if ((startDate != null&& (startDate.after(currentDate))){
            appointment.setStartDate(startDate);
        }
        if ((endDate != null&& (endDate.after(startDate))){
            appointment.setEndDate(endDate);
        }
    }
    
    public void buildDescription(String newDescription){
        appointment.setDescription(newDescription);
    }
    
    public void buildAttendees(ArrayList attendees){
        if ((attendees != null&& (!attendees.isEmpty())){
            appointment.setAttendees(attendees);
        }
    }
    
    public void buildLocation(Location newLocation){
        if (newLocation != null){
            appointment.setLocation(newLocation);
        }
    }
    
    public Appointment getAppointment() throws InformationRequiredException{
        requiredElements = 0;
        
        if (appointment.getStartDate() == null){
            requiredElements += START_DATE_REQUIRED;
        }
        
        if (appointment.getLocation() == null){
            requiredElements += LOCATION_REQUIRED;
        }
        
        if (appointment.getAttendees().isEmpty()){
            requiredElements += ATTENDEE_REQUIRED;
        }
        
        if (requiredElements > 0){
            throw new InformationRequiredException(requiredElements);
        }
        return appointment;
    }
    
    public int getRequiredElements(){ return requiredElements; }
}

class InformationRequiredException extends Exception{
    private static final String MESSAGE = "Appointment cannot be created because further information is required";
    public static final int START_DATE_REQUIRED = 1;
    public static final int END_DATE_REQUIRED = 2;
    public static final int DESCRIPTION_REQUIRED = 4;
    public static final int ATTENDEE_REQUIRED = 8;
    public static final int LOCATION_REQUIRED = 16;
    private int informationRequired;
    
    public InformationRequiredException(int itemsRequired){
        super(MESSAGE);
        informationRequired = itemsRequired;
    }
    
    public int getInformationRequired(){ return informationRequired; }
}

class MeetingBuilder extends AppointmentBuilder{
    public Appointment getAppointment() throws InformationRequiredException{
        try{
            super.getAppointment();
        }
        finally{
            if (appointment.getEndDate() == null){
                requiredElements += END_DATE_REQUIRED;
            }
            
            if (requiredElements > 0){
                throw new InformationRequiredException(requiredElements);
            }
        }
        return appointment;
    }
}


           
       
Related examples in the same category
1. 构建模式-范例构建模式-范例
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.