Design Patterns
239 pág.

Design Patterns


DisciplinaEngenharia de Software I5.641 materiais57.743 seguidores
Pré-visualização50 páginas
Abstract factory pattern 18
Java
/* GUIFactory example -- */
interface GUIFactory {
 public Button createButton();
}
class WinFactory implements GUIFactory {
 public Button createButton() {
 return new WinButton();
 }
}
class OSXFactory implements GUIFactory {
 public Button createButton() {
 return new OSXButton();
 }
}
interface Button {
 public void paint();
}
class WinButton implements Button {
 public void paint() {
 System.out.println("I'm a WinButton");
 }
}
class OSXButton implements Button {
 public void paint() {
 System.out.println("I'm an OSXButton");
 }
}
class Application {
 public Application(GUIFactory factory) {
 Button button = factory.createButton();
 button.paint();
 }
}
public class ApplicationRunner {
 public static void main(String[] args) {
 new Application(createOsSpecificFactory());
 }
 public static GUIFactory createOsSpecificFactory() {
 int sys = readFromConfigFile("OS_TYPE");
Abstract factory pattern 19
 if (sys == 0) return new WinFactory();
 else return new OSXFactory();
 }
}
Objective-C
/* GUIFactory example -- */
#import <Foundation/Foundation.h>
@protocol Button <NSObject>
- (void)paint;
@end
@interface WinButton : NSObject <Button>
@end
@interface OSXButton : NSObject <Button>
@end
@protocol GUIFactory
- (id<Button>)createButton;
@end
@interface WinFactory : NSObject <GUIFactory>
@end
@interface OSXFactory : NSObject <GUIFactory>
@end
@interface Application : NSObject
- (id)initWithGUIFactory:(id)factory;
+ (id)createOsSpecificFactory:(int)type;
@end
@implementation WinButton
- (void)paint {
 NSLog(@&quot;I am a WinButton.&quot;);
}
@end
@implementation OSXButton
- (void)paint {
 NSLog(@&quot;I am a OSXButton.&quot;);
}
@end
Abstract factory pattern 20
@implementation WinFactory
- (id<Button>)createButton {
 return [[[WinButton alloc] init] autorelease];
}
@end
@implementation OSXFactory
- (id<Button>)createButton {
 return [[[OSXButton alloc] init] autorelease];
}
@end
@implementation Application
- (id)initWithGUIFactory:(id)factory {
 if (self = [super init]) {
 id button = [factory createButton];
 [button paint];
 }
 return self;
}
+ (id)createOsSpecificFactory:(int)type {
 if (type == 0) {
 return [[[WinFactory alloc] init] autorelease];
 } else {
 return [[[OSXFactory alloc] init] autorelease];
 }
}
@end
int main(int argc, char* argv[]) {
 @autoreleasepool {
 [[Application alloc] initWithGUIFactory:[Application 
createOsSpecificFactory:0]];// 0 is WinButton
 }
 return 0;
}
Lua
--[[
 Because Lua is a highly dynamic Language, an OOP scheme is 
implemented by the programmer.
 The OOP scheme implemented here implements interfaces using 
documentation.
 
 
 A Factory Supports:
 - factory:CreateButton()
Abstract factory pattern 21
 
 A Button Supports:
 - button:Paint()
]]
-- Create the OSXButton Class
do
 OSXButton = {}
 local mt = { __index = OSXButton }
 
 function OSXButton:new()
 local inst = {}
 setmetatable(inst, mt)
 return inst
 end
 
 function OSXButton:Paint()
 print(&quot;I'm a fancy OSX button!&quot;)
 end
end
-- Create the WinButton Class
do
 WinButton = {}
 local mt = { __index = WinButton }
 
 function WinButton:new()
 local inst = {}
 setmetatable(inst, mt)
 return inst
 end
 
 function WinButton:Paint()
 print(&quot;I'm a fancy Windows button!&quot;)
 end
end
-- Create the OSXGuiFactory Class
do
 OSXGuiFactory = {}
 local mt = { __index = OSXGuiFactory }
 
 function OSXGuiFactory:new()
 local inst = {}
 setmetatable(inst, mt)
 return inst
Abstract factory pattern 22
 end
 
 function OSXGuiFactory:CreateButton()
 return OSXButton:new()
 end
end
-- Create the WinGuiFactory Class
do
 WinGuiFactory = {}
 local mt = { __index = WinGuiFactory }
 
 function WinGuiFactory:new()
 local inst = {}
 setmetatable(inst, mt)
 return inst
 end
 
 function WinGuiFactory:CreateButton()
 return WinButton:new()
 end
end
-- Table to keep track of what GuiFactories are available
GuiFactories = {
 [&quot;Win&quot;] = WinGuiFactory,
 [&quot;OSX&quot;] = OSXGuiFactory,
}
--[[ Inside an OS config script ]]
OS_VERSION = &quot;Win&quot;
--[[ Using the Abstract Factory in some the application script ]]
-- Selecting the factory based on OS version
MyGuiFactory = GuiFactories[OS_VERSION]:new()
-- Using the factory
osButton = MyGuiFactory:CreateButton()
osButton:Paint()
Abstract factory pattern 23
References
[1] [ |Gamma, Erich (http:/ / www. informit. com/ authors/ bio. aspx?a=725735c6-e618-488a-9f9b-a3b8344570dc)]; Richard Helm, Ralph
Johnson, John M. Vlissides (2009-10-23). &quot;Design Patterns: Abstract Factory&quot; (http:/ / www. informit. com/ articles/ article. aspx?p=1398599)
(in English) (HTML). informIT. Archived from the original (http:/ / www. informit. com/ ) on 2009-10-23. . Retrieved 2012-05-16. &quot;Object
Creational: Abstract Factory: Intent: Provide an interface for creating families of related or dependent objects without specifying their concrete
classes.&quot;
[2] [ |Veeneman, David (http:/ / www. codeproject. com/ script/ Membership/ View. aspx?mid=319264)] (2009-10-23). &quot;Object Design for the
Perplexed&quot; (http:/ / www. codeproject. com/ Articles/ 4079/ Object-Design-for-the-Perplexed) (in English) (HTML). The Code Project.
Archived from the original (http:/ / www. codeproject. com/ ) on 2011-09-18. . Retrieved 2012-05-16. &quot;The factory insulates the client from
changes to the product or how it is created, and it can provide this insulation across objects derived from very different abstract interfaces.&quot;
[3] &quot;Abstract Factory: Implementation&quot; (http:/ / www. oodesign. com/ abstract-factory-pattern. html) (in English) (HTML). OODesign.com. .
Retrieved 2012-05-16.
[4] http:/ / www. lepus. org. uk/ ref/ legend/ legend. xml
External links
\u2022 Abstract Factory (http:/ / www. lepus. org. uk/ ref/ companion/ AbstractFactory. xml) UML diagram + formal
specification in LePUS3 and Class-Z (a Design Description Language)
Builder pattern
The builder pattern is an object creation software design pattern. The intention is to abstract steps of construction of
objects so that different implementations of these steps can construct different representations of objects. Often, the
builder pattern is used to build products in accordance with the composite pattern.
Definition
The intent of the Builder design pattern is to separate the construction of a complex object from its representation.
By doing so, the same construction process can create different representations. [1]
Structure
Builder
Abstract interface for creating objects (product).
Concrete Builder
Provides implementation for Builder. It is an object able to construct other objects. Constructs and assembles
parts to build the objects.
Builder pattern 24
Useful tips
\u2022 Builder focuses on constructing a complex object step by step. Abstract Factory emphasizes a family of product
objects (either simple or complex). Builder returns the product