Martin Q. Zhao 9/20/2013 THE DAO OF OBJECT-ORIENTATION TOPICS Dao De Jing (Tao-Te Ching) & Lao Zi (Lao Tzu) Dao of Programing Building the Virtual World OOAD Principles in Dao De Jing 道德经(DaoDe Jing) & 老子(Lao Tzu) 郭店竹简 (GuoDian Bamboo Slips) A World With 10,000 Objects Existence Nothingness, emptiness Name, concept Subtlety The Tao Beginning The Tao that can be expressed in words is not the true and eternal Tao… (Tao:1) 10,000 things, objects Heaven & earth, the world Dao Of Programming??? Info Books (September 1986) Addison-Wesley Professional; 1 edition (July 22, 2004) M & T Books (December 1990) Patrick Burns Hayden Books; 2 Pap/Dskt edition (July 1994) (December 16, 2012) Computing & Computer Programs The discipline of computing is … the systematic study of algorithmic processes that 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?’ Computer programs: sets of instructions written in programming languages that automate Real World information transformation processes Develop World” a software system is like building a “Virtual How To Structure Programs? 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 Procedural Vs. O-O Approaches Procedural approach, aka Structured Programming (mid-1960’s) top OO approach (since 1980’s) Procedures Or ways to process data Data Data and algorithms go together (in an object) No “top” unit Real World & Virtual World Real World (aka Problem Domain) Consists of objects (or entities) Employee ID Name Phone Brand Category Price Name Address Payment method Virtual World (aka Solution Domain) Specs of classes Objects created at runtime (when VW is alive) At The Very Beginning… Now let’s focus on the Virtual World Building the VW involves defining the classes that will be used to create objects at runtime 有物混成,先天地生。 S/W SystemThe Tao VW 寂兮寥兮,独立而不改。 周行而不殆,可以为天地母。 吾不知其名,强字之为道 ,There is a thing integratedly formed 强为之名曰大 。 And born earlier than Heaven and Earth. Objects 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) A Simplest Example class Dummy A {} {} What is the simplest Java class? Can it be used to create any object? Inheritance Add it here class DummyTest { public static void main (String[] x) { Dummy d = new Dummy(); } extends Object } But can this object be used? Add it here System.out.println(d); What happens behind the scene? Class Object is the “mother” of all Java classes What Does An Object Have? An object has a type Abstraction 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: o:Order they need to Know some information perform some operations -orderId=101 -orderDate=1/1/11 +calOrderTotal() … How To Identify Classes & Assign Responsibilities? One easy choice Just use one GOD CLASS, which R. Martin (2003) Prentice Hall UML for Java Programmers 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 天地不仁,以万物为刍狗… Heaven and Earth have no favorite, They deem all objects as equally common. (Tao:5) Keep It Simple 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 五色令人目盲; 五音令人耳聋; 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) The Single Responsibility Principle Instead of using all-purpose objects, objects with a single responsibility are preferred Maintainability R. Martin (2002) Prentice Hall Agile Software Development How Do I Know What Objects To Use? Focus on the three types of objects Tao Tao begets begets the the One; One; The The One One consists begets the of Two Two;in opposition (the The Yin Twoand begets Yang); the Three; The The two Three begets begets the10,000 Three; things. All (Te:42) things connote the Yin and Yang. (Te:42) 道生一, 一生二, 二生三, 三生万物。 (Cont’d on next slide) A VW With 10,000 Objects CustomerEntity Customer CustomerForm OrderControl Actors OrderEntity SalesRep SalesRepForm OrderFulfillControl 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 Interactions Of Objects In The VW 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 changeGears(5) passing myBike:Bike me:Rider A message has A recipient An operation requested Additional information to carry out the operation Java online tutorials: What Is an Object? http://docs.oracle.com/javase/tutorial/java/concepts/object.html Let The Server Do It Choose to (take) no action One way to get help is to delegate The Delegation client object will “do nothing” (无为) itself (Take) no action It delegates reuses the service from the “server” object that implements it The client and server roles are specific for each episode server client objectA objectB client server Contract Between Objects Encapsulation 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". 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 Reusability Interfaces and abstract classes are commonly used in design patterns [A design] pattern describes a problem Alexander, Christopher (1977) Oxford University Press. A Pattern Language: Towns, Buildings, Construction. 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. A Common Pattern: Arch A Software Pattern: Composite Make Parts(components) Into A Unit (Composite) Client Composite pattern Another Software Pattern: Façade 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 MyGamePanel AbstractTank KeyHandler +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) Software Is Soft Favor flexibility over rigidity 人之生也柔弱,其死也坚强。 万物草木之生也柔脆,其死也枯槁。 故坚强者死之徒,柔弱者生之徒。 …… 强大处下,柔弱处上。 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) Design Smells: The Odors Of Rotting Software Rigidity Fragility Immobility Viscosity Needless complexity Needless repetition Opacity R. Martin (2003) Prentice Hall UML for Java Programmers 五色土(Earth In 5 Colors)