Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Is Object Oriented nesessary? COSCUP 2024

Is Object Oriented nesessary? COSCUP 2024

material for the session in COSCUP 2024, 2024-08-03

Naoki Kishida

August 03, 2024
Tweet

More Decks by Naoki Kishida

Other Decks in Programming

Transcript

  1. 2024/08/03 2 Self introduction • Naoki Kishida • LINE Yahoo

    • X(twitter): @kis • blog: きしだのHatena • (nowokay.hatenablog.com) • Writing Java book「プロになるJava」 • not only for Java introduction but also Programming introduction
  2. 08/03/2024 3 Agenda • What is Object Oriented in this

    session • Objective and Functional • from Object Oriented to Data Oriented • How to use Object Oriented
  3. Object Oriented as Design technique • Not used. • UML?

    it is just notation • Importance is I/O or UI definition and architecture • we don't design each procedure. Just follow the architecture. • we decide the detail of design during coding perhaps with test • (the term 'design' refers to the pre-code outline)
  4. Considering conditions other than inheritance • OO if method(obj) is

    written as obj.method() • I doubt whether it is effective enough to make a big trend • OO if you use classes • structure enough? • OO if you write data and functions together • Abstract Data Type • OO if you create objects • To avoid static, to create objects for object-oriented laundering • All good ideas for development is OO • Software engineering branding failure...
  5. Relationship between objects and functions • Objects and functions are

    commutative • Objects with no attribute and single method can be treated as a function • Java lambda expression • A function that returns an element binding giving name can be treated as an object Function<String, Integer> myObj(int a, int b) { return name -> switch(name) { case "a" -> a; case "b" -> b; case "mul" -> a * b; case "add" -> a + b; default -> throw new RuntimeException(); }; } class MyFunc() { int apply(int a, int b) { return a + b; } }
  6. Implements functional feature with design patterns • Design patterns complement

    the missing feature of the language • Something is wrong on memorizing patterns and writing them by hands • Many patterns are no longer necessary with some of language feature • The patterns of higher-order functions are reproduced using class and inheritance • ex Template methods
  7. Template methods • Basics of polymorphism abstract class Base {

    abstract void before(); abstract void after(); void proc() { before(); println("Hello"); after(); } } class School extends Base { @Override void before() { println("Stand up"); } @Override void after() { println("Sit down"); } }
  8. Implement with higher-order functions • Higher-order functions make it simple

    void proc(Runnable before, Runnable after) { before.run(); println("Hello"); after.run(); } proc(() -> println("Stand up"), () -> println("Sit down")); ※ Runnable is used to represent a function with no arguments and no return value
  9. Polymorphism is branching in advance evaluation • How to aggregate

    conditions • Thinking about the cart summarization • Calculation is different between Packed and Bulk sealed interface Type { record Bulk(int price, int unit) implements Type {} record Packed(int price) implements Type {} } record Item(Type type, int amount) {} var cart = List.of( new Item(new Packed(300), 3), new Item(new Bulk(250, 100), 230));
  10. branch with switch • branch with switch var cart =

    List.of( new Item(new Packed(300), 3), new Item(new Bulk(250, 100), 230)); int total = cart.stream() .mapToInt(item -> switch(item) { case Item(Packed(int price), int amount) -> price * amount; case Item(Bulk(int price, int unit), int amount) -> price * amount / unit; }).sum();
  11. branch with inheritance • Using inheritance seems to be more

    streamlined • no change the calc part even if another Type added sealed interface Type { int calc(int amount); record Bulk(int price, int unit) implements Type { int calc(int amount) { return price * amount / unit; } } record Packed(int price) implements Type { int calc(int amount) { return price * amount; } } } var cart = List.of( new Item(new Packed(300), 3), new Item(new Bulk(250, 100), 230)); int total = cart.stream() .mapToInt(item ->  item.type().calc(item.amount())) .sum();
  12. Problems when using inheritance • It is necessary to follow

    multiple classes • It is difficult to write when the condition is not only Type • Time zone, etc • branching is required within the Type • It is a problem if there is Type that you don't know • In business processing, it is better to have all in one place
  13. from Object Oriented to Data Oriented • Object Oriented •

    OO is a technique for modularization • But modularization is already done • Browser - WebServer • Micro Service • No need to further modularize our programs • Data Oriented • treat a data as data(not object) • define characteristics of data as a type • https://www.infoq.com/articles/data-oriented-programming-java/
  14. Define characteristics of data as a type • Abstract Data

    Type • Algebraic Data Type • not polymorphism but branch
  15. Abstract Data Type • Define a type that is resistant

    to change and flexible by exposing only data operations • Encapsulation of data • Includes only data operations as operations
  16. Abstract Data Type • Exposing only the data operation, resistant

    to change and flexible • Encapsulation • only has data operation as operation • Guideline for define a type class PersonName { PersonName (String surname, String givenName); String getFullName(); String getSurname(); String getGivenName(); } class PersonName { private String surname; private String givenName; PersonName (String surname, String givenName) { this.surname = surname; this.givenName = givenName; } String getFullName() { return "%s %s".formatted(surname, givenName); } ... class PersonName { private String fullName; PersonName (String surname, String givenName) { this.fullName = surname + " " + givenName; } String getFullName() { return fullName; } String getSurname() { return fullName.split(" ")[0]; } ... Both are OK
  17. Consider pure data types • Pure functions • No side

    effect • Same return value for same arguments • Pure data type(Coined word in this session) • No side effects outside of the data type • Same return value for same arguments and same attributes
  18. Algebraic Data Type • Guidelines for combining types • Working

    with types algebraically • Algebraically = Additions and Multiplications • Product Type • Sum type
  19. Product Type • Types that combines values • record, class,

    array • Possible patterns are multiplication of patterns of each types • record A(boolean a, byte b) {} • 2 x 256 = 512 patterns
  20. Sum Type • The value will be either type •

    catch (IOException | InterruptedException ex) • inherit • Possible patterns are additive • boolean | byte • 2 + 256 = 258 patterns • sealed sealed interface Op { record AddOp(int a, int b) implements Op {} record SubOp(int a, int b) implements Op {} }
  21. How to use Object-Oriented • Recognize the dataness and the

    modularity of classes • Layer your application • Use Object-Oriented at the endpoints of application
  22. Class dataness and modularity • Class has characteristics as data

    type and as modules • Class as data type • Multiple instance • The fields hold another data that contains • Class as module • basically Singleton • fields hold another module that depends • Becomes a subsystem • Collection of procedures class Name { String first; String last; Name(String first, String last) { this.first = first; this.last = last; } String toString() { return first + " " + last; } } @RestController class Greeting { @GetMapping("/hello") String hello() { return "hello"; } @GetMapping("/myname") Name myName() { return new Name("Naoki", "Kishida"); } }
  23. Object-Oriented is a state management technique • Commonize a state

    management • example • I/O • List abstract class IO { boolean opened; void open() { if (opened) return; openImpl(); opened = true; } void close() { if (!opened) error; closeImpl(); opened = false; } abstract void openImpl(); abstract void closeImpl(); } class MyIO extends IO { @Override void openImpl() { println("oeped!"); } @Override void closeImpl() { println("closed!"); } }
  24. Classification of class • Classification the role of class •

    Class has a characteristic of type and a characteristic of module Classification Role Characteristic Note System boundary External interactions like I/O Module + Type Fit for Object Oriented. but already provided by frameworks and libraries Data Retain a data Type Procedure Describe the procedure Module not treated as a type
  25. Use object-oriented at the terminal • State management appears at

    the end of the module • Ex: DOM、HTTP、DB connections requre state management • Intermediate processing can be stateless GUI logic data store user application DOM HTTP DB conn
  26. Use object-oriented at the terminal • Inherit DBConnection to implement

    MySQLConnection and OracleConnection, it good to use object-oriented • but MySQLConnection and OracleConnection are required as only one implementation • If browser manages GUI state, GUI framework can be stateless • Structured GUI definition and event handlers
  27. Layer your application • Separate module and data • Application

    processing is stateless • Module is a collection of functions • Dividing function, use layer • avoiding infinite loops • Data flows on layers • Data-oriented programming
  28. Summary • Object-Oriented is programming that uses inheritance and state

    management • Words other than object-oriented can be used for other than inheritance • Easy to communicate technically because there is no confusion • Object-Oriented rarely appears in application • Managing state on terminal, the middle will be stateless=Functional • Required for frameworks and middleware • Let's study object-oriented and create frameworks and middleware!