如何创建具有不同数量参数的对象?

时间:2017-06-04 16:44:43

标签: java

我的问题是如果我想创建一个代表联系人列表中某个人的对象。 用户必须输入姓氏,否则,程序将无法运行。 除此之外,用户可以输入名字,地址,电话号码等,但这些都不是必需的。 那么我该如何解决这些问题呢? 我可以用不同的参数组合创建大量的构造函数,但我认为这不是正确的解决方案。

你们可以帮助我吗?

4 个答案:

答案 0 :(得分:3)

简单方法:使用具有必填字段的构造函数,并提供setter来评估其他字段。

如果需要不变性,则更复杂:使用构建器构建Person实例。

Builder应该是Person的静态嵌套类,它提供了构建Builder对象的方法,而Builder类提供了设置{{1}的值的方法。 1}}对象。
此方法中的每一个都返回当前Builder实例,但Builder创建并返回创建的build()对象。

以下是一个示例类:

Person

您可以这样使用它:

public class Person {

    private String name;
    private String address;
    private String phone;

    // private constructor
    private Person() {
    }

    public static class Builder {

      private String name;
      private String address;
      private String phone;

      // mandatory fields
      public Builder(String name) {
        this.name = name;
      }

      public Builder address(String address) {
        this.address = address;
        return this;
      }

      public Builder phone(String phone) {
        this.phone = phone;
        return this;
      }

      public Person build() {
        Person p = new Person();
        p.name = name;
        p.address = address;
        p.phone = phone;
        return p;
      }
   }

}

Person person = new Person.Builder("myName").address("myAddress").build();

答案 1 :(得分:0)

你听说过二传手吗?

我曾经在一个项目中遇到过这个问题,我就是这样想的:

  1. 必须填充值:通过构造函数传递它们
  2. 可选值:使用setters传递它们
  3. 班主任的代码样本:

        public class person {
    
        private String lastName;
        private String firstName;
        private String address;
        private String phoneNumber;
    
        //lastName is mandatory you should pass when you create your object
        public person(String lastName) {
            this.lastName = lastName;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
        public String getFirstName() {
            return firstName;
        }
    
        //This is optional you can pass it using this setter
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
        public String getAddress() {
            return address;
        }
    
        //This is optional you can pass it using this setter
        public void setAddress(String address) {
            this.address = address;
        }
    
        public String getPhoneNumber() {
            return phoneNumber;
        }
    
        //This is optional you can pass it using this setter
        public void setPhoneNumber(String phoneNumber) {
            this.phoneNumber = phoneNumber;
        }
    
        }
    

    主类的代码示例

    public class yourclass {
        public static void main(String[] args) {
            String lastname="fooer"; //choose how you want to intercept the input if you are building a GUI app, tell me I would help you with JavaFx or Swing code.
            String firstname="foo" //let's say this is the only optional to pass
            person newperson = new person(lastname);
            if (firstname.isEmpty()==false)
                newperson.setFirstName(firstname);
    
        }
    }
    

答案 2 :(得分:0)

您不想尝试的是telescoping constructor pattern?如果你没有较少的字段(因此需要较少的构造函数),这很容易实现 在良好的设计方面,您可以考虑以下事项: Builder Design pattern

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Input;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Shapes;
using System.Windows.Input;

namespace Gesture_App_UWP
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
    // Global translation transform used for changing the position of 
    // the Rectangle based on input data from the touch contact.
    private TranslateTransform dragTranslation;
    private CompositeTransform deltaTransform;
    private TransformGroup rectangleTransforms;

    //Various Brushes for changing rectangle's color
    private Brush stationaryBrush;
    private SolidColorBrush greenBrush = new SolidColorBrush(Colors.Green);
    private SolidColorBrush redBrush = new SolidColorBrush(Colors.Red);
    private SolidColorBrush orangeBrush = new SolidColorBrush(Colors.Orange);

    public MainPage()
    {
        this.InitializeComponent();

        // Pointer event listeners.
        touchRectangle.PointerPressed += touchRectangle_PointerPressed;
        touchRectangle.PointerReleased += touchRectangle_PointerReleased;

        // Listener for Manipulation events
        touchRectangle.ManipulationStarted += new ManipulationStartedEventHandler(touchRectangle_ManipulationStarted);
        touchRectangle.ManipulationDelta += new ManipulationDeltaEventHandler(touchRectangle_ManipulationDelta);
        touchRectangle.ManipulationInertiaStarting += new ManipulationInertiaStartingEventHandler(touchRectangle_ManipulationInertiaStarting);
        touchRectangle.ManipulationCompleted += new ManipulationCompletedEventHandler(touchRectangle_ManipulationCompleted);

        //Initiate new instance of various Transforms
        dragTranslation = new TranslateTransform();
        deltaTransform = new CompositeTransform();
        rectangleTransforms = new TransformGroup();

        // Combine moving & resizing tranforms into one TransformGroup.
        // Rectangle's RenderTransform can only contain a single transform or TransformGroup.
        rectangleTransforms.Children.Add(dragTranslation);
        rectangleTransforms.Children.Add(deltaTransform);

        //Assign manipulation data to rectangle
        touchRectangle.RenderTransform = rectangleTransforms;
    }
    private async void button_Info_Click(object sender, RoutedEventArgs e)
    {
        var dialog_Info = new MessageDialog("Page Lynn Potter");
        await dialog_Info.ShowAsync();
    }
    private async void button_Instructions_Click(object sender, RoutedEventArgs e)
    {
        var dialog_Instructions = new MessageDialog("PINCH - Zoom In & Out, LONG TOUCH - Toggle Color, FLICK - Move Rectangle");
        await dialog_Instructions.ShowAsync();
    }
    //Handler for pointer released event.
    private void touchRectangle_PointerReleased(object sender, PointerRoutedEventArgs e)
    {
        //Change rectangle's color
        touchRectangle.Fill = greenBrush;
    }
    // Handler for pointer pressed event.
    private void touchRectangle_PointerPressed(object sender, PointerRoutedEventArgs e)
    {
        //Change rectangle's color
        touchRectangle.Fill = orangeBrush;
    }
    void touchRectangle_ManipulationInertiaStarting(object sender, ManipulationInertiaStartingRoutedEventArgs e)
    {
        //Change rectangle's color
        touchRectangle.Fill = redBrush;
    }
    void touchRectangle_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
    {
        // Save rectangle's original color before changing color.
        stationaryBrush = touchRectangle.Fill;

        //Change rectangle's color
        touchRectangle.Fill = orangeBrush;
    }

    // Handler for ManipulationDelta event.
    // ManipulationDelta data loaded into translation transform & applied to Rectangle
    void touchRectangle_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
    {
        ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY | ManipulationModes.Scale;

        // Move rectangle.
        dragTranslation.X += e.Delta.Translation.X;
        dragTranslation.Y += e.Delta.Translation.Y;


        // Resize rectangle.
        if (deltaTransform.ScaleX > 0 && deltaTransform.ScaleY > 0)
        {
            //Scale Rectangle
            deltaTransform.ScaleX *= e.Delta.Scale;
            deltaTransform.ScaleY *= e.Delta.Scale;
        }
    }
    //When manipulation ends, event handler restores original color to Rectangle
    void touchRectangle_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
    {
        // Restore rectangle's original color.
        touchRectangle.Fill = stationaryBrush;
    }
}
}

答案 3 :(得分:0)

您可以使用不同的模式:

  • JavaBeans(Getters and Setters)
  • 伸缩构造函数(具有不同数量的参数)
  • 构建器模式

但不是它们将保证用户的期望输入。

这主要取决于您的用户界面以及可能从用户界面获得的数据。

如果UI可能会向您发送空值或空字符串,您应该从必填字段处理它们,因为无论您使用哪种模式,它都是您的业务逻辑。

让我们确定你需要从来自UI的DTOPerson建立新人:

public Person register(Person personDTO) {
    validatePerson(personDTO);
    Person person = new Person(personDTO.getName(), personDTO.getSurname());
    // for not mandatory fields
    person.setAddress(personDTO.getAddress());

    // ...
}

public void validatePerson(Person person) {
    Optional.ofNullable(person.getName()).orElseThrow(() -> 
        new IllegalArgumentException("Name is required!")
    );

    Optional.ofNullable(person.getSurname()).orElseThrow(() ->
            new IllegalArgumentException("Surname is required!")
    );
}