Design Patterns
239 pág.

Design Patterns

DisciplinaEngenharia de Software I5.636 materiais57.596 seguidores
Pré-visualização50 páginas
script might also have more flexibility to implement the particular task required.
Several web-tier application frameworks implement the Front Controller pattern, among them:
\u2022\u2022 Ruby on Rails
\u2022 ColdBox, a ColdFusion MVC framework.
\u2022 Spring MVC, a Java MVC framework
\u2022 Yii, Cake, Symfony, Kohana, CodeIgniter and Zend Framework, MVC frameworks written with PHP
\u2022 Cairngorm framework in Adobe Flex.
\u2022 Microsoft's ASP.NET MVC Framework.
\u2022 Yesod web application framework [2] written in Haskell.
[1][1] Alur et al., p. 166.
[2] http:/ / www. yesodweb. com/
External links
\u2022 Bear Bibeault's Front Man\u2122 (http:/ / www. bibeault. org/ frontman/ ), A lightweight Java implementation.
\u2022 Alur, Deepak; John Crup, Dan Malks (2003). Core J2EE Patterns, Best Practices and Design Strategies, 2nd Ed..
Sun Microsystems Press. pp. 650pp. ISBN 0-13-142246-4.
\u2022 Fowler, Martin. Patterns of Enterprise Application Architecture (http:/ / www. martinfowler. com/ books.
html#eaa). pp. 560pp. ISBN 978-0-321-12742-6.
\u2022 Fowler, Martin. "Front Controller" (http:/ / www. martinfowler. com/ eaaCatalog/ frontController. html).
Retrieved February 2, 2008.
Flyweight pattern 109
Flyweight pattern
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. Often some parts of the object state can be shared, and
it is common practice to hold them in external data structures and pass them to the flyweight objects temporarily
when they are used.
A classic example usage of the flyweight pattern is the data structures for graphical representation of characters in a
word processor. It might be desirable to have, for each character in a document, a glyph object containing its font
outline, font metrics, and other formatting data, but this would amount to hundreds or thousands of bytes for each
character. Instead, for every character there might be a reference to a flyweight glyph object shared by every instance
of the same character in the document; only the position of each character (in the document and/or the page) would
need to be stored internally.
In other contexts the idea of sharing identical data structures is called hash consing.
According to a textbook Design Patterns: Elements of Reusable Object-Oriented Software,[1] the flyweight pattern
was first coined and extensively explored by Paul Calder and Mark Linton in 1990[2] to efficiently handle glyph
information in a WYSIWYG document editor, although similar techniques were already used in other systems, e.g.,
an application framework by Weinand et al. (1988).[3]
Example in Java
// Flyweight object interface
public interface CoffeeOrder {
 public void serveCoffee(CoffeeOrderContext context);
// ConcreteFlyweight object that creates ConcreteFlyweight 
public class CoffeeFlavor implements CoffeeOrder {
 private String flavor;
 public CoffeeFlavor(String newFlavor) {
 this.flavor = newFlavor;
 public String getFlavor() {
 return this.flavor;
 public void serveCoffee(CoffeeOrderContext context) {
Flyweight pattern 110
 System.out.println("Serving Coffee flavor " + flavor + " to 
table number " + context.getTable());
public class CoffeeOrderContext {
 private int tableNumber;
 public CoffeeOrderContext(int tableNumber) {
 this.tableNumber = tableNumber;
 public int getTable() {
 return this.tableNumber;
import java.util.HashMap;
import java.util.Map;
//FlyweightFactory object
public class CoffeeFlavorFactory {
 private Map<String, CoffeeFlavor> flavors = new HashMap<String, CoffeeFlavor>();
 public CoffeeFlavor getCoffeeFlavor(String flavorName) {
 CoffeeFlavor flavor = flavors.get(flavorName);
 if (flavor == null) {
 flavor = new CoffeeFlavor(flavorName);
 flavors.put(flavorName, flavor);
 return flavor;
 public int getTotalCoffeeFlavorsMade() {
 return flavors.size();
public class TestFlyweight {
 /** The flavors ordered. */
 private static CoffeeFlavor[] flavors = new CoffeeFlavor[100];
 /** The tables for the orders. */
 private static CoffeeOrderContext[] tables = new 
 private static int ordersMade = 0;
 private static CoffeeFlavorFactory flavorFactory;
Flyweight pattern 111
 public static void takeOrders(String flavorIn, int table) {
 flavors[ordersMade] = flavorFactory.getCoffeeFlavor(flavorIn);
 tables[ordersMade++] = new CoffeeOrderContext(table);
 public static void main(String[] args) {
 flavorFactory = new CoffeeFlavorFactory();
 takeOrders(&quot;Cappuccino&quot;, 2);
 takeOrders(&quot;Cappuccino&quot;, 2);
 takeOrders(&quot;Frappe&quot;, 1);
 takeOrders(&quot;Frappe&quot;, 1);
 takeOrders(&quot;Xpresso&quot;, 1);
 takeOrders(&quot;Frappe&quot;, 897);
 takeOrders(&quot;Cappuccino&quot;, 97);
 takeOrders(&quot;Cappuccino&quot;, 97);
 takeOrders(&quot;Frappe&quot;, 3);
 takeOrders(&quot;Xpresso&quot;, 3);
 takeOrders(&quot;Cappuccino&quot;, 3);
 takeOrders(&quot;Xpresso&quot;, 96);
 takeOrders(&quot;Frappe&quot;, 552);
 takeOrders(&quot;Cappuccino&quot;, 121);
 takeOrders(&quot;Xpresso&quot;, 121);
 for (int i = 0; i < ordersMade; ++i) {
 System.out.println(&quot; &quot;);
 System.out.println(&quot;total CoffeeFlavor objects made: &quot; + 
[1] *Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides (1995). Design Patterns: Elements of Reusable Object-Oriented Software.
Addison-Wesley. pp. 205\u2013206. ISBN 0-201-63361-2.
[2] Calder, Paul R.; Linton, Mark A. (October 1990). &quot;Glyphs: Flyweight Objects for User Interfaces&quot;. The 3rd Annual ACM SIGGRAPH
Symposium on User Interface Software and Technology. Snowbird, Utah, United States. pp. 92-101. doi:10.1145/97924.97935.
ISBN 0-89791-410-4.
[3] Weinand, Andre; Gamma, Erich; Marty, Rudolf (1988). &quot;ET++\u2014an object oriented application framework in C++&quot;. OOPSLA
(Object-Oriented Programming Systems, Languages and Applications). San Diego, California, United States. pp. 46-57.
doi:10.1145/62083.62089. ISBN 0-89791-284-5.
Flyweight pattern 112
External links
\u2022 Flyweight in UML and in LePUS3 (a formal modelling language) (http:/ / www. lepus. org. uk/ ref/ companion/
Flyweight. xml)
\u2022 Article &quot; Enhancing Web Application Performance with Caching (http:/ / theserverside. com/ articles/ article.
tss?l=Caching)&quot; by Neal Ford
\u2022 Section &quot; Flyweight Text Entry Fields ( (http:/ / web. archive. org/ web/ 20070404160614/ http:/ / btl.
usc. edu/ rides/ documentn/ refMan/ rf21_d. html)&quot; from the RIDES Reference Manual by Allen Munro and
Quentin A. Pizzini
\u2022 Description (http:/ / c2. com/ cgi/ wiki?FlyweightPattern) from Portland's Pattern Repository
\u2022 Sourdough Design (http:/ / sourdough. phpee. com/ index. php?node=18)
\u2022 Class::Flyweight - implement the flyweight pattern in OO perl (http:/ / www. perlmonks. org/ ?node_id=94783)
\u2022 Boost.Flyweight - A generic C++ implementation (http:/ / boost. org/ libs/ flyweight/ index. html)
Proxy pattern
Proxy in UML
Proxy in LePUS3 (legend [2])
In computer programming, the proxy
pattern is a software design pattern.
A proxy, in its most general form, is a
class functioning as an interface to
something else. The proxy could
interface to anything: a network