More Related Content
Similar to Design Pattern Explained CH1
Similar to Design Pattern Explained CH1 (20)
More from Jamie (Taka) Wang
More from Jamie (Taka) Wang (20)
Design Pattern Explained CH1
- 2. Naive Problem
If I were to give you the task of wri2ng code to access a descrip2on
of shapes that were stored in a database and then display them.
© Taka Wang, 2017 2
- 3. Structural Programming thinking - possible steps
1. Locate the list of shapes in the database. (
)
2. Open up the list of shapes. ( )
3. Sort the list according to some rules. ( )
4. Display the individual shapes on the monitor. (
)
© Taka Wang, 2017 3
- 4. Func%onal Decomposi%on ( )
4a. Iden)fy the type of shape. ( )
4b. Get the loca-on of the shape. ( )
4c. Call the appropriate func2on that will display the shape, giving
it the shape’s loca2on. (
Func2on)
© Taka Wang, 2017 4
- 7. Dealing with Changes: Func2onal Decomposi2on
4c. Call the appropriate func2on that will display the shape, giving
it the shape’s loca2on. (
Func2on)
© Taka Wang, 2017 7
- 8. Dealing with Changes: Func2onal Decomposi2on
4c. Call the appropriate func2on that will display the shape, giving it the
shape’s loca2on. ( Func2on)
Using Modularity to Contain Varia2on
function: display shape
input: type of shape, description of shape
action:
switch (type of shape)
case square: put display function for square here
case circle: put display function for circle here
© Taka Wang, 2017 8
- 9. 1. ( )
( )
2. type of shape, descrip5on of shape
( array of points )
© Taka Wang, 2017 9
- 10. Func%onal Decomposi%on
1. Weak cohesion (Cohesion refers to how “closely the opera6ons in a
rou6ne are related; cohesion as clarity because the more that
opera6ons are related in a rou6ne (or a class), the easier it is to
understand things.)
2. Tight Coupling (Coupling refers to “the strength of a connec6on
between two rou6nes. Coupling is a complement to cohesion.)
The goal is to create rou/nes with internal integrity (strong cohesion) and
small, direct, visible, and flexible rela/ons to other rou/nes (loose
coupling).
© Taka Wang, 2017 10
- 11. You are an instructor at a conference. People in your class have
another class to a5end following yours, but don’t know where it is
located. One of your responsibili>es is to make sure everyone
knows how to get to the next class.
© Taka Wang, 2017 11
- 12. 1. Get list of people in the class.
2. For each person on this list, do the following:
1. Find the next class he or she is taking.
2. Find the loca;on of that class.
3. Find the way to get from your classroom to the person’s next
class.
4. Tell the person how to get to his or her next class.
© Taka Wang, 2017 12
- 13. 1. A way of ge,ng the list of people in the class.
2. A way of ge,ng the schedule for each person in the class.
3. A program that gives someone direc<ons from your classrom to
any other classroom.
4. A control program that works for each person in the class and
does the reuqired steps for each person.
© Taka Wang, 2017 13
- 14. !
You would probably post direc2ons to go from this classroom to
the other classrooms and then tell everyone in the class, I have
posted the loca.ons of the classes following this in the back of the
room, as well as the loca.ons of the other classrooms. Please use
them to go to your next classroom.
© Taka Wang, 2017 14
- 15. 1. Giving explicit direc+ons to everyone.
2. Giving general instruc+ons and then expect the each person will
figure out how to do the task individually. ( delega+on)
© Taka Wang, 2017 15
- 18. 1. Conceptual: domain concepts
What am I responsibile for?
2. Specifica9on: interface
How am I used?
3. Implementa9on: How do I fulfill
my responsibili9es?"
© Taka Wang, 2017 18
- 19. (you are telling people what you want, not how to
do it.)
( )
© Taka Wang, 2017 19
- 21. The objects were iden*fied by looking at the en**es in the
problem domain. I iden*fied the responsibili1es (or methods) for
each object by looking at what these en**es need to do.
© Taka Wang, 2017 21
- 22. Apply Fowler's Perspec/ves
• At the conceptual level, an object is a set of responsibili5es. (
)
• At the specifica-on level, an object is a set of methods
(behaviors) that can be invoked by other objects or by itself. (
)
• At the implementa-on level, an object is code and data and
computa5onal interac5ons between them.
© Taka Wang, 2017 22
- 23. Object-Oriented Approach
1. Start the control program.
2. Instan4ate the collec4on of students in the classroom.
3. Tell the collec4on to have the students go to their next class.
4. The collec4on tells each student to go to his or her next class.
5. Each student:
1. Finds where his next class is.
2. Determines how to get there.
3. Goes there.
6. Done.
© Taka Wang, 2017 23
- 24. Abstract and Polymorphism
• Student abstract class, GraduageStudent and RegularStudent classes.
• Abstract classes define what other, related, classes can do. Abstract
classes act as placeholders for other classes.
• Collec;on Student Concept (Abstract)
Student (Concrete)
•
Derived Class (Polymorphism)
© Taka Wang, 2017 24
- 25. 1. Locate the list of shapes in the database. (
)
2. Open up the list of shapes. ( )
3. Sort the list according to some rules. ( )
4. Display the individual shapes on the monitor. (
)
© Taka Wang, 2017 25
- 28. • ( )
1. Concrete Class
2. Derived Class
•
1. Collec6on Sor6ng algorithm
The object-oriented approach has limited the impact of changing
requirements.
© Taka Wang, 2017 28
- 29. 1.
2. (
depend on interface, not implementa4on)
3. The internals of an object are unknown to other objects. (
< >)
© Taka Wang, 2017 29