我们还可以将其称为构建器模式吗?

发布于 2024-11-08 16:44:54 字数 1812 浏览 0 评论 0原文

这是我的工厂方法类。

public class AFactory : FactoryBase {
        public override ProductBase ProduceProdut() {
            return new SuperCoolProduct();
        }
}

这是我的建造者课程。

public class ABuilder : BuilderBase {

        ProductBase product = new SuperCoolProduct()

        public override void BuildStep1() {
             //Do Something (e.g. product.Add() )
        }
        public override void BuildStep2() {
             //Do some more...(e.g. product.AddMore() ) 
        }

        public override ProductBase GetFinalProduct() {
            return product;
        }
}

我的问题如下〜1

)构建器是否必须公开Director可以调用的一些方法来组合产品?

这个类还可以被视为构建器类吗?或者,我们必须将其称为工厂类吗?我认为我们不能再将下面的此类称为构建器,它现在是一个工厂类。

public class ABuilder : BuilderBase {

            ProductBase product = new SuperCoolProduct();        

            public override ProductBase GetFinalProduct() {
                BuildStep1();    
                BuildStep2();

                return product;
            }

            private void BuildStep1() {
                 //Do Something (e.g. product.Add() )
            }
            private void BuildStep2() {
                 //Do some more...(e.g. product.AddMore() ) 
            }

    }

2)下面的代码是否是结合“构建器”模式和“工厂方法”模式的一个很好的示例?

public class AFactory : FactoryBase {

                BuilderBase builder;

                public AFactory(BuilderBase builder){
                   this.builder = builder;
                }
                public override ProductBase ProduceProdut() {
                    this.builder.BuildStep1();
                    this.builder.BuildStep1();
                    return this.builder.GetFinalProduct();
                }
        }

Here is my factory-method class.

public class AFactory : FactoryBase {
        public override ProductBase ProduceProdut() {
            return new SuperCoolProduct();
        }
}

Here is my builder class.

public class ABuilder : BuilderBase {

        ProductBase product = new SuperCoolProduct()

        public override void BuildStep1() {
             //Do Something (e.g. product.Add() )
        }
        public override void BuildStep2() {
             //Do some more...(e.g. product.AddMore() ) 
        }

        public override ProductBase GetFinalProduct() {
            return product;
        }
}

My questions are the following ~

1) Does the builder has to expose some methods that the Director can call for compositing the product?

Can this class still be considered as a builder class? Or, Do we have to call it as a factory class? I think that we can't call this class below as a builder anymore and it's a factory class now..

public class ABuilder : BuilderBase {

            ProductBase product = new SuperCoolProduct();        

            public override ProductBase GetFinalProduct() {
                BuildStep1();    
                BuildStep2();

                return product;
            }

            private void BuildStep1() {
                 //Do Something (e.g. product.Add() )
            }
            private void BuildStep2() {
                 //Do some more...(e.g. product.AddMore() ) 
            }

    }

2) Would the code below be a good example of combining "Builder" pattern and "Factory Method" pattern?

public class AFactory : FactoryBase {

                BuilderBase builder;

                public AFactory(BuilderBase builder){
                   this.builder = builder;
                }
                public override ProductBase ProduceProdut() {
                    this.builder.BuildStep1();
                    this.builder.BuildStep1();
                    return this.builder.GetFinalProduct();
                }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百思不得你姐 2024-11-15 16:44:54
  1. 是的,
  2. 我会称其为导演。但是是的。您可以将其称为工厂方法实现。

对于工厂:更常见的是调用方法CreateXXX

  1. Yes
  2. I would call it a director. But yes. You could call it a factory method implementation.

As for factories: It's more common to call the methods CreateXXX

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文