Builder Design Pattern

Поделиться
HTML-код
  • Опубликовано: 27 янв 2025

Комментарии •

  • @ProgramArtist
    @ProgramArtist  7 лет назад +1

    Let me know what would you like me to talk about, it can be anything as long as it is programming related

    • @I_get_to_start_my_bucketlist
      @I_get_to_start_my_bucketlist 6 лет назад +1

      hi, I am a java guy and decide to learn more and work more with typescript. I really appreciate your work to break down typescript knowledge into details. well done.
      How about some comparisons between java and typescript regarding general aspects such as exception, OOP, IO, collection, thread vs worker. (I always wonder if typescript can be like java or better than java for big scale projects)
      thanks. :)

    • @web3tel
      @web3tel 5 лет назад

      Thanks, but I am a bit disappointing as the approach shown here does not leverage unique type system of typescript. I hope it should be possible to encode rules of building pizza as constrains so any errors will be detected by compiler and invoking build method for incomplete pizza must not type check. Maybe you can make another tutorial for typesafe builder? Thanks again.

    • @ProgramArtist
      @ProgramArtist  5 лет назад

      @@web3tel This video emphasizes the builder pattern. I use Typescript here as a tool to demonstrate it. You can create the same pattern using Javascript, Java, C#, Python etc. Not all the languages support all the features especially the compile-time detection features you are talking about.
      Also, it really depends on the use-case, for example, here I set default values in the build method in case nothing was provided. So we don't have to call every method of the builder in order for it to work properly.
      Encoding it in the compile-time will be a very hard thing to do and I'm not really sure it is possible. Just think of a case where you create a builder and pass it through a bunch of external modules and functions which on their own terms with some specific logic call the builder set methods. Catching that not all the methods were called in the compile-time would be impractical since it might depend on some variable values and some logical conditions. (I would use tests instead)
      The builder supposed to be more flexible, if you want to make sure that every variable is initialized at compile-time, you should probably use Factory instead of a builder. The way to do it with builder is to validate the variables at runtime.
      Hope my point was clear

    • @web3tel
      @web3tel 5 лет назад

      Thanks, this video demonstrates build pattern in general. Maybe it is topic for another video, but I have feeling that constrains in typescript are powerful enough to describe requirements to data integrity at least for cases similar to creating pizza. Maybe pizza should be defined as product of required ingredients , where some ingredients are union of default values or values set by users. As we add new ingredients typescript will infer new shape of the type. When shape (type) meets all required constrains compiler will type check invocation of build. I am just beginning with Typescript but I am already fascinated with power of Typescript type system.

    • @ProgramArtist
      @ProgramArtist  5 лет назад

      @@web3tel Be careful not to abuse it and make something way too complicated to understand, maintain and upgrade in the future. These are also important aspects of software engineering.

  • @buncha1499
    @buncha1499 6 лет назад +1

    Thank you very much ProgramArtist!

  • @ЕвгенийИванов-ю2б
    @ЕвгенийИванов-ю2б 5 лет назад

    I understood everything. Thanks!

  • @Km-pn3hf
    @Km-pn3hf 5 лет назад +1

    thanks

  • @gidmanone
    @gidmanone 4 года назад

    hi , could you please leave a comment on why you stopped uploading videos. thanks)

    • @ProgramArtist
      @ProgramArtist  4 года назад +1

      Hi, thanks for asking, I'm sorry I stopped, I intend some day to continue.
      I stopped because I started working in a new company which is more demanding so it left less free time. We also bought a new house and have a lot of renovation projects to do. I have some ideas of new videos I keep adding to my list but I can't find the energy to film them.

    • @gidmanone
      @gidmanone 4 года назад +1

      @@ProgramArtist thanks for the response. we will be here waiting for you whenever you decide to comeback. happy new year to you and your family)

  • @web3tel
    @web3tel 5 лет назад

    One more question: the build method throws Error. Is it idiomatic way to return error at TypeScript? Would not it make sense to return union of Pizza and some error? Or may be optional Pizza?

    • @ProgramArtist
      @ProgramArtist  5 лет назад

      The exceptions mechanism is built in many languages for a reason, it has nothing to do with TypeScript.
      Returning error codes from methods will require you to write an error checking code after every method call.
      If for example you call 3 methods in a row you now need to check after every one of them their return value and make sure it is not an error. Like so:
      result1 = foo();
      if (isError1(result1)) { .... }
      result2 = bar();
      if (isError2(result2)) { ... }
      Using exceptions you can catch them. The error handling code is now concentrated in one place and the main logic in another one.
      try {
      foo();
      bar();
      ) catch(error) {
      ...
      }

    • @web3tel
      @web3tel 5 лет назад

      @@ProgramArtist Thanks. I know that that now days all mainstream languages provide ability to compose computations that returns error using map or flatMap (sometimes using some syntactical sugar). In case of Typescript I think one case use 'optional chaining" (www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#optional-chaining). I thought that TypeScript as a language with strong type system would prefer to encode errors with types, but it is good to know that TypeScript idiomatic way is to just to throw exceptions.

  • @web3tel
    @web3tel 5 лет назад

    Is there option to disable background music?