1 / 28

Object Design

CEN 4010 Class 19 – 11/01. Object Design. Review System Design Object Design: Reuse Concepts Object Design Activities Design Patterns – Command, Singleton. From this point on you should only be working with the 4 or 5 use cases that your team will implement. Review System Design.

ralphjones
Download Presentation

Object Design

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. CEN 4010 Class 19 – 11/01 Object Design Review System Design Object Design: Reuse Concepts Object Design Activities Design Patterns – Command, Singleton From this point on you should only be working with the 4 or 5 use cases that your team will implement.

  2. Review System Design When decomposing the system you must address: • H/w and s/w mapping • Data management • Access control • Control flow • Boundary conditions Design goals are organized into the following groups: performance, dependability, cost, maintenance, and end user criteria. CEN 4010 Class 19 - 11/01

  3. Overview of Object Design • Refine the analysis and system design models. • Close gap between the application objects and COTS. • Identification of custom objects. • Adjustment of COTS components. • Precise specification of each subsystem interface and class. • Partition object model into sets of classes to be implemented by individual developers. CEN 4010 Class 19 - 11/01

  4. Overview cont Object Design includes four groups of activities: 1. Component selection: • Adapt components identified during system design to realize each subsystem. • Select class libraries and additional components for basic data structures and services. CEN 4010 Class 19 - 11/01

  5. Overview cont 2. Service specification: • Specify the subsystems services (identified during system design) in terms of class interfaces, including operations, arguments, type signatures and exceptions. • Identify ,missing operations and objects needed to transfer data among subsystems. • Result is the complete interface specification for each subsystem. For project specify only those subsystems you will be implementing. CEN 4010 Class 19 - 11/01

  6. Overview cont 3. Restructuring: • Manipulate the system model to increase code reuse or meet other design goals, i.e. incorporate the use of design patterns e.g. the command pattern. • Activities include: • converting n-ary associations to binary, • implementing binary associations as references, • merging two similar classes from two different subsystems into a single class, CEN 4010 Class 19 - 11/01

  7. Overview cont • collapsing classes with no significant behavior into attributes, • splitting complex classes into simpler ones, • rearranging classes and operations to increase the inheritance and packaging. During restructuring design goals are address e.g., maintainability, readability, and understandability of the system model. CEN 4010 Class 19 - 11/01

  8. Overview cont • Optimization: Addresses performance requirements i.e., changing algorithms, reducing multiplicity in associations, and adding derived attributes to improve the access to objects. • The four activities above occur concurrently. • Note object design is more complex than analysis or system design due to large number of objects and developers, the high rate of change, and the # of concurrent decisions made. CEN 4010 Class 19 - 11/01

  9. Object Design: Reuse Concepts Application objects vs. solution objects • Application objects represent concepts of the domain that the system manipulates. • Solution objects represent support components that do not have a counterpart in the application domain, e.g., GUIs, DBMSs. • During object design additional objects are identified. CEN 4010 Class 19 - 11/01

  10. Reuse Concepts cont • Recall during analysis • entity objects, their relationships, attributes, and operations were identified • most entity objects are application objects that are independent of any specific system • solution objects such as, boundary and control objects were identified. • During system design additional solution objects were identified in terms of s/w and h/w platforms. CEN 4010 Class 19 - 11/01

  11. Reuse Concepts cont • In object design we refine and detail both application and solution objects. Additional objects are often required. Spec. inheritance and implem. inheritance. • During analysis inheritance was used to classify objects based on common behavior. • Superclass (or base class) – objects with general behavior. • Subclass (or derived class – objects with specialized behavior. CEN 4010 Class 19 - 11/01

  12. Reuse Concepts cont • Focus of inheritance in object design is to reduce redundancy and enhance extensibility. Fig. 8.3 P. 311 • Factoring all redundant behavior into a single superclass reduces the risk of inconsistencies when changes are made. • Use of abstract classes and interfaces allows for extensibility i.e., new specialized behavior can be written in new subclasses. CEN 4010 Class 19 - 11/01

  13. Reuse Concepts cont • Inheritance is a powerful mechanism and should be carefully considered before use. • Note there is usually strong coupling between classes in the inheritance hierarchy. • There are two types of inheritance: • Implementation inheritance – sole purpose of reusing code. • Specification inheritance (or interface inheritance) – classification of concepts into type hierarchies. CEN 4010 Class 19 - 11/01

  14. Reuse Concepts cont Delegation • Delegation is an alternative to implementation inheritance that should be used when reuse is desired. • A class is said to delegate another class if it implements an operation by resending a message to another class. • Delegation makes explicit the dependencies between the reused class and the new class. CEN 4010 Class 19 - 11/01

  15. Reuse Concepts cont • Preferable mechanism to implementation inheritance as it does not interfere with existing components and leads to more robust code. • Specification inheritance is preferable to delegation in subtyping situations as it leads to a more extensible design. Liskov Substitution Principle: • If an object of type S can be substituted in all places where an object of type T is expected then S is a subtype of T (strict inheritance). CEN 4010 Class 19 - 11/01

  16. Reuse Concepts cont • Subtyping: • subclasses are behaviorally compatible with the superclass, • the subclass should at least have the same interface as its ancestors, • note the superclass here represents a subset of the behavior common to all subclasses, • normally occurs if the inheritance performs only an extension, rather than overriding something that is already defined in the superclass, CEN 4010 Class 19 - 11/01

  17. Reuse Concepts cont • Specialization: • subclass is no longer behaviorally compatible with the superclass, if the operations and/or information structure have been redefined or deleted. • Focus on subtyping for implementing inheritance (specification inheritance). CEN 4010 Class 19 - 11/01

  18. Reuse Activities - Design Patterns • The use of delegation and inheritance in conjunction with abstract classes decouples the interface of a subsystem from its actual implementation. • Recall a design pattern provides a scheme for refining the subsystems or components of a software system, or relationships between them. • Patterns considered: Bridge, Adapter, Strategy, Abstract Factory, Command, Composite (see Appendix A in text). CEN 4010 Class 19 - 11/01

  19. Reuse Activities - Design Patterns • Name:Command Pattern (A.4 P. 699) • Problem description: Encapsulates requests so that they can be executed, undone, or queued independently of the request. • Solution: A Command abstract class declares the interface supported by all ConcreteCommands. ConcreteCommands encapsulate a service to be applied to a Receiver. The Client creates ConcreteCommands and binds them to specific Receivers. The Invoker actually executes or undoes the command. CEN 4010 Class 19 - 11/01

  20. Reuse Activities - Design Patterns Fig: Structure of Command Pattern Command Invoker invokes execute() Client <<binds>> ConcreteCommand1 Receiver action1() action2() execute() ConcreteCommand1 execute() CEN 4010 Class 19 - 11/01

  21. Reuse Activities - Design Patterns • Consequences: • The object of the command (Receiver) and the algorithm of the command (ConcreteCommand) are decoupled. • Invoker is shielded from specific commands. • ConcreteCommands are objects. They can be created and stored. • New ConcreteCommands can be added without changing existing code. CEN 4010 Class 19 - 11/01

  22. Reuse Activities - Design Patterns • Examples: • Providing and undo stack for user commands: All user-visible commands are refinements of the Command abstract class. Each command is required to implement the do(),undo(), and redo() methods. Once a command is executed, it is pushed onto the an undo stack. If the user wishes to undo the last command, the Command object on the top of the stack is sent the message undo(). • See P. 699 for other examples. CEN 4010 Class 19 - 11/01

  23. class Light {   public void turnOn( ) {       System.out.println("Light is on ");    }    public void turnOff( ) {        System.out.println("Light is off");   }} public class TestCommand {      public static void main(String[] args) {            Light  testLight = new Light( ); Command testL; if(args.length = = 0)             testL = new LightOnCommand(testLight); else              testL = new LightOffCommand(testLight);            testL.execute( ); } } public interface Command { public abstract void execute ( );}class LightOnCommand implements Command {   private Light myLight;   public LightOnCommand ( Light L) {        myLight  =  L;   }   public void execute( ) {   myLight.turnOn( );  }} class LightOffCommand implements Command {   private Light myLight;  public LightOffCommand ( Light L) {      myLight  =  L;   }   public void execute( ) {      myLight.turnOff( );   }} CEN 4010 Class 19 - 11/01

  24. Reuse Activities - Design Patterns • Name:Singleton Pattern (GOF) • Problem description: Ensures a class only has one instance, and provide a global point of access to it. • Solution: Hide the operation that creates the instance behind a class operation (that is, either a static member function or a class method) that guarantees only one instance is created. This approach ensures that a singleton is created and initialized before its first use. Note: can also create singletons with subclasses (see GOF). CEN 4010 Class 19 - 11/01

  25. Reuse Activities - Design Patterns Fig: Structure of Singleton Pattern Singleton Invoker invokes static uniqueInstance singletonData static Instance() SingletonOperation() GetSingletonData() Note: static Instance() returns uniqueInstance CEN 4010 Class 19 - 11/01

  26. Reuse Activities - Design Patterns • Consequences: • Controlled access to instance: strict control over how and when clients access it. • Reduced name space: avoids polluting the name space with global variables. • Permits refinement of operations and representation: Singleton class may be subclassed. • Permits a variable number of instances: use a similar approach to control the number of instances that the application uses. CEN 4010 Class 19 - 11/01

  27. Reuse Activities - Design Patterns • More flexibility than class operations: can use static operations, however difficult to design, also may prevent overriding and polymorphism. • Examples: • To use the instance methods of the Runtime class in Java , you must obtain a reference to the sole Runtime object by using a static method of the class. • All parts of an application must use the same SecurityManager object. The unique instance is accessed through a static method of the System class. • The Interviews user interface toolkit uses the Singleton pattern to access the unique instance of its Session and WidgetKit classes. CEN 4010 Class 19 - 11/01

  28. public class Singleton { /** * The constructor could be made private * to prevent others from instantiating this * class. But this would also make it * impossible to create instances of * Singleton subclasses. */ protected Singleton( ) { // ... }   /** * A handle to the unique Singleton * instance. */ static private Singleton _instance = null;   /** * @return The unique instance of this * class. */ static public Singleton instance( ) { if ( null = = _instance) { _instance = new Singleton( ); } return _instance; }     //. ...additional methods omitted... } CEN 4010 Class 19 - 11/01

More Related