Professional Documents
Culture Documents
Light
نريد نمذجة دارات منطقية اعتباراً من Componentsكما في الشكل التالي :
Or
اىـ َٝ Componentيل ػي ٚاألمثش دخي ٗ ِٞخشج ٗحٞذ.
ٍِ أخو ثْبء داسح فإُ خشج ٝ Componentدت أُ ٝزصو إىٍ ٚذخو . Component
And
خشج اىـ َٝ Componentنِ أُ ٝنُ٘ فؼبالً أٗ الٕ .زا ٝز٘قف ػيّ٘ ٚع اىـ ٗ Componentق ٌٞدخئ.
ّق٘ه إُ دخالً فؼبالً إرا مبُ ٍ٘ص٘الً إى ٚخشج فؼبه ٗ غٞش فؼبه إرا مبُ ٍ٘ص٘الً إى ٚخشج غٞش فؼبه.
اىـ Generatorال َٝيل دخالً ٗ خشخٔ دٍٗب ً فؼبه.
Not
Light
isLighted() in Component_1_Out
Generator
isActive()
Component_1_in_1_Out Component_2_in_1_Out
in1 in1, in2
When to use :
to add responsibilities to individual objects dynamically and
transparently.
for responsibilities that can be withdrawn.
when extension by subclassing is impractical. Sometimes a large
number of independent extensions are possible and would produce
an explosion of subclasses to support every combination.
System.out.println("End of For.");
//
// Several ohers thousands of System.out.println().
//
System.out.println("End of program.");
}
}
super(d);
}
protected String decor(String message) { DecoratorDebugger
DecoratorDebugger()
return "[" + new java.util.Date() + "] : " +
Display(msg)
message; Abstract Decor(msg)
} public class DebuggerWithoutDecoration
} extends DecoratorDebugger {
public DebuggerDecoredWithDate (Debugger d){ DebuggerDecor
super(d); edWithDate
Decor(msg)
}
protected String decor(String message) { DebuggerWitho
return message; utDecoration
Decor(msg)
}
}
SolutionWithDebugger.main();
SolutionWithDebogger.main("to screen with decoration",
new DebuggerDecoredWithDate(toScreen) ); هل تتذكرون شيئا ً مشابها ً بالـجافا؟
إغالق الـ
إغالق الـ
ClientSocket
ConnectionSocket
باسم قصيبة.د 32
Distributed Sys - Damas univ
TCP Socket
Socket A on M1 Socket B on M2
Remote Remote
@IP = "M2" Out 1 In 2 @IP = "M1"
Port = 5040 Port = 6070
A.getInetAddress() = B.getLocalInetAddress()
A.getPort() = B.getLocalPort()
A.getLocalInetAddress() = B.getInetAddress()
A.getLocalPort() = B.getPort()
باسم قصيبة.د 33
Distributed Sys - Damas univ
JAVA – Sum TCP Client
import java.io.*;
import java.net.*;
public class sumClient {
public static void main(String[] args)throws Exception {
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
try {
InetAddress lclHost = InetAddress.getLocalHost();
Socket clientSocket = new Socket(lclHost, 6789);
outToServer.writeInt(n1);
outToServer.writeInt(n2);
باسم قصيبة.د 34
Distributed Sys - Damas univ
JAVA – Sum TCP Server
import java.io.*;
import java.net.*;
public class sumServer {
public static void main(String[] args) throws Exception {
int n1 = inFromClient.readInt();
int n2 = inFromClient.readInt();
outToClient.writeInt(sum);
}
}
}
باسم قصيبة.د 35
Distributed Sys - Damas univ
Proxy : JavaRMI
Ex : Remote interface + Impl Object
import java.rmi.*;
public interface Addition extends Remote
{
public int add(int x, int y) throws RemoteException;
}
import java.rmi..*;
public class AdditionImpl implements Addition
{
public int add (int x, int y) throws RemoteException
{
return x + y ;
}
}
باسم قصيبة.د 36
Distributed Sys - Damas Univ
Proxy : JavaRMI
Ex : Server
//deployment of servants
import java.rmi.*;
import java.rmi.server.*;
public class AdditionServer{
public static void main(String[] args){
try{
Addition a = new AdditionImpl();
UnicastRemoteObject.exportObject(a);
Naming.rebind("/Firstpro", a);
System.out.println("First remote program ……”);
System.out.println("main thread finished”);
}
catch(Exception ex) {ex.printStackTrace)(;}
}
}
باسم قصيبة.د 37
Distributed Sys - Damas Univ
Proxy : JavaRMI
Ex : Client
import java.rmi.*;
public class AdditionClient{
public static void main(String[] args) throws Exception {
Addition a = (Addition) Naming.lookup("/Firstpro”);
System.out.println(a.add(5, 3));
}
}
Local machine Remote machine
2 1
/ client look up
Registry
bind / server
3 0
Object
export
9 Server
7
RMI return 8 return RMI
Stub result Skeleton
Client Server
4 5
Method Argument
call marshaling
باسم قصيبة.د 38
Distributed Sys - Damas Univ
Structural Design Patterns
Composite = Compose objects into tree structures to represent part-whole
hierarchies. Composite lets clients treat individual objects and
compositions of objects uniformly.
Adapter = Convert the interface of a class into another interface clients
expect. Adapter lets classes work together that couldn't otherwise because
of incompatible interfaces.
Bridge = Decouple an abstraction from its implementation so that the two
can vary independently.
Decorator = Attach additional responsibilities to an object dynamically.
Decorators provide a flexible alternative to subclassing for extending
functionality.
Façade = Provide a unified interface to a set of interfaces in a subsystem.
Facade defines a higher-level interface that makes the subsystem easier to
use.
Flyweight = Use sharing to support large numbers of fine-grained objects
efficiently.
Proxy = Provide a surrogate or placeholder for another object to control
access to it.
Soft Eng 3 - Damas - باسم قصيبة.د 39
2009
Adapter (1)
Convert the interface of a class into another interface clients expect.
Adapter lets classes work together that couldn't otherwise because of
incompatible interfaces.
To use when :
a class exists, but its interface does not match the one you need.
you need to use several existing subclasses, but it's impractical to adapt
their interface by subclassing every one. An object adapter can adapt the
interface of its parent class.
To use when :
to avoid a permanent binding between an abstraction and its
implementation. (example, the implementation must be selected or
switched at run-time).
both the abstractions and their implementations are independently
extensible by subclassing.
changes in the implementation of an abstraction should have no impact on
clients; that is, their code should not have to be recompiled.
Soft Eng 3 - Damas - باسم قصيبة.د 46
2009
Bridge (4)
Example : Forall x in l
imp.addLine (x)
Applicability :
An application uses a large number of objects.
Storage costs are high because of the sheer quantity of objects.
Most object state can be made extrinsic.
Many groups of objects may be replaced by relatively few shared objects
once extrinsic state is removed.
The application doesn't depend on object identity.