JAVA PROGRAMMING Answer these questions a What is the purpos

 JAVA PROGRAMMING  Answer these questions:  a) What is the purpose of a design pattern ?  b) When do you apply the Observer pattern ?  c) You review a design written by somebody else for an application and you find these:    - an interface Shape with a method draw()   - a class Circle that implements Shape   - a class Rectangle that implements Shape   - a class CompoundShape that:        o  implements interface Shape        o  aggregates 0 or more Shape objects,         o  has an extra method called add(Shape sh)        o  for implementing method draw() calls the draw() method for all           aggregated Shape objects.     You assume that a CompoundShape object is made of multiple shapes. What design pattern is at work in this application? Explain your answer.  d) The TitledBorder class can give a title to a border. Consider the code      panel.setBorder(new TitledBorder(new EtchedBorder(), \"Enter Option\"));  What design pattern(s) are at work? Explain your answer. 

Solution

a) In java design patterns are well-proved solution for solving a specific problem or task.

So whenever I say a specific problem or task you may have a doubt what kind of problem

so let me explain in detail by taking an example

Suppose you want to create a class for which only a single instance or object is to be created and that single object can be used by all other classes.

This is the problem , to this Singleton design pattern is the best solution problem. So, every design pattern has some specification or set of rules for solving the problems.

you have to remember one-thing that design patterns are programming language independent strategies for solving the common object-oriented design problems. That means it represents an idea, not a particular implementation.

By using this you can make your code more flexible, reusable and maintainable. java internally follows design patterns.

b)

The Observer Pattern is a behavioral design pattern, it defines a one-to-many relationship between objects i.e., when one object changes its state, all dependent objects are notified and updated automatically. for example, I used this pattern to link the portal’s authentication mechanism with the forum software. The act of logging into the portal also automatically triggered the user to be logged into the forum as well.

c)

Interface is the code that is used to create an interface in java.It provides absolute abstraction

For Circle Class---------

Circle class has implemented all the methods defined in the interface and it has some of its own methods also like getRadius(). The interface implementations can have multiple type of constructors.

Rectangle:------------------------

Interface provides a contract for all the implementation classes, so its good to code in terms of interfaces because implementation classes can’t remove the methods we are using.

 JAVA PROGRAMMING Answer these questions: a) What is the purpose of a design pattern ? b) When do you apply the Observer pattern ? c) You review a design writte

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site