Tutorial Factory Pattern (GUI Design)
Tutorial Factory Pattern (GUI Design)
It is very basic and useful pattern for GUI design. What? Don?t u kno w it? Use google search Factory Pattern. Don?t confuse: design pattern only a so rt of strategy of programming, not a component or a bean. Let us ignore boring n otation of Factory Pattern, directly go to my case study: Problem: assume we need to display different style, color, content, size?. Of te xt on different panel. Solution: What is your design idea? Don?t tell me you will create each scrollpan e for each textarea for each different documents one by one. It is tedious work, I wont do that. I will use Factory Pattern bec I am lazy. Step 1 for Factory Pattern: find the common properties of the objects (here text ), abstract class and interface is basic elements for design pattern, I create a bstractTextArea class here: import javax.swing.*; import java.awt.*; public abstract class abstractTextArea{ abstract public String getFontName(); abstract public int getFontStyle(); abstract public int getFontSize(); abstract public Color getFontColor(); abstract public String getText(); JTextArea a=new JTextArea(); JScrollPane b= new JScrollPane(area()); public JTextArea area() { a.setText(getText()); a.setFont(new Font(getFontName(),getFontStyle(), getFontSize())); a.setLineWrap(true); a.setWrapStyleWord(true); a.setEditable(false); a.setForeground(getFontColor()); return a; } public JScrollPane scroll(){ b.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); b.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
return b; } } In this class, we create 5 properties for the text and definiate the common user interface which will be used for displaying text. Step 2: let us ?define one concrete instance of one of possible subclasses depen ding on the data which it is presented with?, I hate notation, but sometimes the y are necessary. assume the request text has such properties: font name: gg, fon t style: ITALIC, font size: 20, font color: BLUE, content: ?slfdsalfd?, so I hav e the one subclass here: import java.awt.*; public class textArea extends abstractTextArea{ public String getFontName() {return "gg";} public int getFontStyle() {return Font.ITALIC;} public int getFontSize() {return 20;} public Color getFontColor() {return Color.BLUE;} public String getText() {return "dsfdskfdsk";} } Also we can create another totally different instance: import java.awt.*; public class textArea1 extends abstractTextArea{ public String getFontName() {return "dfdf";} public int getFontStyle() {return Font.BOLD;} public int getFontSize() {return 15;} public Color getFontColor() {return Color.RED;} public String getText() {return "dsfdsf";} } You can create countless instance inherit the one parent class!!!!! Step 3: the actual work display those different text(very very easy job): import javax.swing.*; public class factoryTabbedPane extends JFrame { //initiate those subclasses private abstractTextArea test1=new textArea(); private abstractTextArea test2=new textArea1(); //add more as you wish?. public factoryTabbedPane () {
super("FactoryPattern"); JTabbedPane tabbedPane = new JTabbedPane(); ImageIcon icon; /*add those components which contain the text,actually we only create one, but i t extends many different appearance children*/ tabbedPane.addTab("Tab 1", null, test1.scroll(), "Does nothing"); tabbedPane.addTab("Tab 2", null, test2.scroll(), "Does nothing"); //add more as you wish??. add(tabbedPane); setDefaultLookAndFeelDecorated(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setSize(500,500); setVisible(true); } public static void main(String[] args) { new factoryTabbedPane(); } } From this example, you can easily create a simple internet brower with different display modes. Hope it is helpful, se ya!