Académique Documents
Professionnel Documents
Culture Documents
2
Structural
patterns
Contents
1. Concept of structural pattern. . . . . . . . . . . . . . . . . . . . . . . . 4
2. Adapter pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3. Bridge pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4. Composite pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.1. Pattern purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.2. Causes of pattern occurrence. . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.3. Pattern structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4.4. Results of pattern use. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23
4.5. Example of a practical pattern application. . . . . . . . . . . . . . . . 23
5. Decorator pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
6. Facade pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
7. Flyweight pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
7.1. Pattern purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
7.2. Causes of pattern occurrence. . . . . . . . . . . . . . . . . . . . . . . . . . . 42
7.3. Pattern structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
7.4. Results of pattern use. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46
7.5. Case example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
8. Proxy pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
1. Concept of structural
pattern
Based on the definition of information systems, it is obvious
that any software solution operates some block of data. When
analyzing them we obtain some information that allows us to
make business and regular decisions on management of the
Information System itself.
The analysis of data assumes that it should be arranged in a
structure. On the one side, it allows to identify different data
(logically distinguish them), on the other side to organize
elementary values into structured objects and their combinations.
Data structuring is necessary to reflect the concepts used in
the analysis and display the relationships formed between the
objects in the process of their occurrence.
Object-oriented approach provides us with a wealth of tools
for description of structured concepts and reflection of their
relationships. However, the organization of structure that connects
concepts into a working system is critical for the effective use
of information systems, as well as maintenance obtained in the
result of the product development process.
In general, design patterns reflect the best models of making
decisions on engineering problems, which stand the test of time
and practice. Structural patterns provide models of organization
of data into structures, which solve the problem of organization
of data management in the most efficient way. It is understood
that the data may be presented not only with atomic values, but
also with all the objects existing within our application.
4
Lesson 2
2. Adapter pattern
2.1. Pattern purpose
Lesson 2
Lesson 2
10
Lesson 2
11
Lesson 2
13
14
Lesson 2
3. Bridge pattern
3.1. Pattern purpose
16
Lesson 2
18
Lesson 2
19
4. Composite pattern
4.1. Pattern purpose
Lesson 2
21
22
Lesson 2
24
Lesson 2
25
5. Decorator pattern
5.1. Pattern purpose
Lesson 2
27
28
Lesson 2
30
Lesson 2
31
Lesson 2
33
34
Lesson 2
6. Facade pattern
The Facade pattern is largely designed for encapsulation
(hiding) of content and division of logical parts to independent
subsystems. In the real world the Facade pattern structure
is applied in many situations. A good example is a fast-food
restaurant, where you just come to the cashiers desk and order
the meals. As a rule, you are not interested in the detailed
structure of such a quick service restaurant. However, there is
also an office in this restaurant. You may come there and refer
to it according to the narrow interface (for example, to become
a regular client). Thus, the restaurant gets two subsystems,
which depend on each other in minimum. Together they are
added up to one big system.
User
subsystem
Restaurant
subsystem
Office
Cashiers desk
Reception desk
Accounting
department
Kitchen
Support
services
Warehouse
Cleaning
35
Personnel
Department
Marketing
Lesson 2
37
38
Lesson 2
39
40
Lesson 2
41
7. Flyweight pattern
7.1. Pattern purpose
Lesson 2
44
Lesson 2
45
// Return the object resulting from the collection or a newly created one.
return D;
Lesson 2
47
48
Lesson 2
49
8. Proxy pattern
The Proxy pattern is often called a substitute pattern. Further,
the main class that will be discussed is referred to us as a
substitute or a proxy. Both terms are correct and describe the
same class/object.
Proxy is an object providing a transit access to another object.
Substitute is an object that externally looks in the same way
as the main one, but it has no internal functionality.
A substitute or a proxy object externally looks in the same
way as the object represented by it. However, all of its internal
functionality is a kind of a tunnel providing the client classes
with a possibility to access the main objects.
Additionally, a substitute or a proxy object may be called
a stand-in object.
Client
Substitute
Target class
Lesson 2
If the target class object will occupy a lot of space after its
creation and there is no guarantee that the object will be needed
in the application, it is possible to create a target object when
the client calls the method of the substitute object at the first
time. It will speed the application performance up if the object
will not be used. However, if the object is created, then it will
not be so harmful for the program performance.
Such a system of access will allow us to use a target class in
the same, but more protected way (it is possible to perform
various checks of the accepted parameters within a substitute).
For example, if a target class is a calculator, it just adds two
parameters into the method arguments and performs basic
mathematical computations. Within the substitute of the
calculator class, it is possible to check that the calculator will
not be given such a task, as to divide by zero.
You can use structure of the proxy pattern in order to create
an object sent into another name space. For example, target
class is described in the name space that for some reason is
impossible (or uncomfortable) to connect. In this case we are
able to create a substitute that will be located in the needed
name space and the target class object will be created within
the substitute taken from another name space.
52
Lesson 2
Lesson 2
55
}
return "You have no access";
}
56
Lesson 2
Lesson 2
59
60
Lesson 2
61
62
Lesson 2
Menu
Bar
64
Lesson 2
set
{
__text = value;
TextChanged(this, new EventArgs());
}
}
static void TextArea_TextChanged(object sender, EventArgs e)
{
}
public virtual void KeyPressEventHeandler(object sender, KeyPressEventArgs arg)
{
if ((int)arg.KeyChar == 8)
{
if (Text.Length > 0)
{
Text = Text.Remove(Pointer-1, 1);
Pointer--;
}
}
else if (!Char.IsControl(arg.KeyChar))
{
Text = Text.Insert(Pointer++, arg.KeyChar.ToString());
}
}
public void KeyDownEventHandler(object sender, KeyEventArgs arg)
{
switch (arg.KeyData)
{
case Keys.Left:
if (Pointer > 0)
Pointer--;
break;
case Keys.Right:
if (Pointer < Text.Length)
Pointer++;
break;
}
}
}
65
Lesson 2
67
Lesson 2
69
Repaint(
mainPanel,
new PaintEventArgs(
mainPanel.CreateGraphics(),
mainPanel.ClientRectangle));
}
public void Repaint(object sender, PaintEventArgs e)
{
Image img = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
Graphics DC = Graphics.FromImage(img);
DC.Clear(BackColor);
BorderDecor.Paint(DC, ClientRectangle);
e.Graphics.DrawImage(img, 0,0);
DC.Dispose();
img.Dispose();
}
private void mainPanel_Resize(object sender, EventArgs e)
{
Repaint(
mainPanel,
new PaintEventArgs(
mainPanel.CreateGraphics(),
mainPanel.ClientRectangle));
}
private void Open_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = false;
dlg.Filter = "Text files|*.txt";
if (dlg.ShowDialog() == DialogResult.OK)
{
(View as TextView).Text = System.IO.File.ReadAllText(dlg.FileName,Encoding.Default);
RecountScrollMaximum();
mainPanel.Invalidate();
}
}
private void Exit_Click(object sender, EventArgs e)
{
Application.Exit();
}
70
Lesson 2
watering process
Temperature
watering process
Obtain a plan
To be poured
Date
For
Enter
l
minutes
72
Lesson 2
73
74
Lesson 2
75
<summary>
Method receives a plan by date
</summary>
<param name=Date>The day on which the plan is needed</param>
76
Lesson 2
Enter temperature
data
USER
77
78
Lesson 2
79