使用工厂和构建器创建复合体 class
Create complex class with factory and builder
背景:
我为网上购物构建了一个 class 图。
为了创建带有拖曳类型(金色用户和银色用户)的用户界面,我使用工厂模式。
但是 User class 变得非常复杂。
如何通过构建器创建此 class,另一方面,指定用户类型(如工厂)的能力将保留在 class 名称中
(将帮助我通过多态性而不是 if&else 来识别哪种类型)
在这种特殊情况下,您不应该使用 Factory 创建相同 class 的不同实例。它可用于创建一个通用抽象的不同实现。尝试实现 IUser 接口。然后通过两个classes实现这个接口:GoldenUser和SilverUser。您的工厂将创建 GoldenUser 或 SilverUser 的实例,并且 return 它作为 IUser。此外,您可以创建用户抽象 class 而不是接口 IUser,它将由 GoldenUser 和 SilverUser 继承。
两种模式的意图不同:Factory 创建一个对象实例(可以容纳更多其他 class 实例),而 Builder 的目标是逐步创建对象并减少重载的构造函数。
例如(使用 java 个片段):
工厂方法
用户界面:
public interface User {
}
黄金用户class:
class GoldUser implements User {
// ... field declarations
// Ctor
GoldUser(fields...){}
// ... methods
}
银用户class:
class SilverUser implement User {
// ... field declarations
// Ctor
SilverUser(fields...){}
// ... methods
}
用户工厂Class:
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");
}
}
}
在你的另一个 class:
// ... code stuff here
User user=UserFactory.createUser(1); // will return new SilverUser instance
// ... other code stuff here
建造者模式
如果您的 class 中有很多字段并且其中只有一些是强制性的,则您不必创建很多构造函数,一个构建器就足够了:
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
装饰器模式是一个简单的解决方案:
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";
}
}
背景:
我为网上购物构建了一个 class 图。
为了创建带有拖曳类型(金色用户和银色用户)的用户界面,我使用工厂模式。
但是 User class 变得非常复杂。
如何通过构建器创建此 class,另一方面,指定用户类型(如工厂)的能力将保留在 class 名称中 (将帮助我通过多态性而不是 if&else 来识别哪种类型)
在这种特殊情况下,您不应该使用 Factory 创建相同 class 的不同实例。它可用于创建一个通用抽象的不同实现。尝试实现 IUser 接口。然后通过两个classes实现这个接口:GoldenUser和SilverUser。您的工厂将创建 GoldenUser 或 SilverUser 的实例,并且 return 它作为 IUser。此外,您可以创建用户抽象 class 而不是接口 IUser,它将由 GoldenUser 和 SilverUser 继承。
两种模式的意图不同:Factory 创建一个对象实例(可以容纳更多其他 class 实例),而 Builder 的目标是逐步创建对象并减少重载的构造函数。 例如(使用 java 个片段):
工厂方法
用户界面:
public interface User {
}
黄金用户class:
class GoldUser implements User {
// ... field declarations
// Ctor
GoldUser(fields...){}
// ... methods
}
银用户class:
class SilverUser implement User {
// ... field declarations
// Ctor
SilverUser(fields...){}
// ... methods
}
用户工厂Class:
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");
}
}
}
在你的另一个 class:
// ... code stuff here
User user=UserFactory.createUser(1); // will return new SilverUser instance
// ... other code stuff here
建造者模式
如果您的 class 中有很多字段并且其中只有一些是强制性的,则您不必创建很多构造函数,一个构建器就足够了:
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
装饰器模式是一个简单的解决方案:
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";
}
}