用工厂和建造者创建复杂的类

时间:2018-08-31 06:36:09

标签: oop design-patterns class-design class-diagram

背景: 我建立了一个网上购物的类图。 为了使用拖曳类型(golden-User和silver-User)创建用户界面,我使用了工厂模式。
但是User类变得非常复杂。

如何通过bulider创建此类,另一方面,可以在类名上保留指定用户类型(例如工厂)的功能 (将帮助我通过多态性而不是if&else来识别哪种类型)

3 个答案:

答案 0 :(得分:1)

Decorator模式是一个简单的解决方案:

public class Main {

    public static void main(String[] args) {

        User silverUser = new UserDecorator(new SilverUser("Kyriakos", "Georgiopoulos"));
        User goldenUser = new UserDecorator(new GoldenUser("GoldenUser firstName", "GoldenUser lastName"));
        User nullUser = new UserDecorator(null);

        System.out.println(silverUser.firstName() + " " + silverUser.lastName() + " is " + silverUser.type());
        System.out.println(goldenUser.firstName() + " " + goldenUser.lastName() + " is " + goldenUser.type());
        System.out.println(nullUser.firstName() + " " + nullUser.lastName() + " is " + nullUser.type());
         }
}

interface User {
    String firstName();

    String lastName();

    String type();
}

class SilverUser implements User {

    private final String firstName;
    private final String lastName;

    SilverUser(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String firstName() {
        return firstName;
    }

    public String lastName() {
        return lastName;
    }

    public String type() {
        return "SilverUser ";
    }
}

class GoldenUser implements User {

    private final String firstName;
    private final String lastName;

    GoldenUser(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String firstName() {
        return firstName;
    }

    public String lastName() {
        return lastName;
    }

    public String type() {
        return "GoldenUser ";
    }
}

class UserDecorator implements User {
    private final User user;
    
    UserDecorator(User user){
        this.user = user;
    }
    
    public String firstName() {
        return user != null && user.firstName() != null && user.firstName().length() > 0 ?
                user.firstName() : "";
    }

    public String lastName() {
        return user != null && user.lastName() != null && user.lastName().length() > 0 ?
                user.lastName() : "";
    }
    
    public String type() {
        return user != null ? user.type() : "NullPointerException";
    }
}

答案 1 :(得分:0)

在这种特殊情况下,您不应该使用Factory创建同一类的不同实例。它可用于创建一种通用抽象的不同实现。尝试实现IUser接口。然后,通过两个类实现此接口:GoldenUser和SilverUser。您的工厂将创建GoldenUser或SilverUser的实例,并将其作为IUser返回。同样,除了接口IUser之外,您可能还可以创建User抽象类,该类将由GoldenUser和SilverUser继承。

答案 2 :(得分:0)

两种模式的目的是不同的:Factory创建对象实例(可以容纳更多其他类实例)时,Builder的目标是逐步创建对象并减少重载的构造函数。 例如(带有Java代码段):

工厂方法

用户界面:

public interface User {
}

GoldUser类:

class GoldUser implements User {
  // ... field declarations
  // Ctor
  GoldUser(fields...){}
  // ... methods 
}

SilverUser类:

class SilverUser implement User {
  // ... field declarations
  // Ctor
  SilverUser(fields...){}
  // ... methods 
}

用户工厂类:

public class UserFactory {
  // ... user versions
  public static int GoldUser = 0;
  public static int SilverUser = 1;

  // ... private Ctor because we don't want to instantiate this class - only in this example
  private UserFactory (){}

  // ... creating appropriate User instance
  public static User createUser(int userType){
     switch (userType){
        case GoldUser: return new GoldUser;
        case SilverUser: return new SilverUser;
        default throw new WrongUserTypeException("Wrong User Type");
     }
  }
}

在另一个班级:

// ... code stuff here
User user=UserFactory.createUser(1); // will return new SilverUser instance
// ... other code stuff here

构建器模式

如果您的班级中有很多字段,而其中只有一些是必填字段,则不必创建许多构造函数,那么一个构建器就足够了:

class UserBuilder{
private static Service_A serviceA;  // required
private static Service_B serviceB;  // required
private static Service_C serviceC;
private static Service_D serviceD;
private static Service_E serviceE;

// since this builder is singleton
private static UserBuilder builderInstance = new UserBuilder();
private UserBuilder () {};

public static UserBuilder getBuilderInstance (Service_A service_A, Service_B service_B){
serviceA = service_A;
serviceB = service_B;
serviceC = null; 
serviceD = null;
serviceE = null;
return builderInstance;
}

public static UserBuilder addServiceC (Service_C service_C) {
  serviceC = service_C;
  return builderInstance; 
}

public static UserBuilder addServiceD (Service_D service_D) {
  serviceC = service_D;
  return builderInstance; 
}

public static UserBuilder addServiceE (Service_E service_E) {
  serviceE = service_E;
  return builderInstance; 
}

public static User build(){
  return new User (serviceA, ServiceB, ServiceC, ServiceD, ServiceE);
}

然后您可以建立自定义用户:

UserBuilder aUserBuilder = UserBuilder.getBuilderInstance(aServiceA, aServiceB);
// ... other stuff
    aUserBuilder.addServiceE(aServiceE);
///... more stuff
 User aUser= aUSerBuilder.addServiceC(aServiceC)
    .build(); // will return the fresh built User instance

希望我能为您服务! 问候, Cs