150 likes | 266 Vues
This article explores the identification and resolution of common software design problems through 23 recognized design patterns. Categorized into creational, structural, and behavioral patterns, these strategies provide insights into object creation, combination, and interaction. Key patterns such as Singleton, Factory Method, Observer, and Template Method are discussed alongside their implementations and variations. By sharing learned knowledge from practical experience, these patterns create a common vocabulary for developers to tackle software design challenges effectively.
E N D
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
23 Patterns • Design patterns fall in three categories • Creational: How are objects created • Structural: How are objects combined • Behavioral: How do objects interact
23 Patterns • Creational patterns • Singleton • Prototype • Builder • Factory Method • Abstract Factory
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
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
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
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
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
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
23 Patterns • Structural patterns • Adapter • Bridge • Composite • Decorator • Facade • Flyweight • Proxy
23 Patterns • Behavioral patterns • Chain of responsibility • Command • Interpreter • Iterator • Mediator • Memento • Observer • State • Strategy • Template Method • Visitor
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
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