我想知道是否有更好的方法来实现createEmployee(),它使用字典或其他方式来快速查找被请求的类型而不是if-else块。
function Clerk( options ) { this.hourRate = options.hourRate || 20; this.firstName = options.firstName || "no first name"; this.lastName = options.lastName || "no last name"; this.id = options.id || "-9999999999"; } function Manager( options) { this.hourRate = options.hourRate || 200; this.firstName = options.firstName || "no first name"; this.lastName = options.lastName || "no last name"; this.id = options.id || "-9999999999"; this.yearBonus = options.yearBonus || "200000"; } function Teacher( options) { this.hourRate = options.hourRate || 100; this.firstName = options.firstName || "no first name"; this.lastName = options.lastName || "no last name"; this.id = options.id || "-9999999999"; this.subject = options.subject || "history"; } var EmployeesFactory = function() {}; EmployeesFactory.prototype.createEmployee = function (options) { if(options.employeeType == "Clerk") employeeConstructor = Clerk; else if(options.employeeType == "Manager") employeeConstructor = Manager; else if(options.employeeType == "Teacher") employeeConstructor = Teacher; return new employeeConstructor(options); } var factory = new EmployeesFactory(); var person = factory.createEmployee( { employeeType: "Manager", firstName: "Haim", lastName: "Michael", id: 234234234 } ); document.write(person instanceof Manager);I was wondering if there is a better way to implement the createEmployee() that uses a dictionary or some other way to quickly lookup the type being requested rather than the if-else block.
function Clerk( options ) { this.hourRate = options.hourRate || 20; this.firstName = options.firstName || "no first name"; this.lastName = options.lastName || "no last name"; this.id = options.id || "-9999999999"; } function Manager( options) { this.hourRate = options.hourRate || 200; this.firstName = options.firstName || "no first name"; this.lastName = options.lastName || "no last name"; this.id = options.id || "-9999999999"; this.yearBonus = options.yearBonus || "200000"; } function Teacher( options) { this.hourRate = options.hourRate || 100; this.firstName = options.firstName || "no first name"; this.lastName = options.lastName || "no last name"; this.id = options.id || "-9999999999"; this.subject = options.subject || "history"; } var EmployeesFactory = function() {}; EmployeesFactory.prototype.createEmployee = function (options) { if(options.employeeType == "Clerk") employeeConstructor = Clerk; else if(options.employeeType == "Manager") employeeConstructor = Manager; else if(options.employeeType == "Teacher") employeeConstructor = Teacher; return new employeeConstructor(options); } var factory = new EmployeesFactory(); var person = factory.createEmployee( { employeeType: "Manager", firstName: "Haim", lastName: "Michael", id: 234234234 } ); document.write(person instanceof Manager);最满意答案
尝试这个:
var constructors; constructors = { "Clerk" : function (options) { // your constructor code here for clerks }, "Manager" : function (options) { // your constructor code here for managers }, "Teacher" : function (options) { // your constructor code here for teachers } }; EmployeesFactory.prototype.createEmployee = function (options) { return constructors[options.employeeType](options); };我建议保持EmployeesFactory隐藏的constructors对象。 这样你也可以摆脱不需要的功能名称( Clerk , Manager和Teacher )。
另外,您应该只创建一次constructors对象并在创建中重用它。 不要在创建中实例化它。 你可以用下面的方法做到这一点:
var EmployeesFactory = function () { var constructors; constructors = { "Clerk" : function (options) { // your constructor code here for clerks }, "Manager" : function (options) { // your constructor code here for managers }, "Teacher" : function (options) { // your constructor code here for teachers } }; return { "create" : function (options) { return constructors[options.employeeType](options); } }; };你这样得到工厂:
factory = EmployeesFactory();并可以这样创建东西:
factory.create(options);所有的都将舒适和隐藏在外面的封闭式封闭物内,外面没有任何胆量。
工厂模式的目的是为了隐藏对象构造的复杂性和细节,因此在模式的一个方面(我承认,轻微/微小)忽略了消耗方法。 通过使用闭包,您也可以获得隐藏的好处。
Try this:
var constructors; constructors = { "Clerk" : function (options) { // your constructor code here for clerks }, "Manager" : function (options) { // your constructor code here for managers }, "Teacher" : function (options) { // your constructor code here for teachers } }; EmployeesFactory.prototype.createEmployee = function (options) { return constructors[options.employeeType](options); };I recommend keeping the constructors object well hidden inside EmployeesFactory. This way you also get rid of the undesired function names (Clerk, Manager and Teacher).
Also, you should create the constructors object only once and reuse it in the create. Don't instantiate it in the create. You can do this in the following way:
var EmployeesFactory = function () { var constructors; constructors = { "Clerk" : function (options) { // your constructor code here for clerks }, "Manager" : function (options) { // your constructor code here for managers }, "Teacher" : function (options) { // your constructor code here for teachers } }; return { "create" : function (options) { return constructors[options.employeeType](options); } }; };You get the factory this way:
factory = EmployeesFactory();And can create stuff this way:
factory.create(options);All will be cozy and hidden inside the outer enveloping closure with no guts hanging outside.
The intention of the Factory Pattern is to hide the complexity and details of object construction, so keeping the methods around for consumption is missing out on an aspect (I admit, minor/minimal) of the pattern. By using closures you'll get the benefits of the hiding, too.
更多推荐
发布评论