CSC/ECE 517 Fall 2011/ch1 1e ap
Introduction to Object Oriented Design
Introduction to Object Oriented Programming
Object-oriented programming (OOP) is a programming language model organized around "objects" rather than "actions" and data rather than logic. It uses "objects" - data structures consisting of data fields and methods together with their interactions – to design applications and computer programs.
Object-oriented programming takes the view that what we really care about are the objects we want to manipulate rather than the logic required to manipulate them. Examples of objects range from human beings (described by name, address, and so forth) to buildings and floors (whose properties can be described and managed) down to the little widgets on your computer desktop (such as buttons and scroll bars).
Object-oriented programming is a method of programming based on a hierarchy of classes, and well-defined and cooperating objects.
- Classes
The class is the fundamental object-oriented unit. It is a structure that defines the data and the methods to work on that data. Classes define a set of objects that share a common structure and behavior. When you write programs in the Java language, all program data is wrapped in a class.All objects are created from classes, so it important to understand the structure of a class. In fact, it is the design of classes that has fueled the development of various Object Oriented languages.Object is an instance of a class.
- Objects
An "Object" is binding together of data and behavior, and Object Oriented programming, is the use of objects while solving the problem. What makes an "object" unique is the formal and explicit combination of these smaller pieces' behavior with its' data into a single entity.
Object Oriented Design
Object-oriented design is the process of planning a system of interacting objects for the purpose of solving a software problem. It is one approach to software design. Object-oriented design is the discipline of defining the objects and their interactions to solve a problem that was identified and documented during object-oriented analysis. Much of design involves refining the analysis model through the introduction of classes and algorithms that define exactly how certain required features are realized. Another common design activity is the development of an overall system architecture. This entails organizing the system as a set of major building blocks, such as subsystems or components. For a concurrent system, the architecture includes the basic task or process structure.For a distributed system, it includes the organization of hardware in terms of processors and their interconnections. Basically, object-oriented design (OOD) entails transforming the analysis model into a feasible design. Once the design reaches a sufficient level of specificity, it is translated into an implementation through object-oriented programming. This task can be either relatively straightforward or rather challenging, depending upon the amount of detail in the design.
The first question in OOD ,especially for newcomers, is "How can I decide what classes my program should have in it?" The fundamental rule is that a class should represent some abstraction. For example, a Date class might represent calendar dates, an Integer class might deal with integers, and a Matrix class would represent mathematical matrices. So you need to ask "What kinds of entities does my application manipulate?".
Some examples of potential classes in different application areas would include:
a) GUI/Graphics - Line, Circle, Window, TextArea, Button, Point
b) Architecture - Building, Plot, Room
b) Geography - River, Country, Sea, Continent
Instead of viewing an application as something that performs steps A, B, and C, that is, looking at the program in terms of its functions, instead ask what types of objects and data the application manipulates. Instead of taking a function-oriented approach, take an object-oriented one.
Object Oriented Concepts
- Abstraction: To implement real world entity into program, class uses the concept of abstraction. Abstraction is a process that involves identifying the crucial behavior of an object and eliminating irrelevant and tedious details.
- Encapsulation: Binding the data and code to access that data. Encapsulation only refers to a container which has a data and its related functions in it.
- Inheritance: Defining new classes from the existing one. The new class will get all the methods and properties of the existing class.
- Polymorphism: Ability to acquire different forms. There are basically two types of polymorphism. Compile Time (also knows as Early binding) and Run Time (also knows as Late binding) Polymorphism.
- Information Hiding: As encapsulation bind data and methods together, data hiding restrict sensitive data accessing to the system. It is also achieved by using access modifiers i.e. Private, Protected and Public.
Design principles and patterns
Object Oriented Design Principles
- The LiskovSubstitutionPrinciple (LSP): "An instance of a derived should be able to replace any instance of its superclass"
- The OpenClosedPrinciple (OCP): "A reusable class should be open for extension, but closed for modification."
- The DependencyInversionPrinciple (DIP): "The modules that implement a high level policy should not depend on the modules that implement the low level policies, but rather, they should depend on some well-defined interfaces."
- The InterfaceSegregationPrinciple (ISP): "The dependency of one class to another one should depend on the smallest possible interface."
- The ReuseReleaseEquivalencePrinciple (REP): "The granule of reuse is the granule of release." module == type, the fusion of distinct notions is what gives OO its distinctive flavor.
- The CommonClosurePrinciple (CCP):: "The classes in a package should be closed together against the same kinds of changes. A change that affects a package affects all the classes in that package."
Object Oriented Design Patterns
- Singleton - Ensure that only one instance of a class is created and Provide a global access point to the object.
- Factory - Creates objects without exposing the instantiation logic to the client and Refers to the newly created object through a common interface.
- Command - Encapsulate a request in an object, Allows the parameterization of clients with different requests and Allows saving the requests in a queue.
- Iterator - Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
- Strategy - Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
- Adapter - Convert the interface of a class into another interface clients expect. / Adapter lets classes work together, that could not otherwise because of incompatible interfaces.
Examples
Advantages of OOD
- Simplicity: software objects model real world objects, so the complexity is reduced and the program structure is very clear.
- Modularity: each object forms a separate entity whose internal workings are decoupled from other parts of the system.
- Maintainable: objects can be maintained separately, making locating and fixing problems easier.
- Re-usability: objects contain both data and functions that act on data, objects can be thought of as self-contained “boxes”.This feature
makes it easy to reuse code in new systems.
- Scalability: object’s interface provides a roadmap for reusing the object in new software and thus provides you with all the information
you need to replace the object without affecting other code.
References
- Principles of Object Oriented Design - http://c2.com/cgi/wiki?PrinciplesOfObjectOrientedDesign