Unformatted text preview:

ModulesSeparation of Concerns PrincipleLanguage constructs for modularityAda: PackagesPrivate parts and information hidingClient can use only visible interfaceImplementationImplementation of QueuesPredicates on queuesOperator OverloadingSyntactic sugar: use clausesSugar can be indispensableC++ : namespacesDependencies between modules in C++Header files are visible interfacesImplementation of namespaceSyntactic sugar: using declarationsWholesale import: the using directiveVisibility: the Koenig device (on your own)The Koenig device:details (on your own)LinkingInclude directives = multiple declarationsModules in JavaDependencies between classesAspect-Oriented ProgrammingAspectJ example: logging http://www.developer.com/java/other/article.php/3109831AspectJ example: join conceptAspectJ example: pointcut and adviceAspectJ example: aspectAspectJ example2: trace every method callSummaryModules•Program is built out of components.•Each component defines a set of logically related entities (strong internal coupling)•A component has a public interface that defines entities exported by the component.•A component may depend on the entities defined in the interface of another component (weak external coupling)•The component may include other (private) entities that are not exportedSeparation of Concerns Principle•Overall program calls subcomponents for specialized jobs.•Each component has its own private way of doing things.•Process repeats itself recursively.•Large engineering projects built this way.•Software has one more twist; need to integrate code without global disruption.Language constructs for modularity•Need to declare:–public interface–private implementation–dependencies between modules–naming conventions of imported entities–relationship between modules and files•To each his own:–Ada : package declaration and body, with-clause, use clause–C : header files, #include directives–C++ : header files, namespaces, #include and using declarations / directives–Java: packages, import statementsAda: Packagespackage Queues is Size : constant Integer := 1000; type Queue is private; -- information hiding procedure Enqueue (Q : in out Queue, Elem : Integer); procedure Dequeue (Q : in out Queue; Elem : out Integer); function Empty (Q : Queue) return Boolean; function Full (Q : Queue) return Boolean; function Slack (Q : Queue) return Integer; function “=“ (Q1, Q2 : Queue) return Boolean; -- overloaded operatorprivate … -- concern of implementation, not of client of packageend Queues;Private parts and information hiding package Queues is … -- visible declarations private type storage is array (integer range <>) of integer; type queue is record front : integer := 0; -- next element to remove back : integer := 0; -- next available slot contents : storage (0 .. Size - 1); -- the actual contents Num : Integer := 0; end record;end Queues;Client can use only visible interfacewith Queues; use Queues; with Text_IO;procedure Test is Q1, Q2: Queue; -- local objects of a private type Val : Integer;begin Enqueue (Q1, 200); -- visible operation for J in 1 .. 25 loop Enqeue (Q1, J); Enqueue (Q2, J); end loop; Deqeue (Q1, Val); -- visible operation if Q1 /= Q2 then Text_IO.Put_Line (“lousy implementation”); end if;end Test;Implementation•Package body holds bodies of subprograms that implement interface•Package may not require a body: package Days is type Day is (Mon, Tue, Wed, Thu, Fri, Sat, Sun); subtype Weekday is Day range Mon .. Fri; Tomorrow : constant array (Day) of Day := (Tue, Wed, Thu, Fri, Sat, Sun, Mon); Next_Work_Day : constant array (Weekday) of Weekday := (Tue, Wed, Thu, Fri, Mon); end Days;Implementation of Queuespackage body Queues is procedure Enqueue (Q : in out Queue; Elem : Integer) is begin if Full (Q) then -- need to signal error: raise exception else Q.Contents (Back) := Elem; end if; Q.Num := Q.Num +1; Q.Back := (Q.Back + 1) mod Size; end Enqueue;Predicates on queues function Empty (Q : Queue) return Boolean is begin return Num = 0; -- client cannot query Num directly end Empty; function Full (Q : Queue) return Boolean is begin return Num = Size; end Full; function Slack (Q : Queue) return Integer is begin return Size - Num; end Slack;Operator Overloading function “=“ (Q1, Q2 : Queue) return Boolean is begin if Q1.Num /= Q2.Num then return False; else for J in 1 ..Q1. Num loop -- check corresponding elements if Q1.Contents ( (Q1.Front + J -1) mod Size) /= Q2.Contents ( (Q2.Front + J -1) mod Size) then return False; end if; end loop; return True; -- all elements check equal end if; end “=“; -- operator “/=“ is implicitly declared as the negationSyntactic sugar: use clauses•Visible part of package is akin to record: visible entities can be denoted with an expanded name: with text_io; … text_io.put_line (“no ambiguity here”); package L_IO is new text_io.Integer_Io (Long_Integer);•Use clause makes name of entity directly usable: with text_io; use text_io; … put_line (“clear enough”);Sugar can be indispensable with queues; procedure test is Q1, Q2: Queues.Queue; … if Q1 = Q2 then ... -- error: “=“ is not directly visible -- must write instead: Queues.”=“ (Q1, Q2)•Two solutions:use queues; -- import all entitiesuse type queues.queue; -- import operators onlyC++ : namespaces•Late addition to the language• An entity requires one or more declarations and a single definition.•A namespace declaration can contain both, but definitions may also be given separately. namespace Nam { int f (int); /* declaration of f */ } int Nam::f (int) { /* definition provides body of function */ }Dependencies between modules in C++•Files have semantic significance: #include directives means textual substitution of one file in another•Convention is to use header files for shared interfaces•The argument of an #include directive need not be a namespace, but often is. #include <iostream> // import declarations int main () { std::cout << “C++ is really different\n”; } // the standard library can always be namedHeader files are visible interfaces namespace Stack { // in file stack.h


View Full Document

NYU CSCI-GA 2110 - Modules

Download Modules
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Modules and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Modules 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?