classes encapsulation

Upload: forumer00

Post on 05-Apr-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Classes Encapsulation

    1/19

    Object-OrientatedDesign Unit 3: Objects

    and ClassesJin Sa

  • 7/31/2019 Classes Encapsulation

    2/19

    Session Aims

    introduce the concept of objects andclasses.

    present methods for identifying classes

    given textual descriptions of requiredsystem functionality.

    illustrate the benefits of highly cohesiveloosely coupled classes.

    explain the UML class and object notion.

    introduce the term encapsulation andshow the benefits of applying

    encapsulation to classes.

  • 7/31/2019 Classes Encapsulation

    3/19

    What is an Object? -recap

    an object knows something itsdata, and

    it knows how to do something itsfunctions.

    E.g. a Bank account object

    may know the balance of the account and

    provide functions to add or withdraw funds.

    In object-orientated programminglanguages object behaviour is

    implemented in the form of methods.

  • 7/31/2019 Classes Encapsulation

    4/19

    Classes and objects

    A class is a blueprint for an object. The classdefines the generic behaviour of an objectand the type of data it may store

    Objects that behave in a manner specified bya class are called instances of that class.

    Example A Film class :

    data could include title of the film and thecertification rank

    Methods could be modifying certification rank

    One instance of the Film class could containdetails of the film The Godfather and anotherThe Dark knight.

    Each of the instances stores exactly the sametype of data, film name, certification and

    classification yet each one represents adifferent film.

  • 7/31/2019 Classes Encapsulation

    5/19

    Classes and objects :summary

    Objects are instances of classes.

    Classes define the behaviour and thedata types for an object

    Data held in instances of the sameclass will typically vary.

    Instances of the same class provideexactly the same services (subject tothe data not controlling the flow).

  • 7/31/2019 Classes Encapsulation

    6/19

    Terms, Concepts and UMLNotation

    In UML, a class is rendered as arectangle.

  • 7/31/2019 Classes Encapsulation

    7/19

    UML notation: Class

    Every class must have a name, e.g.Student

    An attribute (data) is a namedproperty of a class that describes arange of values that instances of theproperty may hold, e.g. every

    customer has a name and anaddress.

    An operation is an implementation of

    a service that can be requested from

  • 7/31/2019 Classes Encapsulation

    8/19

    CRC Process

    Once you have a reasonable list ofcandidate classes in your OO designyou can further evaluate their place

    in a particular system by identifyingtheir responsibilities what they do, and

    who they need to work with to do this their collaborations.

    The process is referred to as the ClassesResponsibilities and Collaborationsprocess or more commonly as the CRCprocess.

  • 7/31/2019 Classes Encapsulation

    9/19

    CRC process

    write the names of all candidate classes on a series of

    cards. work through the textual narrative of the systemrequirements, i.e. the use case descriptions assigningresponsibilities to classes, e.g. determining doingsomething, knowing something and decision making.Classes that have no responsibilities can be removed

    because they do not add value to the system

  • 7/31/2019 Classes Encapsulation

    10/19

    A Common technique modellingthe vocabulary of a system

    To model the vocabulary of a system, Identify those things users or

    implementers use to describe theproblem or the solution. Use CRC anduse case based analysis to help findthese abstractions.

    For each abstraction, identify a set ofresponsibilities. Make sure that eachclass is crisply defined and there is agood balance of responsibilities amongall your classes.

    Provide the attributes and the

    operations that are needed to carry out

  • 7/31/2019 Classes Encapsulation

    11/19

    Example

    Abstractionsdrawn from aretail problem

    domain couldinclude:Customer, Order,Product,

    Shipment, Invoiceand Warehouse.Abstract such as

    Transaction is asolution related

    abstraction.

  • 7/31/2019 Classes Encapsulation

    12/19

    Encapsulation

    encapsulation refers tothe process of an objectcontrolling outsideaccess to its internaldata.

    Client only knows how tocall the methods, nothow the methods areimplemented.

    Data attributes aremade private, i.e.protected from beingdirectly accessed fromoutside

    Visible methods(services) are made

  • 7/31/2019 Classes Encapsulation

    13/19

    Benefits of encapsulation

    The main benefit of encapsulation is that theprogrammer may change the implementation ofthe object without affecting the whole program, ifhe or she preserves the interface of the object.

    Any change of the data representation will affectonly the implementation of the methods.

    By keeping data private and providing public well-defined service methods the role of the object

    becomes clear to other objects. This increasesusability.

  • 7/31/2019 Classes Encapsulation

    14/19

    Cohesion and Coupling

    objects in the system must work together to achieve acommon set of goals

    avoid placing too many responsibilities with a singleobject.

    create object that knows how to do one task and they

    are able to do that task well Example: a system goal such as load the product

    catalogue, We may have one object that knows how to open

    and read a file, another object may know how to

    parse the XML text, a third object may know how tostore in memory the data-type structures and so on Alternatively, we could create a single object that

    did all of these tasks but not flexible for reuseelsewhere as it does too much. Also changing to thecode may be difficult because all of the functionality

    is so tightly linked. Hence we would say the ease ofmaintenance for this object is poor.

  • 7/31/2019 Classes Encapsulation

    15/19

    Object coupling andcohesion

  • 7/31/2019 Classes Encapsulation

    16/19

    Object coupling andcohesion

    Cohesive object does one thing only and does that onething well

    Low cohesive objects are unclear about their purposes Highly cohesive classes have clearly defined

    relationships with other classes, and collaborate in

    clear ways Classes of low cohesion have complex and confused

    relationships with other classes, and collaborate incomplex ways.

    Within models of tightly coupled classes, a change to

    one class often has knock-on effects on many otherrelated classes. Strong coupling results in complexmodels that are difficult to understand and maintain,

    With loosely coupled classes, a change to one class isoften encapsulated to that class, preventing knock-onchanges to related classes.

  • 7/31/2019 Classes Encapsulation

    17/19

    Object coupling andcohesion

    In summary, by creating well defined publicservices you are making it clear what theresponsibilities of the object are.

    By allocating the appropriate responsibilities to

    a class and not overburdening it you arepromoting cohesion and loose coupling. Assigning too many responsibilities to an

    individual class will promote loose coupling butat the expense of cohesion. Assigning too few

    responsibilities or misplacing responsibilitieswill create a system with tight coupling.Therefore as software designers we aim to

    create highly cohesive objects that are looselycoupling. In reality this will not always bepossible and often the skill of the designer is

    finding the best tradeoffs, a happy medium!

  • 7/31/2019 Classes Encapsulation

    18/19

    Recap

    Classes exist in code and are theblueprints used to create objects

    Objects are instances of classes andexist at runtime

    A common modelling technique is tomodel the vocabulary of the system.

    Encapsulation is achieved by making

    data private and services public We seek to develop classes that arehighly cohesive and loosely coupled.

  • 7/31/2019 Classes Encapsulation

    19/19

    Student activity

    Complete student activity 3.2