See Design Patterns, part one; Introduction to get the definitions and the laws of design patterns.

Structural Patterns define the way objects are connected with other objects so changes in the system do not affect these connections. Again, the low coupling law is the base for the app designs.

There are eight patterns:

  • Adapter – converts the interface of a class into another interface clients expect;
  • Bridge – separates an object’s interface from its implementation;
  • Façade – provides a more comfortable way to deal with a library or bundle of resources;
  • Decorator – adds objects responsibilities dynamically;
  • Flyweight – reducing the number of objects;
  • Proxy – an object representing other object;
  • Composite – to compose objects into tree structures to represent part-whole hierarchies;
  • Private Class Data – restricts accessor/ mutator access.

 

Adapter

Adapter converts the interface of a class into another interface clients expect, as any cable adapter you may encountered. Usually you want to reuse an old component that offers compelling functionality, but its “view of the world” is not compatible with the architecture of the system currently being developed.

Reuse has always been painful and elusive, against everything that theorists said – OOP is about reuse, reuse and reuse. There is always something not quite right between the old and the new. It may be physical dimensions or misalignment. It may be timing or synchronization. It may be unfortunate assumptions or competing standards.

Adapter functions as a wrapper or modifier of an existing class. It provides a different or translated view of that class. Adapter provides a different interface to its subject, Proxy provides the same interface. Adapter makes things work after they’re designed, Bridge makes them work before they are.

Proxy

Proxy speaks for other object. It is used to support distributed, controlled or intelligent access.

You can think about Proxy when

  • you need to delay the creation of a costly object until it is needed;
  • you have to control communication with remote objects;
  • you want to control access to objects;
  • you may want smart reference to an object – caching, locking etc.

Façade

Façade wraps a complicated subsystem with a simpler interface. An Operating System is a Façade for hardware. An eCommerce site is a Façade for ordering, billing and shipping the goods.

The Façade design captures the complexity and collaborations of the component and delegates to the appropriate methods. The client uses (is coupled to) Façade only.

Adapter and Façade are both wrappers. Façade defines a new interface, whereas Adapter uses an old interface. Adapter wraps a single object, while Façade routinely wraps multiple objects. Façade could front-end a single complex object and Adapter could wrap several legacy objects.

Bridge

The goal of Bridge is to allow you to structure your code so that you can easily add new kinds of front-end objects which are implemented with functionality in new kinds of back-end objects. Therefore, both handle and body can vary independently of each other.

Bridge is really a code-organization tool that allows you to add in any number of new front-end services that implement their operations by delegating to any number of back-end options. Using Bridge, you can accomplish this without the normal combinatorial explosion of possibilities that would otherwise occur.

Flyweight

Some programs require a huge number of objects that have some shared state among them. Each flyweight object can be divided into two pieces: the state-dependent (extrinsic) part and the state-independent (intrinsic) part. Then put them to share that intrinsic part to save memory. The key here is the HUGE number of objects. The simplicity law ask us to get it only if the memory is a pain.

Decorator

Too many classes! Decorators provide a flexible alternative to subclassing for extending functionality. Instead to create all combination at design time, you will add dynamically behavior or state to individual objects at run-time.

Note that this pattern allows responsibilities to be added to an object, not methods to an object’s interface. The interface presented to the client must remain constant as functionalities are specified.

There are some tradeoffs: coding is more complicated when using Decorators. So, try compromise – there will always be certain combinations that you will describe regularly, which would often work exactly as they are, but if you wanted to decorate them then you would use decorators to make the modifications. This will keep your code readable and maintainable.

Private Class Data

  • Encapsulate class data initialization!
  • Control write access to class attributes!
  • Separate data from methods that use it!

Yes, these are the first lessons in coding. And yes, it is about a design pattern, if you check the definition of Design Patterns.

Composite

The common objects relationship in computing – Containers that contain Containees, each of which could be a Container.

Divide your domain concepts into container classes and containee classes. Create a lowest common denominator interface that makes your containers and containees interchangeable. It should specify the behavior that needs to be exercised uniformly across all containee and container objects.

In a larger Composite problems could arise as recursion is always expensive.

G.

 

See Design Patterns, part one; Introduction to get the definitions and the laws of design patterns.

Creational Patterns are used to isolate the details of object creation. They will help us stay under the law of low coupling. App controllers will be decoupled of object types and they will not change as new types arrived. Creational Patterns are about how an object can be created.

There are identified six patterns (more or less an exact number, they have not yet standardized the patterns):

  • Singleton – only one instance can exist;
  • Factory Method – creates an instance of several derived classes;
  • Abstract Factory – creates an instance of several families of classes;
  • Builder – to separate the construction from the representation to allow multiple different representations;
  • Object Prototyping – creates objects based on a fully initialized instance (copy or clone);
  • Object Pool – avoid expensive acquisition and recycle.

 

Singleton

You may use Singletons when your application needs one, and only one, instance of an object. The first intent is to prevent the client programmer from having any way to create an object except the ways you provide.

There are many examples of Singleton usage:

  • any Factories as they are a global need;
  • logging points;
  • configuration classes;
  • lookup service;
  • accessing resources in shared mode.

There are several implementations of this pattern, but all make all constructors private and create at least one constructor to prevent the compiler from synthesizing a default constructor. Some of the theorists ask for lazy initialization, but I do not consider it a must. The Singleton design pattern is one of the most inappropriately used patterns. Singletons are intended to be used when a class must have exactly one instance, no more, no less. Programmers frequently use Singletons in a misguided attempt to replace global variables, why the hell are you using global data?

Object Pool

Sometimes objects are expensive (eg. db connections) and you can think about recycling them. This is a technique to create a limited pool of objects.

As a Factory Design it is a Singleton. Factories create the objects and then forget about them, but The Pool manages objects lifetime and keeps track of them.

Factory Method

The new operator is considered harmful and this pattern makes the difference between requesting an object and creating one. The new operator always creates an object and fails to encapsulate object creation. Factory Method enforces that encapsulation and allows an object to be requested without inextricable coupling to the act of creation.

When you need to add new types to the system the first idea is to use polymorphism as new types may be added without disturbing existing code. If the code that creates objects is distributed throughout your application, you must chase down all the points of your code where type matters and you will broke the night compilation. The solution is to force the creation in one place through a common Factory. As you get experience I suspect that Factories will be the most universally useful kinds of design patterns.

Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward more abstract as the designer needs more flexibility.

As OOD has two fundamental ways to relate classes – inheritance and composition here you may also think as two ways to create objects – Factory Method use creation through inheritance and Prototyping creation through delegation. Prototype doesn’t require subclassing, but it does require an Initialize operation. Factory Method requires subclassing, but doesn’t require Initialize.

The most used example is the injection mold press. Manufacturers of plastic toys process plastic molding powder and inject the plastic into molds of the desired shapes. The class of toy (car, action figure etc.) is determined by the mold.

Abstract Factory

Provide an interface for creating families of related or dependent objects without specifying their concrete classes. It is a natural evolution of Factories as modern applications can run on different platforms. If an application is to be portable, it needs to encapsulate platform dependencies (including UI, OS, DBs etc). The application must replace the entire family of products simply by instantiating a different concrete instance of the abstract factory. In other words, The Abstract Factory is a super-factory which creates other factories (Factory of factories).

Follow the law of simplicity and do not consider platform independency if not needed. To get one more abstraction layer just because you do not choose what DB to build on it is a very bad design. First to do is a Pro-Cons matrix of “platforms” versus “products”, sometimes it is easier and cheaper to build products on platforms.

Builder

The goal of Builder is to separate the construction from the representation to allow multiple different representations. The construction process stays the same, but the resulting object has different possible representations. The differentia of Builder in Factories is that it has multiple steps in creating an object and those steps are accessed externally to the Builder object.

An old lady thought me that McDonalds is using a builder process. Whether a customer orders a hamburger, cheeseburger or chicken the process is the same. The employee at the counter directs the crew to assemble a main item, side item and toy. These items and the drink are then placed in a bag.

Prototyping

The mitotic division of a cell – resulting in two identical cells – is an example of a prototype. The new operator is considered harmful and then we can consider build our own Factory that cache prototypical objects for use as breeders of all future instances. javascript is the common object-oriented language that relays on Prototyping.

A problem with the other Factory designs above is that they require a central location where all the types of the objects must be known: inside the factory( ) method. If new types are regularly being added to the system, the factory( ) method must be changed for each new type. When you discover something like this, it is useful to try to go one step further and move all of the information about the type, including its creation, into the class representing that type. This way, the only thing you need to do to add a new type to the system is to inherit a single class.

 

At the end I must emphasize that sometimes Creational Patterns are competitors: there are cases when either Prototype or Abstract Factory could be used properly. At other times they are complementary: Abstract Factory might store a set of Prototypes from which to clone and return product objects.

G.

 

  • On average, each employee uses 17 cloud apps, but many organizations don’t know what is in use, or whether these apps meet security, privacy and compliance requirements
  • In 91% of organizations, employees grant their personal accounts access to the organization’s cloud storage
  • 70% of the organizations allow cloud admin activity from non-corporate, unsecured networks
  • 75% of privileged cloud accounts are not in use. These accounts might be eating up the cost of a license, or worse, increasing the attack surface of the organization
  • On average, an organization shares 13% of its files externally, of which 25% are shared publicly

Vremuri bune să te-apuci de furat secrete comerciale!

G.