DOC PREVIEW
AUBURN COMP 7700 - Creational Design Patterns

This preview shows page 1-2-3-22-23-24-45-46-47 out of 47 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

COMP 7700 – Creational Design Patterns1Chapter 7Creational Design PatternsCOMP 7700 – Creational Design Patterns2Process Phases Discussed in This ChapterRequirementsAnalysisDesignImplementationArchitectureFramework Detailed DesignxKey: = secondary emphasisx= main emphasisAdapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.COMP 7700 – Creational Design Patterns3Design PurposeCreate individual objects in situations wherethe constructor alone is inadequate.Design Pattern SummaryUse methods to return required objects.FactoryAdapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.Objects often aggregate other objects or must belongto subclasses determined at runtime.COMP 7700 – Creational Design Patterns4Factory Class ModelFactory design patternMyClass createObjectOfRequiredClass(): RequiredClass «create object»RequiredClassClientAdapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.Note: pattern is a form of delegation.A common motive for using Factory occurs when a baseclass object is required but the subclass to which it belongs is not known at runtime.COMP 7700 – Creational Design Patterns5Design Goal At Work:  Reusability and Corrrectness We want to write code about automobiles in general: Codethat applies to any make, exercised repeatedly (thusreliably).Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.COMP 7700 – Creational Design Patterns6Applicationof FactorydesignpatternFactory ExampleFord createAutomobile()Toyota createAutomobile()Automobile createAutomobile(): AutomobileClient«create object» «create object»Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.COMP 7700 – Creational Design Patterns7createObjectOfRequiredClass()Sequence Diagram for Factory:MyClass:ClientRequiredClass():RequiredClassAdapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.Correct Typing:class Ford extends Automobile{ Automobile createAutomobile() { return new Ford() } ......}Incorrect Typing:Ford createAutomobile()// return type not identical with// that of the createAutomobile()// we are overriding.COMP 7700 – Creational Design Patterns8Typical Output of E-Mail Generation ExampleAdapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.“From a single version of control code, generate mail messages tailored to various customers.”COMP 7700 – Creational Design Patterns9Design Goals At Work:  Correctness and Reusability We want to separate the code common to all types ofcustomers. We want to separate the specialized code thatgenerates e-mail for each type of customer. This makes iteasier to check for correctness and to reuse parts.Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.COMP 7700 – Creational Design Patterns10Application of Factory design patternCustomer getMessage()ClientsendMessage()Frequent getMessage()Returning getMessage()Curious getMessage()Newbie getMessage()MailMessagetextMailGenerationApplicationgetCustomerTypeFromUser()«setup»Factory: Email Generation ExampleAdapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.COMP 7700 – Creational Design Patterns11Factory Applied to getGraphics() in JavaFactory design patternComponent getGraphics(): Graphics «createobject»ClientGraphicsAdapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.The Java API makes extensive use of Factory. For example, the Factorymethod getGraphics() in the Java Component API class obtains a graphicsobject that draws on the specific Component object to which getGraphics()belongs. Box is a Container class allowing display in a horizontal direction or vertical direction. The API provides several facotry methods in Box which create particular kinds of Box objects such as public static Box createVerticleBox()public static Box createHorizontalBox()COMP 7700 – Creational Design Patterns12Key Concept:  Singleton Design Pattern -- when a class has exactly one instance.Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.COMP 7700 – Creational Design Patterns13Design PurposeEnsure that there is exactly one instance of aclass SS. Be able to obtain the instance fromanywhere in the application.Design Pattern SummaryMake the constructor of SS private; define aprivate static attribute for SS of type S; S; define apublic accessor for it.SingletonAdapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.COMP 7700 – Creational Design Patterns14Design Goal At Work:  Correctness Singleton enforces the intention that only oneUser object exists, safeguarding the applicationfrom unanticipated User instance creation.Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.COMP 7700 – Creational Design Patterns15Singleton: Class ModelSingleton Design PatternMyClass getSingletonOfMyClass(): MyClass Client1singletonOfMyClass«static»Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.First, statically define a single instance of class S.Making the constructor of the class MyClass private prevents thecreation of MyClass objects except by methods of MyClass itself (intheory).COMP 7700 – Creational Design Patterns16The Singleton Design Pattern -- applied to MyClass Define a private static member variable ofMyClass of type MyClassprivate static MyClass singletonOfMyClass = new MyClass(); Make the constructor of MyClass privateprivate MyClass() { /* …. constructor code …. */ }; Define a public static method to access thememberpublic static MyClass getSingletonOfMyClass(){ return singletonOfMyClass;}Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.COMP 7700 – Creational Design Patterns17Output for Singleton Experiment ExampleAdapted from Software Design:


View Full Document

AUBURN COMP 7700 - Creational Design Patterns

Download Creational Design Patterns
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 Creational Design Patterns 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 Creational Design Patterns 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?