110 likes | 308 Views
Unit 11 Generic Interfaces and Encapsulation, Continued. Kirk Scott. This is an introductory unit. These are the units/chapters belonging to this section of the course : Unit 12, Flyweight, book chapter 13 Unit 13, Decorator, book chapter 27 Unit 14, Command, book chapter 24
E N D
Unit 11Generic Interfaces and Encapsulation, Continued Kirk Scott
This is an introductory unit. • These are the units/chapters belonging to this section of the course: • Unit 12, Flyweight, book chapter 13 • Unit 13, Decorator, book chapter 27 • Unit 14, Command, book chapter 24 • Unit 15, State, book chapter 22
What will be given next is an extremely brief overview of these topics. • Although the patterns are different from each other, they can be viewed as having this broad characteristic in common: • They wrap up functionality in a desirable way • In other words, they provide models for how to divide attributes and operations among classes in an overall design
Flyweight • Book definition: • The intent of the Flyweight pattern is to use sharing to support large numbers of fine-grained objects efficiently. • Comment mode on: • It might be preferable to say “large numbers of references to fine-grained objects”, although there may also be a large number of objects • Simple large-scale sharing is supported by making the shared object(s) immutable
Decorator • Book definition: • The intent of Decorator is to let you compose new variations of an operation at runtime. • Comment mode on: • The word compose describes nested construction where the resulting object contains a composite of functionalities • The Decorator pattern is not related to the composite design pattern, which will be given later
Command • Book definition: • The intent of the Command pattern is to encapsulate a request in an object. • Comment mode on: • Informally, this pattern allows you to “pass a method” • You do this by creating a class containing the desired method, constructing an instance of the class, and passing that object as a parameter • In the receiving code it is then possible to call the desired method on the object
State • Book definition: • The intent of the State pattern is to distribute state-specific logic across classes that represent an object’s state • Comment mode on: • Although literally true, “distribute” is somewhat misleading
By distributing using the state pattern, the overall goal, and saving, is that the state-specific logic will occur fewer times than in a design that handles this logic differently • Particular bits of that logic will occur only once, in one state method, rather than many times, in several different domain methods
In summary: • Flyweight encapsulates the core, immutable characteristics of an entity into a class • Decorator nests construction of objects of different classes, resulting in an object with a “concentric” set of functionalities • Command packages a method in a class so that it can be called elsewhere by passing an object that the method can be called on • State subdivides functionality associated with states into different classes • In one way or another, these patterns all have something to do with what belongs in a given class in a design.
These are my mnemonic devices for the patterns and their characteristics: • Flyweight: Immutable core • Decorator: Nested construction, concentric function • Command: Packaged, wrapped, passable method • State: Subdivide design to support (state) transition