Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

SitePoint PHP Blog:
Flyweight Design Pattern and Immutability: A Perfect Match
Oct 22, 2015 @ 16:56:32

The SitePoint PHP blog has a tutorial they've posted (from author Andrew Carter) looking at the Flyweight design pattern and immutability, how they're a "perfect match". The flyweight pattern makes it possible to reuse objects after they've been created with one requirement: they must be immutable.

The fundamental principle behind the flyweight pattern is that memory can be saved by remembering objects after they have been created. Then, if the same objects need to be used again, resources do not have to be wasted recreating them. [...] You can think of the flyweight pattern as a modification to a conventional object factory.

One important feature of flyweight objects is that they are immutable. This means that they cannot be changed once they have been constructed. This is because our factory can only guarantee that it has remembered the correct object if it can also guarantee that the object it originally created has not been modified.

The post includes code examples of how to implement the pattern with a simple File object that fetches data from a file when created. He then creates the factory class, with a getFile method that takes in the path and creates the immutable File object from it. It's then stored in an internal array for potential reuse later. He also talks about how the pattern could be useful for handling enumeration objects and how you can use it to build out "type" objects.

tagged: flyweight designpattern immutable object factory tutorial type enumeration

Link: http://www.sitepoint.com/flyweight-design-pattern-immutability-perfect-match/

DevShed:
Generating Web Pages with the Flyweight Pattern in PHP 5
Mar 05, 2007 @ 18:19:00

DevShed concludes their look at the Flyweight pattern with this new tutorial - the second part focusing on building an actual application with the pattern implemented.

In this final part of the series, I'm going to teach you in a step-by-step format how to create in PHP 5 a flyweight class to balance the instantiation of objects that will be used to generate web documents on the fly. Hopefully, by the end of this article, you should have acquired a considerable background in how to apply the flyweight pattern in a real-world situation.

They create a simple application that generates dynamic HTML elements - DIV tags - with a simple interface to define things like content, name, and ID. Their Flyweight factory class creates and manages the number of DIVs that are created, blocking requests for any more. Finally, they apply it, showing the creation of a simple web page with multiple DIVs in it.

tagged: tutorial flyweight designpattern html element dynamic application tutorial flyweight designpattern html element dynamic application

Link:

DevShed:
Generating Web Pages with the Flyweight Pattern in PHP 5
Mar 05, 2007 @ 18:19:00

DevShed concludes their look at the Flyweight pattern with this new tutorial - the second part focusing on building an actual application with the pattern implemented.

In this final part of the series, I'm going to teach you in a step-by-step format how to create in PHP 5 a flyweight class to balance the instantiation of objects that will be used to generate web documents on the fly. Hopefully, by the end of this article, you should have acquired a considerable background in how to apply the flyweight pattern in a real-world situation.

They create a simple application that generates dynamic HTML elements - DIV tags - with a simple interface to define things like content, name, and ID. Their Flyweight factory class creates and manages the number of DIVs that are created, blocking requests for any more. Finally, they apply it, showing the creation of a simple web page with multiple DIVs in it.

tagged: tutorial flyweight designpattern html element dynamic application tutorial flyweight designpattern html element dynamic application

Link:

DevShed:
Introducing the Flyweight Pattern with PHP 5
Feb 26, 2007 @ 18:41:00

DevShed charges right ahead with its emphasis on design patterns in PHP with a new start of a new series today that looks at the Flyweight pattern.

Among the considerable variety of structural design patterns that can be implemented with PHP 4 (and PHP 5, by the way), there's one in particular that deserves special attention. It's easy to apply in the context of a given web application, and it offers remarkable functionality when it comes to preventing the unnecessary instantiation of different classes. This two-part series covers that pattern.

As they explain, the Flyweight pattern helps to keep your code light and simple by preventing the instantiation of unneeded objects and resources. They go with a HTML form example, illustrating how to prevent a submit button or an input button's class to be reinitialized every time a new form field is needed.

tagged: flyweight pattern php5 designpattern tutorial form element flyweight pattern php5 designpattern tutorial form element

Link:

DevShed:
Introducing the Flyweight Pattern with PHP 5
Feb 26, 2007 @ 18:41:00

DevShed charges right ahead with its emphasis on design patterns in PHP with a new start of a new series today that looks at the Flyweight pattern.

Among the considerable variety of structural design patterns that can be implemented with PHP 4 (and PHP 5, by the way), there's one in particular that deserves special attention. It's easy to apply in the context of a given web application, and it offers remarkable functionality when it comes to preventing the unnecessary instantiation of different classes. This two-part series covers that pattern.

As they explain, the Flyweight pattern helps to keep your code light and simple by preventing the instantiation of unneeded objects and resources. They go with a HTML form example, illustrating how to prevent a submit button or an input button's class to be reinitialized every time a new form field is needed.

tagged: flyweight pattern php5 designpattern tutorial form element flyweight pattern php5 designpattern tutorial form element

Link:


Trending Topics: