Posts

Showing posts with the label Structural Patterns

Design Patterns Overview

Image
Design Patterns In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern isn't a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Uses of Design Patterns Design patterns can speed up the development process by providing tested, proven development paradigms. Effective software design requires considering issues that may not become visible until later in the implementation. Reusing design patterns helps to prevent subtle issues that can cause major problems and improves code readability for coders and architects familiar with the patterns. Often, people only understand how to apply certain software design techniques to certain problems. These techniques are difficult to apply to a broader range of problems. Design patterns provide general solutions, documented in a format that doesn...

Proxy Design Pattern

Image
? prev next ? Intent Provide a surrogate or placeholder for another object to control access to it. Also known as Surrogate Explanation Real world example Imagine a tower where the local wizards go to study their spells. The ivory tower can only be accessed through a proxy which ensures that only the first three wizards can enter. Here the proxy represents the functionality of the tower and adds access control to it. In plain words Using the proxy pattern, a class represents the functionality of another class. Wikipedia says A proxy, in its most general form, is a class functioning as an interface to something else. A proxy is a wrapper or agent object that is being called by the client to access the real serving object behind the scenes. Use of the proxy can simply be forwarding to the real object, or can provide additional logic. In the proxy extra functionality can be provided, for example caching when operations on the real object are resource intensive, or checking preconditions ...

Flyweight Design Pattern

Image
? prev next ? Intent Use sharing to support large numbers of fine-grained objects efficiently. Explanation Real world example Alchemist's shop has shelves full of magic potions. Many of the potions are the same so there is no need to create new object for each of them. Instead one object instance can represent multiple shelf items so memory footprint remains small. In plain words It is used to minimize memory usage or computational expenses by sharing as much as possible with similar objects. Wikipedia says In computer programming, flyweight is a software design pattern. A flyweight is an object that minimizes memory use by sharing as much data as possible with other similar objects; it is a way to use objects in large numbers when a simple repeated representation would use an unacceptable amount of memory. A Flyweight Pattern says that just " to reuse already existing similar kind of objects by storing them and create new object when no matching object is found ". Advan...

Facade Design Pattern

Image
? prev next ? Intent It is belongs to s tructural  design patterns catalog. Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use. Explanation Real world example How does a goldmine work? "Well, the miners go down there and dig gold!" you say. That is what you believe because you are using a simple interface that goldmine provides on the outside, internally it has to do a lot of stuff to make it happen. This simple interface to the complex subsystem is a facade. In plain words Facade pattern provides a simplified interface to a complex subsystem. Wikipedia says A facade is an object that provides a simplified interface to a larger body of code, such as a class library. Structure Class Diagram Source code (First Example) Let's build a Home Theater using Facade Pattern. Here Facade class treats the home theater components as a subsystem, and calls the subsystem to implement it's...