class diagram refactoring example p3 practical example t120b029 2012 pavasario semestras

21
Class diagram refactoring example P3 practical example T120B029 2012 pavasario semestras

Upload: jocelyn-eaton

Post on 18-Dec-2015

235 views

Category:

Documents


2 download

TRANSCRIPT

Class diagram refactoring example

Class diagram refactoring example

P3 practical exampleT120B029

2012 pavasario semestras

Typical e-commerce example

• The code that retrieves all products within a given category and store results in a cash.

3

The Snowflake Rule

• The Snowflake Rule – For a non-trivial system, no two analysts will come up with the identical class models for the same application domain

T120B029

Atributes and methods• Determining classes is an iterative

task!• How do we name attributes?• Meaningful in a business context

e – eilutė, i – indeksas ;-)

Modeling Associations• UML has several relations that seem

to all mean the same thing : "has a".1. Association, 2. Aggregation,3. Composition

So, what is the difference between them?

Association• Represents the ability of one

instance to send a message to another instance.

• Implemented as a local variable (pointer) or as a method.

• The arrowheads on the end of the line indicate the directionality of the association

Association example

Aggregation

• Sometimes an object is made up of other objects. A part cannot contain its whole.

• makes sense - a room is part of a building

• doesn't make sense - an address is part of a person

Composition• Particular case of aggregation• The room can not exist without

building

Generalization• “is-a” relationship

Realization• the relationship between the

interface and the implementing class

Dependancy• Connect two classes to indicate

that the connection between them is at a higher level of abstraction than an association relationship

• A change to the Product class might require a change to the Cart class

Refactoring: what‘s wrong with our

example?

Wrong things are1. The ProductService depends on

API changes of the ProductRepository.

2. The code is untestable. Without having a real ProductRepository unable to test ProductService.

3. Dependancy on HttpContext as a cache storage provider (platform specific!)

Dependancy inversion principle

• Decouple ProductService from ProductRepository by having both depend from an interface

Dependancy injection principle

• Move the responsibility of creating ProductRepository implementation out of the ProductService class

Dependancy injection for cache

• Fine, but problem remains: HttpContect API can’t implement the new ICacheStorage interface

Adapter Pattern

The result of application of the adapter pattern

Special case• What if you don’t want to cache

data?• Problem: always provide the

ProductService constructor with the implementation of IChacheStorage

• Provide a null reference and littering the code with checks for a null

Null Object (Spesial case) pattern

Public class NullObjectCache : ICacheStorage{public void Store(){

//Do nothing}

}