Recent researches show that predicative programming can be used to specify OO concepts including classes, objects, interfaces, methods, single and multiple inheritance and overriding. My aim in this project is to use bunches to specify design patterns, relying on existing laws, axioms and programming theories. I would take some of the patterns, use UML to describe them and integrate OO with existing type system to formally specify them. Motivation
Design patterns identify and document the essence of successful solutions to specific problems in object-oriented software design. Using patterns get you to concentrate on good OO designs and to capture expert knowledge and design tradeoffs and thus support the sharing of architectural knowledge among developers. The architecture of a software system can be clearly documented, using design patterns as a shared vocabulary for common design structures. The Value of Design Patterns -- learn from experience
What is a Design Pattern? In general, a pattern has four essential elements: pattern name, problem,solution and consequence. Classify by purpose, reflecting what a pattern does. What is a Design Pattern? -- learn from experience Creational Patterns - concern the process of object creation. Singleton: Ensure a class only has one instance, and provide a global point of access to it. Behavioral Patterns - characterize the ways in which classes or objects interact and distribute responsibility. Observer: Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
What is a Design Pattern? (cont’d) -- learn from experience Structural Patterns - deal with the composition of classes or objects. Adapter: Convert the interface of a class into another interface clients expect. Decorator: Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. Class patterns and Object patterns Class patterns deal with relationships between classes and their subclasses. Object patterns deal with object relationships, which can be changed at run-time and are more dynamic.
Example: Observer pattern and use UML to describe it 07:10:20 PM Clock Timer subject Change notification Requests, modification Example: observers How it is implemented? Have objects (Observers) that want to know when an event happens, attach themselves to another object (Subject) that is actually looking for it to occur. When the event occurs, the subject tells the observers that it occurred.
Example: Observer pattern and use UML to describe it (cont’d) When an abstraction has two aspects, one dependent on the other. Encapsulating these aspects in separate objects lets you vary and reuse them independently. When a change to one object requires changing others, and you don’t know how many objects need to be changed. When an object should be able to notify other objects without making assumptions about who these objects are. Where it is used?
Example: Class Diagram of Observer Pattern Subject + attach(Observer) + detach(Observer) + notify() Observer +update() ConcreteObserver +update() -observerState observerState = subject.GetState() notify: for all obervers: Call update() observers* ConcreteSubject +GetState() +SetState() -subjectState Return subjectState subject *
aConcreteSubjectaConcreteObserver anotherConcreteObserver Interaction Diagram of Observer Pattern setState() Notify() Update() getState() Update() getState()
A class specification has three parts: - a class interface, including attributes and functions: StackInt = “contents” -> list int | “top” -> int - a class definition, which define the functions: STACK = s:StackInt s“top” = s“contents”(#s“contents” –1) - zero or more procedures: push = s:STACK x:int “contents” -> s“contents” +[x] | s Specifying OO Concepts Instantiation of an object: - var s: classA
Specifying OO Concepts (cont’d) Single and Multiple inheritance: - B = “new” -> T | C - B = C1 | C2 | … | Ck Overriding and redefinition - if C is a class definition with function f : T and BInt inherit from C. B = b: BInt (b“f” = body) - a form of dynamic binding of functions: var a:listA var b:B “3”->b | a Accessing fields by dereferencing: - s“contents”