1 / 5

Elements of Design - Class initialization

Explore the significance of class initialization and methods in representing class behavior and state, with examples and best practices in design. Learn how to properly initialize class variables and instances to maintain consistency across instances.

henriettae
Download Presentation

Elements of Design - Class initialization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Elements of Design -Class initialization

  2. Class Methods - Class Instance Variables • Classes (Packet class) represents class (Packet). • Class instance variables are instance variables of class • They should represent the state of class: number of created instances, number of messages sent, superclasses, subclasses.... • Class methods represent class behavior: instance creation, class initialization, counting the number of instances.... • If you weaken the second point: class state and behavior can be used to define common properties shared by all the instances

  3. Class Initialization • How do we know that all the class behavior has been loaded? • At the end ! • Automatically called by the system at load time or explicitly by the programmer. • Used to initialize a classVariable, a pool dictionary or class instance variables. • ‘Classname initialize’ at the end of the saved files in Squeak • In postLoadAction: in VW

  4. Example of class initialization • Magnitude subclass: #Date • instanceVariableNames: 'day year' • classVariableNames: • 'DaysInMonth FirstDayOfMonth MonthNames • SecondsInDay WeekDayNames’

  5. Date class>>initialize • Date class>>initialize • "Initialize class variables representing the names of the months and • days and the number of seconds, days in each month, and first day of • each month. " • MonthNames := #(January February March April May • June July August September October November December ). • SecondsInDay := 24 * 60 * 60. • DaysInMonth := #(31 28 31 30 31 30 31 31 30 31 30 31 ). • FirstDayOfMonth := #(1 32 60 91 121 152 182 213 244 274 305 335 ). • WeekDayNames := #(Monday Tuesday Wednesday Thursday Friday Saturday Sunday )

More Related