All procedures are implemented in the following environments:
Operating system: Windows XP Pro SP1
Programming environment: J2SDK
1.4.2
, JCREATOR Pro 2.00
UML Tools: Borland Together Architect
Reference book: "Java and mode" Hong
Print Typography: Microsoft Office Word 2003
Create mode - Factory Method
This is an example of the creation of a mode factory method: a tableware production plant (TablewareFactory), production
Three tableware: Chopstics, Knife (Knife), Fork. The production process uses Factory Method. Interface TableWare as a common interface of Fork, Knife, Chopstics. Interface TablewareMaker as the public interface of Production Worker forkmaker, Knifemaker, Chopmaker. The class diagram is as follows:
The implementation code is as follows:
1 - Implementation of interface Tableware
Package zhijun.javapattern.FactoryMethod;
Public interface Tableware {public void getmes ();
2 - implementation of the FORK class
Package zhijun.javapattern.FactoryMethod;
Public Class fork Implements Tableware
{
Public fork () {system.out.println ("a fork isbook.");}
Public void getmes () {system.out.println ("this is a fork.");}
}
3 - Realization of Knife class
Package zhijun.javapattern.FactoryMethod;
Public Class Knife Implements Tableware
{
Public knife () {system.out.println ("a knife isbook.");}
Public void getmes () {system.out.println ("this is a knife.");}
}
4 - Realization of CHOPSTICTS class
Package zhijun.javapattern.FactoryMethod;
Public Class Chopsticks IMPLEments TABLEWARE
{
Public chopsticks () {system.out.println ("a Pair of Chopsticks Are Made.");
Public void getmes () {system.out.println ("They Are A Pair of Chopsticks);
}
5 - Interface TablewareMaker
Package zhijun.javapattern.FactoryMethod;
Public interface Tableware Make ();} 6 - implementation of the ForkMaker class
Package zhijun.javapattern.FactoryMethod;
Public Class forkmaker Implements TablewareMaker
{
Public forkmaker () {}
Public Tableware Make () {return new fork ();
}
7 - Realization of Knifemaker class
Package zhijun.javapattern.FactoryMethod;
Public Class KnifeMaker Implements TablewareMaker
{
Public knifemaker () {}
Public Tableware Make () {Return New Knife ();
}
8 - Realization of ChopMaker class
Package zhijun.javapattern.FactoryMethod;
Public Class Chopmaker IMPLEments TablewareMaker
{
Public chopmaker () {}
Public Tableware make () {return new choSticks ();
}
9 - Client program TablewareFactory implementation
Package client_test;
Import zhijun.javapattern.FactoryMethod. *;
Public Class TablewareFactory
{
Public Static TablewareMaker forkmaker, Knifemaker, Chopsticksmaker;
Public Static Tableware fork, Knife, Chopsticks;
Public static void main (string [] args)
{
Forkmaker = new forkmaker ();
Knifemaker = new knifemaker ();
Chopsticksmaker = new chopmaker ();
Fork = forkmaker.make ();
Knife = knifemaker.make ();
Chopsticks = chopsticksmaker.make ();
}
}
Structural mode - FACADE mode
This example comes from "Java and Mode". Program Description: This program simulates a security system to explain the facade mode. This security system consists of 2 recorders (Camera), 2 electric lights, a sensor (Sensor), a alarm (ALARM). The security system operators need to start (Activate) and close (deActivate). According to the facade mode, design a console (SecurityFacade) as a user interface of the security system. The security system operators only need to operate this simplified interface to control all instruments.
The facade securityFacade object assumes the task of dealing with each object of the security system, while the customer object only needs to deal with the facade object. This is a portal between the client and the security system, which makes the relationship between the client and the internal programs make simple and easy to manage. The system class diagram is as follows: The source code is as follows:
1 - Client Program Client .java
Package zhijun.javapattern.facade;
Public Class Client
{
Private static securityfacade security;
Public static void main (string [] args)
{
Security = new securityFacade ();
Security.Activate ();
Security.deactivate ();
}
}
2 - Facade securityFacade.java
Package zhijun.javapattern.facade;
Public Class SecurityFacade
{
Private Camera Camera1 = New Camera ();
Private Camera Camera2 = New Camera ();
PRIVATE LIGHT1 = New Light ();
PRIVATE LIGHT2 = New Light ();
PRIVATE SENSOR SENSOR = New Sensor ();
Private alarm alarm = new alarm ();
Public void activate ()
{
Camera1.turnon (); Camera2.turnon ();
Light1.turnon (); Light2.turnon ();
Sensor.Activate (); alarm.acticate ();
}
Public void deactivate ()
{
Camera1.Turnoff (); Camera2.Turnoff ();
Light1.turnoff (); Light2.Turnoff ();
Sensor.Activate (); alarm.acticate ();
}
}
3 - Camera Camera.java
Package zhijun.javapattern.facade;
Public Class Camera
{
Public void Turnon () {System.out.println ("Turn on The Camera.");
Public void turnoff () {system.out.println ("Turn Off The Camera.");
Public void rotate (int De De ")
{System.out.println ("Rotate The Camera By" DegRees DegRees. ");}
}
4 - Electric Light Light.java
Package zhijun.javapattern.facade;
Public Class Light
{
Public void Turnon () {System.out.println ("Turn on The Light.");
Public void turnoff () {system.out.println ("Turn Off The Light.");} // Replace Bulb
Public void changebulb () {sysetm.out.println ("Change THS Ligy Bulb");
}
5 - Sensor Sensor.java
Package zhijun.javapattern.facade;
Public Class Sensor
{
Public void activate () {system.out.println ("Activating the Sensor.");
Public void deactivate () {system.out.println ("deactivating the sensor.");
// Trigger sensor
Public void Trigger () {system.out.println ("The Sensor Has Been Triggered.");
}
6 - Alarm Alarm.java
Package zhijun.javapattern.facade;
Public Class Alarm
{
Public void activate () {system.out.println ("Activating the alarm.");
Public void deactivate () {system.out.println ("deActivating the alarm.");}
// Alarm ringing ring
Public void Ring () {system.out.println ("Ring The alarm.");}
// Stop alarm
Public void stopring () {system.out.println ("stop the alarm.");
}
Behavior mode - Command
This example comes from "Java and Mode". The Command mode is a package of commands. Command mode The responsibility of issuing commands and the responsibility of the execution command delegates different objects. Each is an operation: one request is requested to perform an action; accepting a request and performs actions.
This example demonstrates the application of Command by simulating a system that looks at the TV through the remote control TV.
In the design, TVWatcher plays a customer role, Controler plays a requestor, and TVWatcher issues a command through the Controler (Remote), which is written by the TV. Abstract command roles are played by a command interface, there are three specific command classes: Oncommand, Offcommand, ChannelCommand, represents 3 operations: Turn on TV, turn off TV, and change the Taiwan. Controler (Remote Control) The details of the implementation are separated from the client, and TVWatcher (watching TV) only needs to operate the remote control (the corresponding key) can achieve the operation of TV (opening, closing, change) station).
The UML class diagram of the system is as follows:
The source code is as follows: 1 - client program:
Package client_test;
Import zhijun.javapattern.Command. *;
Public class tvwatcher
{
Private static controller;
Public static void main (string [] args)
{Test ();
Public static void test ()
{
TV mytv = new TV ();
OnCommand on = New Oncommand (MyTV);
OFFCOMMAND OFF = New Offcommand (MyTV);
ChannelCommand Channel = New ChannelCommand (MyTV, 2);
Controler = New Controler (on, off, channel);
Controle.Turnon ();
Controler.changechannel ();
Controle.Turnoff ();
}
}
2 - Command Interface Command.java
Package zhijun.javapattern.Command;
Public Interface Command
{
Void Execute ();
}
3 - Specific command oncommand.java
Package zhijun.javapattern.Command;
Public Class Oncommand Implements Command
{
Private TV myTV;
Public OnCommand (TV TV) {mYTV = TV;}
Public void execute () {mytv.turnon ();
}
4 - Specific command offcommand.java
Package zhijun.javapattern.Command;
Public Class Offcommand Immments Command
{
Private TV myTV;
Public Offcommand (TV TV) {mytv = TV;
Public void execute () {mytv.turnoff ();
}
5 - Specific command ChannelCommand.java
Package zhijun.javapattern.Command;
Public Class ChannelCommand IMPLEMENTS COMMAND
{
Private TV myTV;
Private int channel;
Public ChannelCommand (TV TV, int channel "
{
Mytv = TV;
THIS.CHANNEL = CHANNEL;
}
Public void execute () {mytv.changechannel;}
}
6 - Commandor recent Controler.java
Package zhijun.javapattern.Command;
Public Class Controler
{
Private Command Oncommand, Offcommand, ChannelCommand;
Public Controler (Command ON, Command Off, Command Channel)
{
OnCommand = ON;
OFFCOMMAND = OFF;
ChannelCommand = CHANNEL;
Public void turnon () {oncommand.execute ();
Public void turnoff () {OFFCommand.execute ();
Public void chancechannel () {channelcommand.execute ();
}
7 - Command Receiver TV.JAVA
Package zhijun.javapattern.Command;
Public Class TV
{
Private int currentchannel = 0;
Public void Turnon () {system.out.println ("THE TV is on.");
Public void turnoff () {system.out.println ("THE TV is off.");}
Public Void ChangeChannel (Int Channel)
{
Currentchannel = CHANNEL
System.out.println ("Now TV Channel IS: Channel);
}
}
Run results screenshot: