1 / 15

Source: Per S Laursen. et al

Source: Per S Laursen. et al. 23 Patterns. Authors examined a large base of high-quality code , to identify how common software design problems were solved No theoretical work ; pattens are discovered , not invented

josie
Download Presentation

Source: Per S Laursen. et al

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Source: Per S Laursen. et al

  2. 23 Patterns • Authors examined a large base of high-qualitycode, to identifyhowcommon software design problems weresolved • No theoreticalwork; pattens arediscovered, not invented • 23 commonstrategies for solving design problems wereidentified, and dubbeddesign patterns

  3. 23 Patterns • Design patterns fall in three categories • Creational: How are objects created • Structural: How are objects combined • Behavioral: How do objects interact

  4. 23 Patterns • Creational patterns • Singleton • Prototype • Builder • Factory Method • Abstract Factory

  5. Creational patterns • Singleton • Ensures a class has only one instance, and provides a global point of access to it. • Factory method • Define an interface for creating an object, but let subclasses decide which class to instantiate. Patterns in programming

  6. Singleton • Idea • Exactly 1 instance of a class • A single global point of access for all clients • Example class A { private static A INSTANCE = new A(); // eager initialization public static A getInstance() { return INSTANCE; } private A() { … } // more methods } • Variations • Lazy initialization • The instance is not created before it is needed. • If the instance is never needed, it does not use resources. • getInstance() method might need synchronization. • Protected constructor • Makes it possible to subclass the singleton class Patterns in programming

  7. Factory method • Idea • Can return an instance of the specified class, or one of its subclasses • A constructor can only “return” an instance of its own class • Does not have to construct a new object • A constructor must create a new object • Can have any name • A constructor must have the same name as the class Patterns in programming

  8. Behavioral patterns • Observer • Define a one-to-many dependency between object so that when one object changes state, all its dependents are notified (and updated) automatically. • Template method • Define a skeleton of an algorithm, deferring some steps to subclasses. Patterns in programming

  9. Observer • Idea • Decouples otherwise dependent classes. • Observers register with an observable class. • When something happens (like change of state) in the observable, all observers are notified. • Usage • Swing / AWT • Visual components send events to listeners. • JavaBeans • Components sends property change events to listeners. • Variations • Data is sent with the notification. • Notification does not contain data. Data is fetched from the observable using get-methods. Patterns in programming

  10. Template method • Idea • Encapsulate part of an algorithm in an abstract method implemented by subclasses. • Example abstract class SuperClass { abstract protected T doPartOfSomething(); public void doSomething() { …doPartOfSomething(); … } } class SubClass1 extends SuperClass { protected T doPartOfSomething() { … } } • Usage • Often used in frameworks • Users of the framework are supposed to extend the abstract classes. Patterns in programming

  11. 23 Patterns • Structural patterns • Adapter • Bridge • Composite • Decorator • Facade • Flyweight • Proxy

  12. 23 Patterns • Behavioral patterns • Chain of responsibility • Command • Interpreter • Iterator • Mediator • Memento • Observer • State • Strategy • Template Method • Visitor

  13. Design Patterns • Are distilled knowledge • Learned from practical experience • Solve a problem in a given context • Provides a common vocabulary • Is not the solution to all SW design issues… • …but almost 

  14. Pattern categories • Architectural patterns • High level: Affects a whole application or a major part of an application. • Design patterns • Intermediate level: Affects a few classes. • Idioms • Low level: Affects a single class or part of a single class. • Closing a resource like a file or network connection • try { … use resource … } finally { close resource } • Testing an expected exception in JUnit try { methodThatThrowsExceptions(); fail(…); } catch (SomeException ex) { /* ignore */ } Patterns in programming

More Related