You are on page 1of 17
The Progress of Abstraction Monday, September7,2020 453 PM + "bicycles for minds" - Steve Jobs referring to computers as mind amplification tools + 00P is part of this movement towards using the computer as an expressive medium + the complexity of the problem to be solved is directly related to the kind and quality of the abstraction ‘+ assembly languages -> SMALL ABSTRACTION © the resulting imperative lang. (FORTRAN, BASIC, C) focus the thinking in terms of computer rather than the structure of the problem + alternative: model the problem, NOT the machine © early lang. - LISP: “all problems are ultimately lists" - ALP: "all problems are algorith mi - Prolog: chain of decisions + 00P: a step further ‘+ provides tools to represent elements in the problem space } solutions to particular kinds of problems objects elements Problem Space Solution Space + each object is ike alittle computer; it has a state & operations + Alan's Kay5 basic characteristics of Smalltalk (1st OOP lang.) 1) Everything is an object 2) A program is a bunch of objects telling each other what to do by sending messages 3) Each object has its own memory made up of other objects 4) Every object has a type 5) All object of a particular type can receive the same message + "Aan object has a state, behavior and identity" - Booch © state-> internal data © behavior-> methods © identity > unique address memory An Object has an Interface aa3 Monday, Septem + Simula-67 introduced the keyword class ‘© created for developing simulations - the classic "bank teller problem" ‘© numerous tellers, customers, accounts, transactions, unit of money --> objects => classes of objects + dlasses are an abstract data type + they work exactly like built-in types ‘© createa variable of type (like objects/instances) ‘© manipulate it (by sending messages/requests) + createa class to fit a problem rather than being forced to use an existing one + once a class is established, many objects can be created out of it + 00P's challenge: create a one-to-one mapping between elements of real problems -> solution's space + the requests one can make to an object are defined by its interface Type name Tight (dass) — on ono, GH vn sscitin inetace 7 brighten() lass: a box dim() write any data & methods cared to describe Light It = new Light(); It-on; + the code that implements the request + hidden data = implementation An Object Provides Services 11:57PM Monday, Septem + object can be seen as “service providers" - provides services to the user + programmer's goal: produce (or locate in existing code libraries) a set of objects that provide the ideal services to solve the problem “What services would those object provide, and what object would they need to fulfil their obligation?" + this thinking has a benefit: improves the cohesiveness + high cohesion - fundamental quality of software design ‘© various aspects of a software component "fit together” well + ina good 00 design, each object does one thing well, but doesn't try to do too much The Hidden Implementation Tuesday, September 8, 2020 12:04 AM + dassification ‘© class creators: new data types; collect a toolbox full of classes to use for rapid application development © client programmers: the class consumers who use the data types in their application + aneed for access control arises, s:t an uninformed client programmer to not alter the data + dass creator's job: hide the implementation + access control ‘© protect the internal operation of the data type ‘© but NOT part of the interface the user needs ‘© allows the library designer to change the internal workings of the class ‘© Bexplicit keywords * public - available for everyone + private ‘5 noone can access the element except the creator _ brick-wall between the programmer & client-programmer ‘5 compiler-time error + protected acts like private 5 however, an inheriting class has access to protected members, but NOT private + +default itis there by default also called package access 5 dlasses can access the members of other classes within the same package 13 outside of the package, the same members appear to be private Reusing the Implementation Tuesday, September 8, 2020 12:15AM + code reuse is a great advantage for OOP + itcan behardto achieve + reusea class in 2 ways: 1) use an object of that class directly - simplest 2) place an object of that class in another - called "creating a member object" + the 2nd way to reuse a class is based on composing a class from existing ones COMPOSITION + itisa*has-a” relationship ‘+ if its used dynamically it is called AGGREGATION car -—— Engine ‘Acar hasan engine. + the new members of the new class are typically private + the members object can also be changed at runtime, dynamically changing the behavior + inheritance doesn't allow this ability + however, abusing inheritance can lead to complicated designs + look at composition first, then inheritance Inheritance Tuesday, September 8, 2020 12:31 AM + clone a class, and then make additions and modifications to the clone + the original class-> the base/parent/super class + the doned class -> the derived/inherited/child/sub class Base + ifthe base class changes, the child class also changes ‘+ 2types can have common characteristic and behaviors, but one may contain Extends and handle more messages + inheritance allows this similarity between classes by using the base & derived type Derived + through inheritance, we build a type hierarchy, which expresses the problem interms of its type + dassic"shape" example Shap aren) serase() seove() tgetColon() Hetcolor() Adircleis-a shape. A cirete ‘Square Teiansie + when you inherit from an existing type => you create a new type + new type = derived class = same type as base class, ‘© contains all the members of the existing type (the private ones are hidden and inaccessible) ‘© duplicates the interface of the base class + 2ways to differentiate between base/derived classes 1) add new methods to the derived 2), change the behavior of an existing base-class method = OVERRIDING 4) 2) Shape. arent) SeraseQ) = store) Eo, Seetcoton() an wEntcotor) Shape Triangle cireie teare Triangle eliprortzonts2() seran) saran0 saree fab seresed) feresed) ‘+ basically, a new definition for the method in the derived class is created ‘+ Should inheritance override only base class methods, and not add new methods that aren't in the base one? ‘+ pure substitution -> substitution principl he derived class is the same as the base one relationship + extending the interface: when new methods are needed--> "7s like-a” relationship Thermostat +Controls CoolingSystem lowerTemperatured) C0010) A AirConditioner HeatPump. cool) cool) +heat() + example: air conditioner for a house the house is wired with all the controls for cooling = it has an interface if the air conditioner breaks, and it is replaced with a heat pump which can both heat but also cool => the heat pump is like an air conditioner however the cooling system doesn't know about the heat capability => the interface of the new object has been extended, and the existing system knows only the original interface if the cooling system is modified into a temperature control system that includes heating than the substitution principle works (is-a) Interchangeable Objects with Polymorphism jay, Septemb) nesd 9,2020 2:29PM + regarding type hierarchies, it is desired to treat an object not as the specfic type that it its, but instead as its base type + = code independent of specific types + ex: methods that manipulate generic shapes, unconcerned if they're circles, squares, triangles; all shapes can be drawn, erased, moved etc,; these methods simply send a message to a shape object, unconcerned of how the object copes with the message + this code is unaffected by the addition of new types, + one can create a new subtype (pentagon) without modifying the methods that deal only with the gen shape + = greatly improves designs while reducing the cost of software maintenance + problem (treating derived types as their generic base types) ‘© ifamethod is telling a generic base type to do smth., the compiler cannot know at compile time precisely what piece of code will be executed © the programmer doesn't want to know what piece will be executed ‘© ex: the draw() method can be applied equally to a circle, square, triangle andthe object will execute the proper code + BirdController object works with generic Bird and doesn't know what exact type they are (convenient from BirdController POV, because it doesn't have to write special code to determine the exact type or behavior of Bird) Hfmove( is called (while ignoring the specific type of Bird) the right behavior occurs (Goose walks, flies, oF swims and Penguin walks or swims). What happens ow 2 move() is called? Sw BirdController i Bird +relocate() +move() Goose Penguin move() Fmove() + OOP: the compiler cannot make a function call in the traditional sense ‘© non-OOP compiler: early binding = the compiler generates a call to a specific function name, and the runtime system resolves this call to the absolute address of the code to be executed © 00P: late binding = the program cannot determine the address of the code until runtime = when one sends a message to an object, the code being executed isn't determined until runtime = the compiler does ensure that the methods exists & performs type checking on the arguments & return vales + ITDOESN'T KNOW the exact code to execute = uses a special bit of code in the space of the absolute call * it calculates the address of the method body, using information stored in the object ‘© other lang, => explicitly specify that you want a method to have late binding (C++: virtual) by default: methods are NOT dynamically bound ‘© JAVA: dynamic binding is the default behavior ‘+ ex. of polymorphism: write a single piece of code that ignores the specific details of type and talks only with base class => decoupled code from type-specific information ‘© anew type: Hexagon - added through inheritance (extensible program) Gircle circle = new Gircle(); void dosomething(Shape shape) { Triangle triangle = new Triangle(); shape. erase()); Line Tine = new Line() I= doSomething(circle) ; shape. drau(); doSomething( triangle) ; } doSomething(1ine) 5 this methods speaks to any NV the calls automatically works Shape => independent of the you'rea shope, know you can erase correctly, regardless of the specifictype of object it's and draw yourself doit, ond toke exact type of the objects drawing or erasing care ofthe details correctly a Circleis being passed into a methods that's expecting a Shape. © aCircle is-a Shape--> it can betreated as one by doSomething( ), that is, any message that doSomething( ) can send to Shape, a Circle can accept ‘© => UPCASTING = treating a derived type as though it were its base type = cast: casting into a mold "up: upper way of the inheritance diagram Shape “Upeasting" one could write the doSomething() method in which it explicitly tells it what to do in case of a Circle, Square etc. + problem: messy & every time a new kind of Shape appear it has to be changed rele Square Triangle + when a message is sent to an object, the object will do the right thing, even when upcastingis involved The Singly Rooted Hierarchy Tuesday, September 22,2020 7:47 PM + Should all classes ultimately be inherited from a single base class? + JAVA: yes & the ultimate base class is Object. + all objects in a singly rooted hierarchy have a common interface, so they are all ultimately the same fundamental type + Ct: you don't know that everythingis the same basic type ‘© backward-compatibility: fits the model of C better, less restrictive ‘© when a full OOP approach is needed one has to build its own hierarchy to provide the same convenience that's build into other OOP lang. © in any newly- acquired class library, some incompatible interface will be used © => effort & possible multiple inheritance to work the new interface © ifneeded this is an option, if working from scratch it can be unproductive + all objects in a singly rooted hierarchy can be guaranteed to have certain functionality + one can perform certain basic operations on every object in your system + all objects can easily be created on the heap, and argument passing is greatly simplified + assingly rooted hierarchy makes it easier to build a garbage collector {advantage of Java over C++) + type of object's information is guaranteed to bein all objects => you'll never end up with an object whose type you cannot determine Containers Tuesday, September 22,2020 8:54PM + How many objects are needed to solve a particular problem? + How long will they last? + How to store them? + How can you know how much space to create if that information isn't known until runtime? unknown answer + solution: create another type of object => CONTAINER ‘© new type of object which holds reference to other objects © can dothe same thing with an array ‘© the container will expand itself whenever necessary to accommodate everything you place inside it ‘© no need of pre knowing how many object one needs ‘© most OOP lang, come with a set of containers as part of the package = Cit: part of Standard C++ Library, often called STL (Standard Template Library) = Smalltalk: very complete set of containers = JAVA: numerous containers several kinds of List classes--> hold sequences 5 Maps (associative arrays) -> associate objects with other objects 5 Sets -> hold one of each type of objects ‘3 more components: queues, trees, stacks, ete. + if'a single type of container satisfies all the needs, there'd beno reason to have different kinds + 2reasons that one need a choice of container 1) containers provide different types of interfaces and external behavior = stack vs queue vs set vs list 2). different container have different eff + List: ArrayList vs LinkedList = different time cost = ex: randomly accessing an element ArrayList —> constant-time operation {same amount of time regardless of the element you select) LinkedList > expensive to move through the list to randomly select, longer if farther down the list = ex: insert an element in the middle--> cheaper in a LinkedList + even if thereis a need to later change to another list than the beginning one, itis easier to do so due to the abstraction via the interface List, with minimal impact on the code for certain operations Parameterized Types (Generics) Tuesday, September 22,2020 9:13 PM + before JAVA SES: container held the one universal type-> Object + SRH (Singly Rooted Hierarchy) --> everythingis an object => a container that holds Objects can hold anything + —> containers are easy to reuse + using such a container ‘© add object references to it & later ask for them back © since the container held only Objects, when an object reference was added it was upcasted to Object --> losing its character ‘© fetching it back--> Object reference, and NOT the reference of the placed type How do you turn it back into something that has the specifictype of the object that you put in the container? > cast down the inheritance hierarchy to a more specific type : one DOESN'T know what to downcast to + ex: upcasting (safe): Circle--> Shape downcasting: Object > 2? Shape--> Circle (Object is everything) + hardly safe to downcast unless one knows exactly what to deal with + not completely dangerous -> runtime error = exception + when an object fetch is performed, one must know what they are so to perform a proper downcast + solution: parameterized type (GENERICS) mechanism. lass that the compiler can automatically customize to work with particular types ‘© the container knows the type it holds, eliminating the need for downcast and mistakes ‘© ex: a parameterized container --> the compiler could customize it soit will accept only Shapes and fetch only Shapes AprayList shapes = new ArrayList (); Object Creation & Lifetime Tuesday, September 22,2020 9:42PM + critical issue: the way objects are created & destroyed + each objects require resources, especially memory in order to exist + object no longer needed --> cleaned up st the resources will be reused + simple prog, situation: create object -> use it > destroy it + complex prog. situation: ex --> air traffic management system ‘© firstly looks simple: create a container to hold airplanes -> create new airplane--> place it in the container for each airplane that enters the control zone cleanup: simply destroy the appropriate airplane object, what if another system has to record data about the planes? --> difficult problem: How can one possibly know when to destroy objects? when a part of the system is done with an object, some other part may not be --> complex problem, especially in C++ where you have to explicitly destroy an object oo ooe Where is the data for an object and how is the lifetime of the object controlled? + C++-> control of efficiency is the most import aspect -> choice 1) for maximum runtime speed: the storage and lifetime can be determined while the program is being written, by placing the objects on the STACK or in the static storage area ‘automatic/scoped variable * priority on the speed of storage allocation = creating storage on stack: single assembly instruction to move theSP down then back it up + sacrifice flexibility because the exact quantity, lifetime, type of object must be known * for problems like CAD, warehouse mgmt,, airtraffic control--> too restrictive 2), create object dynamically in a pool of memory called HEAP = until runtime one doesn't know the # of objects, lifetime, exact type = if'a new object is needed => make it on the heap at the point when it's needed = because storage is dynamically managed (at runtime) => longer time to allocate than the stack = time to create heap storage depends on the design of storage mechanism + JAVA: dynamic memory allocation (exclusively) -- except primitive types ‘© every time an object is created one uses the new operator to dynamically build an instance of that object + lifetime of an object, ‘© STACK: the compiler determines how long an object lasts and can automatically destroy it = Cit: one has to programmatically destroy the object > can lead to memory leaks ‘© HEAP: the compiler has no knowledge of its lifetime = JAVA: provides the garbage collector -> automatically discovers when and object, is no longer in use and release its memory Exception Handling: dealing with errors 23,2020 14:10 PM + error handling has always been a difficult issue + itis hard to design a good error-handling scheme + many languages ignore the issue, passing the problem on to the library designers + most error-handling schemes rely on programmer vigilance in following an agreed-upon convention that is not enforced by the language + it existed before OOP + exception handling wires error handling directly into the programming lang. and sometimes the 0s + exception = object that is "thrown" from the site of the error and can be" caught” by an appropriate exception handler designed to handle that particular type of error + exception handlingis like a different, parallel path of execution that can betaken when things go wrong + athrown exception is UNLIKE an error value that's returned from a method or a flagthat's set by a method in order to indicate an error condition + an exception cannot be ignored --> guaranteed to be dealt with at some point + JAVA: exception handling was introduced from the beginning ‘©. if the code isn't written to properly handle exception a compile-time error message will be received Concurrent Programming Thursday, September 24,2020 4:57 PM + the idea of handling more than onetask at atime + many problems require the program to stop what it's doing, and deal with some other problem and then return to the main one + initially, programmers with low-level knowledge of the machine wrote interrupt service routines, and the suspension of the main process was initiated through a hardware interrupt + > difficult and non-portable--> new type of machine movement was slow & expensive + sometimes interrupts are needed + usually we just need to partition the problem into separately running pieces (tasks), s.t the whole program can be more responsive + separately running pieces (tasks) = threads -> concurrency + ex:user interface ‘© by using threads, a user can press a button and get a quick response rather than waiting + tasks are just a way to allocate the time of a single processor + if the OS supports multiple processors > each task can be assigned to a different processor, and they can truly run in parallel + the program is logically divided into tasks, and if the machine has >= 1 processor -> the program runs faster + problem: shared resources + ex: 2 processes cannot simultaneously send information to a printer + solution: a tasks locks a resource--> complete its task -> release the lock, s.t another process can use the resource Java and the Internet Thursday, ber 24,2020 5:07 PM + JAVA solves programming problems for the World Wide Web 1. Client/server computing + central repository of information (data in DB) that must be distributed on demand to some set of people or machines + the central repository of information can be changed so that those changes will propagate out to all the information consumers + server: information repository + distributing software + machine where software and information resides + client: consumer machine--> the software of the consumer machine communicates with the server, fetches the information, processes it and displays it + problem: single server tryingto serve many clients at once ‘© generally a DB is involved, the designer "balances" the layout of data into tables for optimal use ‘©. system often allow client to insert new information into a server © --> ensure that one client's new data doesn't walk over another client's new data or the data isn't lost in the processes of adding it to the DB (transaction processing) ‘© hundreds of clients making requests of your server at any moment, so a small delay can be critical -> minimize latency, hard work to offload processing tasks, often to client machine, but sometime to other machines at the serve site--> using middleware, which also improves maintainability + problem: client software changes--> build, debugged, installed on client machines ==> complicated & expensive + distributing information on so many layers is quite enigmatic + however itis crucial for roughly 1/2 of all programming activities © taking order and credit-card transactions ‘© distribution of any kind of data- stock market, scientific, government 2. The Web as a Giant Server + web is actually a giant client/serve system + allthe servers and clients coexisting on a single network at once + one cares to connect and interact with one server at a time + initially: simple one-way process ‘© arequest of a server is made and it hands you a file, which your machine's browser software (client) would interpret by formatting it onto the local machine + later, afull client/server capability was wanted, st the client could feed information back to server, e.g. to do DB lookups on the server, add new information, place order --> requires special security measures + Web browser: one piece of information can be displayed on any type of computer without change ‘© original browser were primitive--> rapidly bogged down, not interactive, tended to clogup both the server ‘© to solvethis problem graphics standard have been enhanced to allow better animation and video within browsers--> incorporating the ability to run programs on the client end, under the browser side programming 3. Client-side programming ‘+ Web's initial server-browser design provided interactive content, but the interactivity was completely provided by the server + the server produced static pages for the client browser, which would simply interpret and display them + basic HTML (HyperText Markup Language) contains simple mechanism for data gathering: © text-entry/radio boxes, lists, buttons + this submission passes through CGI (Common Gateway Interface) provided on all Web servers + the text within the submission tells CGI what to do with it ‘+ most common action is to run a program located on the server in a directory that's typically called "cgi-bin" + these programs can be written in most languages ‘©. Perl: designed for text manipulation & is interpreted > can be installed on any server regardless of processor or 05 ‘© Python: greater power and simplicity Plug-ins most significant step forward in client-side programming, ‘© allows an expert programmer to develop extensions and add them to a browser without the permission of the browser manufacturer —> provides a "back-door" which allows the creation of new client-side programming languages add new functionality to the browser by downloading (only once) a piece of code that plugs itself into the appropriate spot in the browser writing a plug-in is not an easy task Scripting languages embed the source code for the client-side program directly into the HTML page, and the plug-in that interprets that language is automatically activated wile the HTML pages being displayed they are simply text, part of an HTML page they load very quickly as part of the single server hit required to procure the page trade-off: the code is exposed for everyone to see (and steal) scripting language without plug-in: JavaScript ‘© originally most Web browsers implemented JavaScript in a different way from others, and even from other versions of themselves ‘©. standardization came in the form of ECMAScript -> took a time for browser to catch up and Microsoft pushed its own agenda of VBScript ‘© generally, one must program in a kind of least-common-denominator form of JavaScript

You might also like