380 likes | 540 Views
Martin Q. Zhao 9/20/2013. The Dao of Object-Orientation. Dao De Jing (Tao- Te Ching ) & Lao Zi (Lao Tzu) Dao of Programing Building the Virtual World OOAD Principles in Dao De Jing. topics. 道德 经 ( DaoDe Jing) & 老子 (Lao Tzu). 郭店竹 简 ( GuoDian Bamboo Slips ).
E N D
Martin Q. Zhao 9/20/2013 The Dao of Object-Orientation
Dao De Jing (Tao-TeChing) & Lao Zi (Lao Tzu) Dao of Programing Building the Virtual World OOAD Principles in Dao De Jing topics
A World With 10,000 Objects Nothingness, emptiness Name, concept Existence Subtlety The Tao Beginning The Tao that can be expressed in words is not the true and eternal Tao… (Tao:1) Heaven & earth, the world 10,000 things, objects
Dao Of Programming??? Info Books (September 1986) Addison-Wesley Professional; 1 edition (July 22, 2004) M & T Books (December 1990) Patrick Burns (December 16, 2012) Hayden Books; 2 Pap/Dskt edition (July 1994)
The discipline of computing is … • Computer programs: sets of instructions written in programming languages that automate Real World information transformation processes • Develop a software system is like building a “Virtual World” Computing & Computer Programs the systematic study of algorithmic processesthat describe and transform information: their theory, analysis, design, efficiency, implementation, and application. The fundamental question underlying all of computing is, ‘What can be (efficiently) automated?’
Software systems can be boiled down to • Systems: the solution as a whole (virtual worlds) • Programs: organizational units of code (or the VW) • Procedures: sequences of instructions as a unit • Statements: instructions • Expressions: meaningful combo of tokens • Tokens: variables, constants, operators, punctuations How To Structure Programs?
Procedural Vs. O-O Approaches • Procedural approach, aka Structured Programming (mid-1960’s) • OO approach (since 1980’s) • Data and algorithms go together (in an object) • No “top” unit top Procedures Or ways to process data Data
Real World & Virtual World • Real World (aka Problem Domain) • Consists of objects (or entities) • Virtual World (aka Solution Domain) • Specs of classes • Objects created at runtime (when VW is alive) Employee ID Name Phone Brand Category Price Name Address Payment method
Now let’s focus on the Virtual World Building the VW involves defining the classes that will be used to create objects at runtime At The Very Beginning… 有物混成,先天地生。 寂兮寥兮,独立而不改。 周行而不殆,可以为天地母。吾不知其名,强字之为道 , 强为之名曰大 。 S/W System- VW There is a thing integratedly formed And born earlier than Heaven and Earth. Silent and empty, it relies on nothing, Moving around forever. We may regard it as the mother of all things I don’t know its name, so I name it as Tao, And further name it as Great. (Tao:25) Object(s)
What is the simplest Java class? • Can it be used to create any object? • But can this object be used? • What happens behind the scene? • Class Object is the “mother” of all Java classes A Simplest Example class Dummy {} class A {} Inheritance class DummyTest { public static void main (String[] x) { Dummy d = new Dummy(); } } Add it here extends Object Add it here System.out.println(d);
An object has a type • Object : instance of a class, created at runtime • Class : template of all objects of the same type • An object has an identity • Two labs in CSB may look the same, but each is a unique entity • (Classes of) objects have their responsibilities: they need to • Know some information • perform some operations What Does An Object Have? Abstraction o:Order -orderId=101 -orderDate=1/1/11 +calOrderTotal() …
One easy choice • Just use one GOD CLASS, which • Knows everything and can do everything • But that is not OO, which tends to • Partition system into many classes around RW concepts, and • Distribute intelligence/behavior evenly among them How To Identify Classes & Assign Responsibilities? R. Martin (2003) Prentice Hall UML for Java Programmers 天地不仁,以万物为刍狗… Heaven and Earth have no favorite, They deem all objects as equally common. (Tao:5)
Keep being simple in nature and mind (见素抱朴) close to Tao to avoid “an early end” (不道早已:whatever goes against Tao will come to an early end) Keeping classes’ responsibilities simple (or cohesive), helps reuse & improve system maintainability Keep It Simple X 五色令人目盲; 五音令人耳聋; 五味令人口爽; 驰聘畋猎,令人心发狂;难得之货,令人行妨。 The five colors make man blind; The five sounds make man deaf; The five tastes make man lose his sense of taste; Riding and hunting make man wild w/ excitement; Rare goods goad man into stealing ... (Tao:12)
Instead of using all-purpose objects, objects with a single responsibility are preferred The Single Responsibility Principle R. Martin (2002) Prentice Hall Agile Software Development Maintainability
Focus on the three types of objects How Do I Know What Objects To Use? Tao begets the One; The One consists of Two in opposition (the Yin and Yang); The two begets the Three; All things connote the Yin and Yang. (Te:42) Tao begets the One; The One begets the Two; The Two begets the Three; The Three begets 10,000 things. (Te:42) 道生一, 一生二, 二生三, 三生万物。 (Cont’d on next slide)
A VW With 10,000 Objects CustomerEntity OrderControl Customer CustomerForm Actors OrderEntity SalesRep OrderFulfillControl SalesRepForm ProductEntity System Boundary
Analysis Class Stereotypes Entity classes usually represent a domain-specific situation or RW object Need to be saved between sessions Control classes represent a workflow, control, or calculation process Boundary classes are used to model interaction between the system and its actors. Jacobson, Booch, & Rambaugh (1999) Addison-Wesley The Unified Software Development Process
Each object can do only one thing • To fulfill its responsibility, an object can ask for help from (aka collaborate with) another object • Object collaboration is done by message passing • A message has • A recipient • An operation requested • Additional information to carry out the operation Interactions Of Objects In The VW changeGears(5) myBike:Bike me:Rider Java online tutorials: What Is an Object? http://docs.oracle.com/javase/tutorial/java/concepts/object.html
(Take) no action • One way to get help is to delegate • The client object will “do nothing” (无为) itself • It delegates reuses the service from the “server” object that implements it • The client and server roles are specific for each episode Let The Server Do It Choose to (take) no action Delegation objectB server server client client objectA
The Design by Contract approach prescribes • software designers should define formal, precise and verifiable interface specifications for software components (i.e. classes), including • Services/operations to provide, and • For each operation, to specify • Preconditions: need to be true before it’s carried out; • Postconditions: need to be true after it’s completed, and • Invariants: need to hold true during the object’s life time • These specifications are referred to as "contracts". Contract Between Objects Encapsulation Mitchell & McKim (2002) Addison-Wesley Design by Contract, by example
The Usefulness Of Nothingness Thirty spokes share one hub. It is just the space (the Nothingness) between them that makes a cart function as a cart. (Tao:11)
Interfaces, Abstract Classes, & Patterns • An abstract class may have some operations not implemented (in another word, left empty) • An interface is a purely abstract class in which no operations may be implemented • Interfaces and abstract classes are commonly used in design patterns Reusability [A design] pattern describes a problem that occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice. Alexander, Christopher (1977) Oxford University Press. A Pattern Language: Towns, Buildings, Construction.
Make Parts(components) Into A Unit (Composite) Client Composite pattern
Hide Parts Using A Façade(MyGameTank) Client Façade pattern
Decouple Client W/ Concrete Tank Abs. Factory pattern
Adding A New Tank Type W/ Ease E1T1TankFactory +createTank():E1T1Tank +createShell():E1T1TankShell E1T1Tank E1T1TankShell
Handling User Input W/ KeyListener Command pattern A hook method in the Java AWT API <<interface>> KeyListener +keyPressed(e:KeyEvent) +keyReleased(e:KeyEvent) +keyTyped(e:KeyEvent) MyGameFrame KeyHandler AbstractTank MyGamePanel +keyPressed(e:KeyEvent) +keyReleased(e:KeyEvent) +keyTyped(e:KeyEvent)
Software Frameworks • Another way to reuse is to use frameworks • A framework provides collections of interfaces, abstract classes, and concrete classes • Sample frameworks in Java • Swing, AWT: from GUI (boundary objects) • JDBC/IO/NIO: for saving entity objects to DB or files • Collections:for managing entity objects
The Strength Of Softness 天下莫柔弱于水,而攻坚强者莫之能胜,以其无以易之。 Nothing in the world is more supple than water, Yet nothing is more powerful than water In attacking hard and strong. (Te:78) The perfect goodness is like water. (Tao:8) 天下之至柔,驰骋天下之至坚。 The most supple in the world, Can go through the hardest in the world. (Te:43)
Favor flexibility over rigidity Software Is Soft 人之生也柔弱,其死也坚强。万物草木之生也柔脆,其死也枯槁。故坚强者死之徒,柔弱者生之徒。……强大处下,柔弱处上。 While alive, a person’s body is supple; when dead, it becomes rigid.While alive, grass and trees are supple; when dead, they become dry and stiff.Thus the hard and strong is of the dying sort; the supple and weak is of the living sort.… …Thus the strong and big is inferior to the weak and supple. (Te:76)
Rigidity Fragility Immobility Viscosity Needless complexity Needless repetition Opacity Design Smells: The Odors Of Rotting Software R. Martin (2003) Prentice Hall UML for Java Programmers